Package thredds.util

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()  
    • 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 seperators (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.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
      • 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)