Class UtilHttp

java.lang.Object
org.apache.ofbiz.base.util.UtilHttp

public final class UtilHttp extends Object
HttpUtil - Misc HTTP Utility Functions
  • Method Details

    • getCombinedMap

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

      public static Map<String,Object> getCombinedMap(HttpServletRequest request, Set<? extends 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 Map<String,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 Map<String,Object> getParameterMap(HttpServletRequest req, Predicate<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 Map<String,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 File getTmpUploadRepository(Delegator delegator)
      Parameters:
      delegator -
      Returns:
      tmpUploadRepository
    • getQueryStringOnlyParameterMap

      public static Map<String,Object> getQueryStringOnlyParameterMap(String queryString)
    • getUrlOnlyParameterMap

      public static Map<String,Object> getUrlOnlyParameterMap(HttpServletRequest request)
    • canonicalizeParameterMap

      public static Map<String,Object> canonicalizeParameterMap(Map<String,Object> paramMap)
    • canonicalizeParameter

      public static String canonicalizeParameter(String paramValue)
    • getJSONAttributeMap

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

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

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

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

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

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

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

      public static Map<String,Object> makeParamMapWithPrefix(HttpServletRequest request, String prefix, String suffix)
    • makeParamMapWithPrefix

      public static Map<String,Object> makeParamMapWithPrefix(HttpServletRequest request, Map<String,? extends Object> additionalFields, String prefix, String suffix)
    • makeParamMapWithPrefix

      public static Map<String,Object> makeParamMapWithPrefix(Map<String,? extends Object> context, Map<String,? extends Object> additionalFields, String prefix, String suffix)
    • makeParamListWithSuffix

      public static List<Object> makeParamListWithSuffix(HttpServletRequest request, String suffix, 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:
      NullPointerException - when request is null.
    • makeParamListWithSuffix

      public static List<Object> makeParamListWithSuffix(HttpServletRequest request, Map<String,?> additionalFields, String suffix, 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:
      NullPointerException - when request or additionalFields are null.
    • getApplicationName

      public static 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 String getServerRootUrl(HttpServletRequest request)
    • getFullRequestUrl

      public static String getFullRequestUrl(HttpServletRequest request)
    • getRequestMethod

      public static 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 Locale getLocale(HttpServletRequest request, HttpSession session, Object appDefaultLocale)
    • getLocale

      public static 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 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, String localeString)
    • setLocale

      public static void setLocale(HttpSession session, Locale locale)
    • setLocaleIfNone

      public static void setLocaleIfNone(HttpSession session, String localeString)
    • setTimeZone

      public static void setTimeZone(HttpServletRequest request, String tzId)
    • setTimeZone

      public static void setTimeZone(HttpSession session, TimeZone timeZone)
    • setTimeZoneIfNone

      public static void setTimeZoneIfNone(HttpSession session, String timeZoneString)
    • getTimeZone

      public static TimeZone getTimeZone(HttpServletRequest request)
    • getTimeZone

      public static TimeZone getTimeZone(HttpServletRequest request, HttpSession session, String appDefaultTimeZoneString)
    • getVisualTheme

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

      public static void setVisualTheme(HttpServletRequest request, VisualTheme visualTheme)
    • setVisualTheme

      public static void setVisualTheme(HttpSession session, VisualTheme visualTheme)
    • getCurrencyUom

      public static String getCurrencyUom(HttpSession session, 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 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, String currencyUom)
      Simple event to set the users per-session currency uom value
    • setCurrencyUomIfNone

      public static void setCurrencyUomIfNone(HttpSession session, String currencyUom)
    • urlEncodeArgs

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

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

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

      public static String getEncodedParameter(String parameter) throws UnsupportedEncodingException
      Encodes a query parameter
      Throws:
      UnsupportedEncodingException
    • getRequestUriFromTarget

      public static String getRequestUriFromTarget(String target)
    • getQueryStringFromTarget

      public static String getQueryStringFromTarget(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 String removeQueryStringFromTarget(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 String getWebappMountPointFromTarget(String target)
    • encodeAmpersands

      public static String encodeAmpersands(String htmlString)
    • encodeBlanks

      public static String encodeBlanks(String htmlString)
    • setResponseBrowserProxyNoCache

      public static String setResponseBrowserProxyNoCache(HttpServletRequest request, HttpServletResponse response)
    • setResponseBrowserProxyNoCache

      public static void setResponseBrowserProxyNoCache(HttpServletResponse response)
    • setResponseBrowserDefaultSecurityHeaders

      public static void setResponseBrowserDefaultSecurityHeaders(HttpServletResponse resp, ConfigXMLReader.ViewMap viewMap)
    • getContentTypeByFileName

      public static String getContentTypeByFileName(String fileName)
    • streamContentToBrowser

      public static void streamContentToBrowser(HttpServletResponse response, byte[] bytes, String contentType, String fileName) throws 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:
      IOException
    • streamContentToBrowser

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

      public static void streamContentToBrowser(HttpServletResponse response, InputStream in, int length, String contentType, String fileName) throws 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:
      IOException
    • streamContentToBrowser

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

      public static void streamContent(OutputStream out, InputStream in, int length) throws 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:
      IOException
    • stripViewParamsFromQueryString

      public static String stripViewParamsFromQueryString(String queryString)
    • stripViewParamsFromQueryString

      public static String stripViewParamsFromQueryString(String queryString, String paginatorNumber)
    • stripNamedParamsFromQueryString

      public static String stripNamedParamsFromQueryString(String queryString, Collection<String> paramNames)
    • parseMultiFormData

      public static Collection<Map<String,Object>> parseMultiFormData(Map<String,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> Map<String,V> removeMultiFormParameters(Map<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 String makeCompositeParam(String prefix, 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 Object makeParamValueFromComposite(HttpServletRequest request, 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 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(Map<String,?> requestMap)
      Returns the number or rows submitted by a multi form.
    • stashParameterMap

      public static String stashParameterMap(HttpServletRequest request)
    • restoreStashedParameterMap

      public static void restoreStashedParameterMap(HttpServletRequest request, String paramMapId)
    • getNextUniqueId

      public static 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, String filename)
    • getAllowAllHttpClient

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

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

      public static String getMultiRowDelimiter()
    • getRowSubmitPrefix

      public static String getRowSubmitPrefix()
    • extractUrls

      public static List<String> extractUrls(String input)