Class ServletUtil


  • public class ServletUtil
    extends Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.slf4j.Logger logServerStartup  
    • Constructor Summary

      Constructors 
      Constructor Description
      ServletUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String formFilename​(String dirPath, String filePath)
      Return the file path dealing with leading and trailing path separators (which must be a slash ("/")) for the given directory and file paths.
      static void forwardToCatalogServices​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
      Forward this request to the CatalogServices servlet ("/catalog.html").
      static String getParameterIgnoreCase​(javax.servlet.http.HttpServletRequest req, String paramName)
      Return the value of the given parameter for the given request.
      static String getReletiveURL​(javax.servlet.http.HttpServletRequest req)
      Return the request URL relative to the server (i.e., starting with the context path).
      static String getRequest​(javax.servlet.http.HttpServletRequest req)
      The entire request including query string
      static String getRequestBase​(javax.servlet.http.HttpServletRequest req)
      This is everything except the query string
      static String getRequestPath​(javax.servlet.http.HttpServletRequest req)
      servletPath + pathInfo
      static String getRequestServer​(javax.servlet.http.HttpServletRequest req)
      This is the server part, eg http://motherlode:8080
      static URI getRequestURI​(javax.servlet.http.HttpServletRequest req)
      The request base as a URI
      static void returnFile​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, File file, String contentType)
      Write a file to the response stream.
      static void returnFile​(javax.servlet.http.HttpServlet servlet, String contentPath, String path, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String contentType)
      Write a file to the response stream.
      static void returnFile​(javax.servlet.http.HttpServlet servlet, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, File file, String contentType)
      Write a file to the response stream.
      static void returnString​(String contents, javax.servlet.http.HttpServletResponse res)
      Send given content string as the HTTP response.
      static int setResponseContentLength​(javax.servlet.http.HttpServletResponse response, String s)
      Set the proper content length for the string
      static String showRequestDetail​(javax.servlet.http.HttpServletRequest req)
      Show details about the request
      static String showRequestHeaders​(javax.servlet.http.HttpServletRequest req)  
      static String showSecurity​(javax.servlet.http.HttpServletRequest req, String role)  
      static void showSession​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, PrintStream out)  
      static void showSystemProperties​(PrintStream out)  
      static void showThreads​(PrintStream pw)  
      static void writeMFileToResponse​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String requestPath)
      Write an MFile to the response stream.
    • Field Detail

      • logServerStartup

        public static final org.slf4j.Logger logServerStartup
    • Constructor Detail

      • ServletUtil

        public ServletUtil()
    • Method Detail

      • formFilename

        public static String formFilename​(String dirPath,
                                          String filePath)
        Return the file path dealing with leading and trailing path separators (which must be a slash ("/")) for the given directory and file paths.

        Note: Dealing with path strings is fragile. ToDo: Switch from using path strings to java.io.Files.

        Parameters:
        dirPath - the directory path.
        filePath - the file path.
        Returns:
        a full file path with the given directory and file paths.
      • returnFile

        public static void returnFile​(javax.servlet.http.HttpServlet servlet,
                                      String contentPath,
                                      String path,
                                      javax.servlet.http.HttpServletRequest req,
                                      javax.servlet.http.HttpServletResponse res,
                                      String contentType)
                               throws IOException
        Write a file to the response stream.
        Parameters:
        servlet - called from this servlet, may be null
        contentPath - file root path
        path - file path relative to the root
        req - the request
        res - the response
        contentType - content type, or null
        Throws:
        IOException - on write error
      • returnFile

        public static void returnFile​(javax.servlet.http.HttpServlet servlet,
                                      javax.servlet.http.HttpServletRequest req,
                                      javax.servlet.http.HttpServletResponse res,
                                      File file,
                                      String contentType)
                               throws IOException
        Write a file to the response stream. Handles Range requests.
        Parameters:
        servlet - called from this servlet, may be null
        req - the request
        res - the response
        file - to serve
        contentType - content type, if null, will try to guess
        Throws:
        IOException - on write error
      • returnFile

        public static void returnFile​(javax.servlet.http.HttpServletRequest req,
                                      javax.servlet.http.HttpServletResponse res,
                                      File file,
                                      String contentType)
                               throws IOException
        Write a file to the response stream. Handles Range requests.
        Parameters:
        req - request
        res - response
        file - must exist and not be a directory
        contentType - must not be null
        Throws:
        IOException - or error
      • writeMFileToResponse

        public static void writeMFileToResponse​(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response,
                                                String requestPath)
                                         throws IOException
        Write an MFile to the response stream.
        Parameters:
        request - the HttpServletRequest
        response - the HttpServletResponse
        requestPath - the request path
        Throws:
        IOException - if an I/O error occurs while writing the response.
      • returnString

        public static void returnString​(String contents,
                                        javax.servlet.http.HttpServletResponse res)
                                 throws IOException
        Send given content string as the HTTP response.
        Parameters:
        contents - the string to return as the HTTP response.
        res - the HttpServletResponse
        Throws:
        IOException - if an I/O error occurs while writing the response.
      • setResponseContentLength

        public static int setResponseContentLength​(javax.servlet.http.HttpServletResponse response,
                                                   String s)
                                            throws UnsupportedEncodingException
        Set the proper content length for the string
        Parameters:
        response - the HttpServletResponse to act upon
        s - the string that will be returned
        Returns:
        the number of bytes
        Throws:
        UnsupportedEncodingException - on bad character encoding
      • getReletiveURL

        public static String getReletiveURL​(javax.servlet.http.HttpServletRequest req)
        Return the request URL relative to the server (i.e., starting with the context path).
        Parameters:
        req - request
        Returns:
        URL relative to the server
      • forwardToCatalogServices

        public static void forwardToCatalogServices​(javax.servlet.http.HttpServletRequest req,
                                                    javax.servlet.http.HttpServletResponse res)
                                             throws IOException,
                                                    javax.servlet.ServletException
        Forward this request to the CatalogServices servlet ("/catalog.html").
        Parameters:
        req - request
        res - response
        Throws:
        IOException - on IO error
        javax.servlet.ServletException - other error
      • showSystemProperties

        public static void showSystemProperties​(PrintStream out)
      • getRequestServer

        public static String getRequestServer​(javax.servlet.http.HttpServletRequest req)
        This is the server part, eg http://motherlode:8080
        Parameters:
        req - the HttpServletRequest
        Returns:
        request server
      • getRequestBase

        public static String getRequestBase​(javax.servlet.http.HttpServletRequest req)
        This is everything except the query string
        Parameters:
        req - the HttpServletRequest
        Returns:
        parsed request base
      • getRequestURI

        public static URI getRequestURI​(javax.servlet.http.HttpServletRequest req)
        The request base as a URI
        Parameters:
        req - the HttpServletRequest
        Returns:
        parsed request as a URI
      • getRequestPath

        public static String getRequestPath​(javax.servlet.http.HttpServletRequest req)
        servletPath + pathInfo
        Parameters:
        req - the HttpServletRequest
        Returns:
        parsed request servletPath + pathInfo
      • getRequest

        public static String getRequest​(javax.servlet.http.HttpServletRequest req)
        The entire request including query string
        Parameters:
        req - the HttpServletRequest
        Returns:
        entire parsed request
      • getParameterIgnoreCase

        public static String getParameterIgnoreCase​(javax.servlet.http.HttpServletRequest req,
                                                    String paramName)
        Return the value of the given parameter for the given request. Should only be used if the parameter is known to only have one value. If used on a multi-valued parameter, the first value is returned.
        Parameters:
        req - the HttpServletRequest
        paramName - the name of the parameter to find.
        Returns:
        the value of the given parameter for the given request.
      • showRequestDetail

        public static String showRequestDetail​(javax.servlet.http.HttpServletRequest req)
        Show details about the request
        Parameters:
        req - the request
        Returns:
        string showing the details of the request.
      • showRequestHeaders

        public static String showRequestHeaders​(javax.servlet.http.HttpServletRequest req)
      • showSession

        public static void showSession​(javax.servlet.http.HttpServletRequest req,
                                       javax.servlet.http.HttpServletResponse res,
                                       PrintStream out)
      • showSecurity

        public static String showSecurity​(javax.servlet.http.HttpServletRequest req,
                                          String role)
      • showThreads

        public static void showThreads​(PrintStream pw)