Class RequestUtils


  • public class RequestUtils
    extends Object
    Contains a set of static utility methods for working with requests
    Author:
    Aaron Zeckoski (azeckoski @ gmail.com)
    • Field Detail

      • mimeTypeToFormat

        public static Map<String,​String> mimeTypeToFormat
        A map from mimetypes to format constants
      • formatToMimeType

        public static Map<String,​String> formatToMimeType
        A map from format constants to mimetypes
      • extensionsToFormat

        public static Map<String,​String> extensionsToFormat
        A map from extensions to format constants
      • ignoreForSearch

        public static String[] ignoreForSearch
    • Constructor Detail

      • RequestUtils

        public RequestUtils()
    • Method Detail

      • handleURLRedirect

        public static void handleURLRedirect​(String redirectURL,
                                             boolean forward,
                                             HttpServletRequest req,
                                             HttpServletResponse res)
        Handles the redirect to a URL from the current location, the URL should be relative for a forward, otherwise it will be a redirect
        NOTE: You should perform no actions after call this method, you should simply pass control back to the handler
        Parameters:
        redirectURL - the URL to redirect to (relative or absolute)
        forward - if false, use redirect (this should be the default), if true use forward, note that we can only forward from your webapp back to your servlets and a check will be performed to see if this is the case, if it is not (anything with a "http", a non-matching prefix, and anything with a query string) will be switched to redirect automatically
        req - the current request
        res - the current response
        Throws:
        IllegalArgumentException - is the params are invalid
      • isRequestOutput

        public static boolean isRequestOutput​(HttpServletRequest req,
                                              EntityView view)
        Gets the correct info out of the request method and places it into the entity view and identifies if this is an output (read) or input (write) request
        Parameters:
        req - the request
        view - the entity view to update
        Returns:
        true if output request OR false if input request
        Throws:
        EntityException - if the request has problems
      • findAndHandleFormat

        public static String findAndHandleFormat​(HttpServletRequest req,
                                                 HttpServletResponse res,
                                                 String defaultFormat)
        This method will correctly extract the format constant from a request (extension first and then Accepts header) and then set it in the response as the correct return type, if none is found then the default will be used
        Parameters:
        req - the Servlet request
        res - the Servlet response
        defaultFormat - (OPTIONAL) if this is set then it will be the default format assigned when none can be found, otherwise the default format is Formats.HTML
        Returns:
        the extracted format (will never be null), e.g Formats.XML
      • makeSearchFromRequestParams

        public static Search makeSearchFromRequestParams​(Map<String,​Object> params)
        This looks at request parameters and returns anything it finds in the request parameters that can be put into the search, supports the page params and sorting params
        Parameters:
        params - the request params from a request (do not include headers)
        Returns:
        a search filter object
      • setResponseEncoding

        public static void setResponseEncoding​(String format,
                                               HttpServletResponse res)
        This will set the response mime type correctly based on the format constant, also sets the response encoding to UTF_8
        Parameters:
        format - the format constant, example Formats.XML
        res - the current outgoing response
      • getServletContext

        public static String getServletContext​(HttpServletRequest req)
        This finds the correct servlet path or returns the default one, will not return "" or null
        Parameters:
        req - the incoming request
        Returns:
        the servlet context path (/ + servletName)