Class UtilHttp


  • public final class UtilHttp
    extends java.lang.Object
    HttpUtil - Misc HTTP Utility Functions
    • Method Detail

      • getCombinedMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getCombinedMap​(HttpServletRequest request)
        Create a combined map from servlet context, session, attributes and parameters
        Returns:
        The resulting Map
      • getCombinedMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getCombinedMap​(HttpServletRequest request,
                                                                                            java.util.Set<? extends java.lang.String> namesToSkip)
        Create a combined map from servlet context, session, attributes and parameters -- this method will only use the skip names for session and servlet context attributes
        Returns:
        The resulting Map
      • getParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getParameterMap​(HttpServletRequest request)
        Creates a canonicalized parameter map from a HTTP request.

        If parameters are empty, the multi-part parameter map will be used.

        Parameters:
        request - the HTTP request containing the parameters
        Returns:
        a canonicalized parameter map.
      • getParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getParameterMap​(HttpServletRequest req,
                                                                                             java.util.function.Predicate<java.lang.String> pred)
        Creates a canonicalized parameter map from a HTTP request.

        If parameters are empty, the multi-part parameter map will be used.

        Parameters:
        req - the HTTP request containing the parameters
        pred - the predicate filtering the parameter names
        Returns:
        a canonicalized parameter map.
      • getMultiPartParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getMultiPartParameterMap​(HttpServletRequest request)
      • getMaxUploadSize

        public static long getMaxUploadSize​(Delegator delegator)
        Parameters:
        delegator -
        Returns:
        maxUploadSize
      • getSizeThreshold

        public static int getSizeThreshold​(Delegator delegator)
        Parameters:
        delegator -
        Returns:
        sizeThreshold
      • getTmpUploadRepository

        public static java.io.File getTmpUploadRepository​(Delegator delegator)
        Parameters:
        delegator -
        Returns:
        tmpUploadRepository
      • getQueryStringOnlyParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getQueryStringOnlyParameterMap​(java.lang.String queryString)
      • getUrlOnlyParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getUrlOnlyParameterMap​(HttpServletRequest request)
      • canonicalizeParameterMap

        public static java.util.Map<java.lang.String,​java.lang.Object> canonicalizeParameterMap​(java.util.Map<java.lang.String,​java.lang.Object> paramMap)
      • canonicalizeParameter

        public static java.lang.String canonicalizeParameter​(java.lang.String paramValue)
      • getJSONAttributeMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getJSONAttributeMap​(HttpServletRequest request)
        Create a map from a HttpRequest (attributes) object used in JSON requests
        Returns:
        The resulting Map
      • getAttributeMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getAttributeMap​(HttpServletRequest request)
        Create a map from a HttpRequest (attributes) object
        Returns:
        The resulting Map
      • getAttributeMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getAttributeMap​(HttpServletRequest request,
                                                                                             java.util.Set<? extends java.lang.String> namesToSkip)
        Create a map from a HttpRequest (attributes) object
        Returns:
        The resulting Map
      • getSessionMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getSessionMap​(HttpServletRequest request)
        Create a map from a HttpSession object
        Returns:
        The resulting Map
      • getSessionMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getSessionMap​(HttpServletRequest request,
                                                                                           java.util.Set<? extends java.lang.String> namesToSkip)
        Create a map from a HttpSession object
        Returns:
        The resulting Map
      • getServletContextMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getServletContextMap​(HttpServletRequest request)
        Create a map from a ServletContext object
        Returns:
        The resulting Map
      • getServletContextMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getServletContextMap​(HttpServletRequest request,
                                                                                                  java.util.Set<? extends java.lang.String> namesToSkip)
        Create a map from a ServletContext object
        Returns:
        The resulting Map
      • makeParamMapWithPrefix

        public static java.util.Map<java.lang.String,​java.lang.Object> makeParamMapWithPrefix​(HttpServletRequest request,
                                                                                                    java.lang.String prefix,
                                                                                                    java.lang.String suffix)
      • makeParamMapWithPrefix

        public static java.util.Map<java.lang.String,​java.lang.Object> makeParamMapWithPrefix​(HttpServletRequest request,
                                                                                                    java.util.Map<java.lang.String,​? extends java.lang.Object> additionalFields,
                                                                                                    java.lang.String prefix,
                                                                                                    java.lang.String suffix)
      • makeParamMapWithPrefix

        public static java.util.Map<java.lang.String,​java.lang.Object> makeParamMapWithPrefix​(java.util.Map<java.lang.String,​? extends java.lang.Object> context,
                                                                                                    java.util.Map<java.lang.String,​? extends java.lang.Object> additionalFields,
                                                                                                    java.lang.String prefix,
                                                                                                    java.lang.String suffix)
      • makeParamListWithSuffix

        public static java.util.List<java.lang.Object> makeParamListWithSuffix​(HttpServletRequest request,
                                                                               java.lang.String suffix,
                                                                               java.lang.String prefix)
        Constructs a list of parameter values whose keys are matching a given prefix and suffix.
        Parameters:
        request - the HTTP request containing the parameters
        suffix - the suffix that must be matched which can be null
        prefix - the prefix that must be matched which can be null
        Returns:
        the list of parameter values whose keys are matching prefix and suffix.
        Throws:
        java.lang.NullPointerException - when request is null.
      • makeParamListWithSuffix

        public static java.util.List<java.lang.Object> makeParamListWithSuffix​(HttpServletRequest request,
                                                                               java.util.Map<java.lang.String,​?> additionalFields,
                                                                               java.lang.String suffix,
                                                                               java.lang.String prefix)
        Constructs a list of parameter values whose keys are matching a given prefix and suffix.
        Parameters:
        request - the HTTP request containing the parameters
        additionalFields - the additional parameters
        suffix - the suffix that must be matched which can be null
        prefix - the prefix that must be matched which can be null
        Returns:
        the list of parameter values whose keys are matching prefix and suffix.
        Throws:
        java.lang.NullPointerException - when request or additionalFields are null.
      • getApplicationName

        public static java.lang.String getApplicationName​(HttpServletRequest request)
        Given a request, returns the application name or "root" if deployed on root
        Parameters:
        request - An HttpServletRequest to get the name info from
        Returns:
        String
      • setInitialRequestInfo

        public static void setInitialRequestInfo​(HttpServletRequest request)
      • getServerRootUrl

        public static java.lang.String getServerRootUrl​(HttpServletRequest request)
      • getFullRequestUrl

        public static java.lang.String getFullRequestUrl​(HttpServletRequest request)
      • getRequestMethod

        public static java.lang.String getRequestMethod​(HttpServletRequest request)
        Resolve the method send with the request. check first the parameter _method before return the request method
        Parameters:
        request -
        Returns:
        method
      • getLocale

        public static java.util.Locale getLocale​(HttpServletRequest request)
        Get the Locale object from a session variable; if not found use the browser's default
        Parameters:
        request - HttpServletRequest object to use for lookup
        Returns:
        Locale The current Locale to use
      • getLocale

        public static java.util.Locale getLocale​(HttpSession session)
        Get the Locale object from a session variable; if not found use the system's default. NOTE: This method is not recommended because it ignores the Locale from the browser not having the request object.
        Parameters:
        session - HttpSession object to use for lookup
        Returns:
        Locale The current Locale to use
      • setLocale

        public static void setLocale​(HttpServletRequest request,
                                     java.lang.String localeString)
      • setLocale

        public static void setLocale​(HttpSession session,
                                     java.util.Locale locale)
      • setLocaleIfNone

        public static void setLocaleIfNone​(HttpSession session,
                                           java.lang.String localeString)
      • setTimeZone

        public static void setTimeZone​(HttpServletRequest request,
                                       java.lang.String tzId)
      • setTimeZone

        public static void setTimeZone​(HttpSession session,
                                       java.util.TimeZone timeZone)
      • setTimeZoneIfNone

        public static void setTimeZoneIfNone​(HttpSession session,
                                             java.lang.String timeZoneString)
      • getTimeZone

        public static java.util.TimeZone getTimeZone​(HttpServletRequest request)
      • getTimeZone

        public static java.util.TimeZone getTimeZone​(HttpServletRequest request,
                                                     HttpSession session,
                                                     java.lang.String appDefaultTimeZoneString)
      • getVisualTheme

        public static VisualTheme getVisualTheme​(HttpServletRequest request)
        Return the VisualTheme object from the user session
        Parameters:
        request -
        Returns:
        VisualTheme
      • getCurrencyUom

        public static java.lang.String getCurrencyUom​(HttpSession session,
                                                      java.lang.String appDefaultCurrencyUom)
        Get the currency string from the session.
        Parameters:
        session - HttpSession object to use for lookup
        Returns:
        String The ISO currency code
      • getCurrencyUom

        public static java.lang.String getCurrencyUom​(HttpServletRequest request)
        Get the currency string from the session.
        Parameters:
        request - HttpServletRequest object to use for lookup
        Returns:
        String The ISO currency code
      • setCurrencyUom

        public static void setCurrencyUom​(HttpSession session,
                                          java.lang.String currencyUom)
        Simple event to set the users per-session currency uom value
      • setCurrencyUomIfNone

        public static void setCurrencyUomIfNone​(HttpSession session,
                                                java.lang.String currencyUom)
      • urlEncodeArgs

        public static java.lang.String urlEncodeArgs​(java.util.Map<java.lang.String,​? extends java.lang.Object> args)
        URL Encodes a Map of arguments
      • urlEncodeArgs

        public static java.lang.String urlEncodeArgs​(java.util.Map<java.lang.String,​? extends java.lang.Object> args,
                                                     boolean useExpandedEntites)
        URL Encodes a Map of arguments
      • urlEncodeArgs

        public static java.lang.String urlEncodeArgs​(java.util.Map<java.lang.String,​? extends java.lang.Object> args,
                                                     boolean useExpandedEntites,
                                                     boolean preserveEmpty)
        URL Encodes a Map of arguments
      • getEncodedParameter

        public static java.lang.String getEncodedParameter​(java.lang.String parameter)
                                                    throws java.io.UnsupportedEncodingException
        Encodes a query parameter
        Throws:
        java.io.UnsupportedEncodingException
      • getRequestUriFromTarget

        public static java.lang.String getRequestUriFromTarget​(java.lang.String target)
      • getQueryStringFromTarget

        public static java.lang.String getQueryStringFromTarget​(java.lang.String target)
        Returns the query string contained in a request target - basically everything after and including the ? character.
        Parameters:
        target - The request target
        Returns:
        The query string
      • removeQueryStringFromTarget

        public static java.lang.String removeQueryStringFromTarget​(java.lang.String target)
        Removes the query string from a request target - basically everything after and including the ? character.
        Parameters:
        target - The request target
        Returns:
        The request target string
      • getWebappMountPointFromTarget

        public static java.lang.String getWebappMountPointFromTarget​(java.lang.String target)
      • encodeAmpersands

        public static java.lang.String encodeAmpersands​(java.lang.String htmlString)
      • encodeBlanks

        public static java.lang.String encodeBlanks​(java.lang.String htmlString)
      • setResponseBrowserProxyNoCache

        public static void setResponseBrowserProxyNoCache​(HttpServletResponse response)
      • getContentTypeByFileName

        public static java.lang.String getContentTypeByFileName​(java.lang.String fileName)
      • streamContentToBrowser

        public static void streamContentToBrowser​(HttpServletResponse response,
                                                  byte[] bytes,
                                                  java.lang.String contentType,
                                                  java.lang.String fileName)
                                           throws java.io.IOException
        Stream an array of bytes to the browser This method will close the ServletOutputStream when finished
        Parameters:
        response - HttpServletResponse object to get OutputStream from
        bytes - Byte array of content to stream
        contentType - The content type to pass to the browser
        fileName - the fileName to tell the browser we are downloading
        Throws:
        java.io.IOException
      • streamContentToBrowser

        public static void streamContentToBrowser​(HttpServletResponse response,
                                                  byte[] bytes,
                                                  java.lang.String contentType)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • streamContentToBrowser

        public static void streamContentToBrowser​(HttpServletResponse response,
                                                  java.io.InputStream in,
                                                  int length,
                                                  java.lang.String contentType,
                                                  java.lang.String fileName)
                                           throws java.io.IOException
        Streams content from InputStream to the ServletOutputStream This method will close the ServletOutputStream when finished This method does not close the InputSteam passed
        Parameters:
        response - HttpServletResponse object to get OutputStream from
        in - InputStream of the actual content
        length - Size (in bytes) of the content
        contentType - The content type to pass to the browser
        Throws:
        java.io.IOException
      • streamContentToBrowser

        public static void streamContentToBrowser​(HttpServletResponse response,
                                                  java.io.InputStream in,
                                                  int length,
                                                  java.lang.String contentType)
                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • streamContent

        public static void streamContent​(java.io.OutputStream out,
                                         java.io.InputStream in,
                                         int length)
                                  throws java.io.IOException
        Stream binary content from InputStream to OutputStream This method does not close the streams passed
        Parameters:
        out - OutputStream content should go to
        in - InputStream of the actual content
        length - Size (in bytes) of the content
        Throws:
        java.io.IOException
      • stripViewParamsFromQueryString

        public static java.lang.String stripViewParamsFromQueryString​(java.lang.String queryString)
      • stripViewParamsFromQueryString

        public static java.lang.String stripViewParamsFromQueryString​(java.lang.String queryString,
                                                                      java.lang.String paginatorNumber)
      • stripNamedParamsFromQueryString

        public static java.lang.String stripNamedParamsFromQueryString​(java.lang.String queryString,
                                                                       java.util.Collection<java.lang.String> paramNames)
      • parseMultiFormData

        public static java.util.Collection<java.util.Map<java.lang.String,​java.lang.Object>> parseMultiFormData​(java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Given multi form data with the ${param}_o_N notation, creates a Collection of Maps for the submitted rows. Each Map contains the key/value pairs of a particular row. The keys will be stripped of the _o_N suffix. There is an additionaly key "row" for each Map that holds the index of the row.
      • removeMultiFormParameters

        public static <V> java.util.Map<java.lang.String,​V> removeMultiFormParameters​(java.util.Map<java.lang.String,​V> parameters)
        Returns a new map containing all the parameters from the input map except for the multi form parameters (usually named according to the ${param}_o_N notation).
      • makeCompositeParam

        public static java.lang.String makeCompositeParam​(java.lang.String prefix,
                                                          java.lang.String suffix)
        Utility to make a composite parameter from the given prefix and suffix. The prefix should be a regular parameter name such as meetingDate. The suffix is the composite field, such as the hour of the meeting. The result would be meetingDate_${COMPOSITE_DELIMITER}_hour.
        Parameters:
        prefix -
        suffix -
        Returns:
        the composite parameter
      • makeParamValueFromComposite

        public static java.lang.Object makeParamValueFromComposite​(HttpServletRequest request,
                                                                   java.lang.String prefix)
        Assembles a composite object from a set of parameters identified by a common prefix.

        For example, consider the following form widget field:

         
         <field name="meetingDate">
             <date-time type="timestamp" input-method="time-dropdown">
         </field>
         
         
        The HTML result is three input boxes to input the date, hour and minutes separately. The parameter names are named meetingDate_c_date, meetingDate_c_hour, meetingDate_c_minutes. Additionally, there will be a field named meetingDate_c_compositeType with a value of "Timestamp". where "_c_" is the COMPOSITE_DELIMITER. These parameters will then be re-composed into a Timestamp object from the composite fields.
        Parameters:
        request - the HTTP request containing the parameters
        prefix - the string identifying the set of parameters that must be composed
        Returns:
        a composite object from data or null if not supported or a parsing error occurred.
      • getSessionId

        public static java.lang.String getSessionId​(HttpServletRequest request)
        Obtains the session ID from the request, or "unknown" if no session pressent.
      • isJavaScriptEnabled

        public static boolean isJavaScriptEnabled​(HttpServletRequest request)
        Returns true if the user has JavaScript enabled.
        Parameters:
        request -
        Returns:
        whether javascript is enabled
      • getMultiFormRowCount

        public static int getMultiFormRowCount​(HttpServletRequest request)
        Returns the number or rows submitted by a multi form.
      • getMultiFormRowCount

        public static int getMultiFormRowCount​(java.util.Map<java.lang.String,​?> requestMap)
        Returns the number or rows submitted by a multi form.
      • stashParameterMap

        public static java.lang.String stashParameterMap​(HttpServletRequest request)
      • restoreStashedParameterMap

        public static void restoreStashedParameterMap​(HttpServletRequest request,
                                                      java.lang.String paramMapId)
      • getNextUniqueId

        public static java.lang.String getNextUniqueId​(HttpServletRequest request)
        Returns a unique Id for the current request
        Parameters:
        request - An HttpServletRequest to get the name info from
        Returns:
        String
      • setContentDisposition

        public static void setContentDisposition​(HttpServletResponse response,
                                                 java.lang.String filename)
      • getAllowAllHttpClient

        public static org.apache.http.impl.client.CloseableHttpClient getAllowAllHttpClient()
      • getAllowAllHttpClient

        public static org.apache.http.impl.client.CloseableHttpClient getAllowAllHttpClient​(java.lang.String jksStoreFileName,
                                                                                            java.lang.String jksStorePassword)
      • getMultiRowDelimiter

        public static java.lang.String getMultiRowDelimiter()
      • getRowSubmitPrefix

        public static java.lang.String getRowSubmitPrefix()
      • extractUrls

        public static java.util.List<java.lang.String> extractUrls​(java.lang.String input)