Package org.apache.ofbiz.base.util
Class UtilHttp
java.lang.Object
org.apache.ofbiz.base.util.UtilHttp
HttpUtil - Misc HTTP Utility Functions
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
canonicalizeParameter
(String paramValue) canonicalizeParameterMap
(Map<String, Object> paramMap) static String
encodeAmpersands
(String htmlString) static String
encodeBlanks
(String htmlString) extractUrls
(String input) static org.apache.http.impl.client.CloseableHttpClient
static org.apache.http.impl.client.CloseableHttpClient
getAllowAllHttpClient
(String jksStoreFileName, String jksStorePassword) static String
getApplicationName
(HttpServletRequest request) Given a request, returns the application name or "root" if deployed on rootgetAttributeMap
(HttpServletRequest request) Create a map from a HttpRequest (attributes) objectgetAttributeMap
(HttpServletRequest request, Set<? extends String> namesToSkip) Create a map from a HttpRequest (attributes) objectgetCombinedMap
(HttpServletRequest request) Create a combined map from servlet context, session, attributes and parametersgetCombinedMap
(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 attributesstatic String
getContentTypeByFileName
(String fileName) static String
getCurrencyUom
(HttpServletRequest request) Get the currency string from the session.static String
getCurrencyUom
(HttpSession session, String appDefaultCurrencyUom) Get the currency string from the session.static String
getEncodedParameter
(String parameter) Encodes a query parameterstatic String
getFullRequestUrl
(HttpServletRequest request) getJSONAttributeMap
(HttpServletRequest request) Create a map from a HttpRequest (attributes) object used in JSON requestsstatic Locale
getLocale
(HttpServletRequest request) Get the Locale object from a session variable; if not found use the browser's defaultstatic Locale
getLocale
(HttpServletRequest request, HttpSession session, Object appDefaultLocale) static Locale
getLocale
(HttpSession session) Get the Locale object from a session variable; if not found use the system's default.static long
getMaxUploadSize
(Delegator delegator) static int
getMultiFormRowCount
(Map<String, ?> requestMap) Returns the number or rows submitted by a multi form.static int
getMultiFormRowCount
(HttpServletRequest request) Returns the number or rows submitted by a multi form.static String
static String
getNextUniqueId
(HttpServletRequest request) Returns a unique Id for the current requestgetParameterMap
(HttpServletRequest request) Creates a canonicalized parameter map from a HTTP request.getParameterMap
(HttpServletRequest req, Predicate<String> pred) Creates a canonicalized parameter map from a HTTP request.static String
getQueryStringFromTarget
(String target) Returns the query string contained in a request target - basically everything after and including the ? character.getQueryStringOnlyParameterMap
(String queryString) static String
getRequestMethod
(HttpServletRequest request) Resolve the method send with the request.static String
getRequestUriFromTarget
(String target) static String
static String
getServerRootUrl
(HttpServletRequest request) getServletContextMap
(HttpServletRequest request) Create a map from a ServletContext objectgetServletContextMap
(HttpServletRequest request, Set<? extends String> namesToSkip) Create a map from a ServletContext objectstatic String
getSessionId
(HttpServletRequest request) Obtains the session ID from the request, or "unknown" if no session pressent.getSessionMap
(HttpServletRequest request) Create a map from a HttpSession objectgetSessionMap
(HttpServletRequest request, Set<? extends String> namesToSkip) Create a map from a HttpSession objectstatic int
getSizeThreshold
(Delegator delegator) static TimeZone
getTimeZone
(HttpServletRequest request) static TimeZone
getTimeZone
(HttpServletRequest request, HttpSession session, String appDefaultTimeZoneString) static File
getTmpUploadRepository
(Delegator delegator) getUrlOnlyParameterMap
(HttpServletRequest request) static VisualTheme
getVisualTheme
(HttpServletRequest request) Return the VisualTheme object from the user sessionstatic String
getWebappMountPointFromTarget
(String target) static boolean
isJavaScriptEnabled
(HttpServletRequest request) Returns true if the user has JavaScript enabled.static String
makeCompositeParam
(String prefix, String suffix) Utility to make a composite parameter from the given prefix and suffix.makeParamListWithSuffix
(HttpServletRequest request, String suffix, String prefix) Constructs a list of parameter values whose keys are matching a given prefix and suffix.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.makeParamMapWithPrefix
(Map<String, ? extends Object> context, Map<String, ? extends Object> additionalFields, String prefix, String suffix) makeParamMapWithPrefix
(HttpServletRequest request, String prefix, String suffix) makeParamMapWithPrefix
(HttpServletRequest request, Map<String, ? extends Object> additionalFields, String prefix, String suffix) static Object
makeParamValueFromComposite
(HttpServletRequest request, String prefix) Assembles a composite object from a set of parameters identified by a common prefix.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.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).static String
removeQueryStringFromTarget
(String target) Removes the query string from a request target - basically everything after and including the ? character.static void
restoreStashedParameterMap
(HttpServletRequest request, String paramMapId) static void
setContentDisposition
(HttpServletResponse response, String filename) static void
setCurrencyUom
(HttpSession session, String currencyUom) Simple event to set the users per-session currency uom valuestatic void
setCurrencyUomIfNone
(HttpSession session, String currencyUom) static void
setInitialRequestInfo
(HttpServletRequest request) static void
setLocale
(HttpServletRequest request, String localeString) static void
setLocale
(HttpSession session, Locale locale) static void
setLocaleIfNone
(HttpSession session, String localeString) static void
static String
setResponseBrowserProxyNoCache
(HttpServletRequest request, HttpServletResponse response) static void
static void
setTimeZone
(HttpServletRequest request, String tzId) static void
setTimeZone
(HttpSession session, TimeZone timeZone) static void
setTimeZoneIfNone
(HttpSession session, String timeZoneString) static void
setVisualTheme
(HttpServletRequest request, VisualTheme visualTheme) static void
setVisualTheme
(HttpSession session, VisualTheme visualTheme) static String
stashParameterMap
(HttpServletRequest request) static void
streamContent
(OutputStream out, InputStream in, int length) Stream binary content from InputStream to OutputStream This method does not close the streams passedstatic void
streamContentToBrowser
(HttpServletResponse response, byte[] bytes, String contentType) static void
streamContentToBrowser
(HttpServletResponse response, byte[] bytes, String contentType, String fileName) Stream an array of bytes to the browser This method will close the ServletOutputStream when finishedstatic void
streamContentToBrowser
(HttpServletResponse response, InputStream in, int length, String contentType) static void
streamContentToBrowser
(HttpServletResponse response, InputStream in, int length, String contentType, String fileName) Streams content from InputStream to the ServletOutputStream This method will close the ServletOutputStream when finished This method does not close the InputSteam passedstatic String
stripNamedParamsFromQueryString
(String queryString, Collection<String> paramNames) static String
stripViewParamsFromQueryString
(String queryString) static String
stripViewParamsFromQueryString
(String queryString, String paginatorNumber) static String
urlEncodeArgs
(Map<String, ? extends Object> args) URL Encodes a Map of argumentsstatic String
urlEncodeArgs
(Map<String, ? extends Object> args, boolean useExpandedEntites) URL Encodes a Map of argumentsstatic String
urlEncodeArgs
(Map<String, ? extends Object> args, boolean useExpandedEntites, boolean preserveEmpty) URL Encodes a Map of arguments
-
Method Details
-
getCombinedMap
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
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
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 parameterspred
- the predicate filtering the parameter names- Returns:
- a canonicalized parameter map.
-
getMultiPartParameterMap
-
getMaxUploadSize
- Parameters:
delegator
-- Returns:
- maxUploadSize
-
getSizeThreshold
- Parameters:
delegator
-- Returns:
- sizeThreshold
-
getTmpUploadRepository
- Parameters:
delegator
-- Returns:
- tmpUploadRepository
-
getQueryStringOnlyParameterMap
-
getUrlOnlyParameterMap
-
canonicalizeParameterMap
-
canonicalizeParameter
-
getJSONAttributeMap
Create a map from a HttpRequest (attributes) object used in JSON requests- Returns:
- The resulting Map
-
getAttributeMap
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
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
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
-
makeParamMapWithPrefix
-
makeParamMapWithPrefix
-
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 parameterssuffix
- the suffix that must be matched which can benull
prefix
- the prefix that must be matched which can benull
- Returns:
- the list of parameter values whose keys are matching
prefix
andsuffix
. - Throws:
NullPointerException
- whenrequest
isnull
.
-
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 parametersadditionalFields
- the additional parameterssuffix
- the suffix that must be matched which can benull
prefix
- the prefix that must be matched which can benull
- Returns:
- the list of parameter values whose keys are matching
prefix
andsuffix
. - Throws:
NullPointerException
- whenrequest
oradditionalFields
arenull
.
-
getApplicationName
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
-
getServerRootUrl
-
getFullRequestUrl
-
getRequestMethod
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
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
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
-
setLocale
-
setLocaleIfNone
-
setTimeZone
-
setTimeZone
-
setTimeZoneIfNone
-
getTimeZone
-
getTimeZone
public static TimeZone getTimeZone(HttpServletRequest request, HttpSession session, String appDefaultTimeZoneString) -
getVisualTheme
Return the VisualTheme object from the user session- Parameters:
request
-- Returns:
- VisualTheme
-
setVisualTheme
-
setVisualTheme
-
getCurrencyUom
Get the currency string from the session.- Parameters:
session
- HttpSession object to use for lookup- Returns:
- String The ISO currency code
-
getCurrencyUom
Get the currency string from the session.- Parameters:
request
- HttpServletRequest object to use for lookup- Returns:
- String The ISO currency code
-
setCurrencyUom
Simple event to set the users per-session currency uom value -
setCurrencyUomIfNone
-
urlEncodeArgs
URL Encodes a Map of arguments -
urlEncodeArgs
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
Encodes a query parameter- Throws:
UnsupportedEncodingException
-
getRequestUriFromTarget
-
getQueryStringFromTarget
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
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
-
encodeAmpersands
-
encodeBlanks
-
setResponseBrowserProxyNoCache
public static String setResponseBrowserProxyNoCache(HttpServletRequest request, HttpServletResponse response) -
setResponseBrowserProxyNoCache
-
setResponseBrowserDefaultSecurityHeaders
public static void setResponseBrowserDefaultSecurityHeaders(HttpServletResponse resp, ConfigXMLReader.ViewMap viewMap) -
getContentTypeByFileName
-
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 frombytes
- Byte array of content to streamcontentType
- The content type to pass to the browserfileName
- 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 fromin
- InputStream of the actual contentlength
- Size (in bytes) of the contentcontentType
- 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
Stream binary content from InputStream to OutputStream This method does not close the streams passed- Parameters:
out
- OutputStream content should go toin
- InputStream of the actual contentlength
- Size (in bytes) of the content- Throws:
IOException
-
stripViewParamsFromQueryString
-
stripViewParamsFromQueryString
-
stripNamedParamsFromQueryString
public static String stripNamedParamsFromQueryString(String queryString, Collection<String> paramNames) -
parseMultiFormData
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
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
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
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>
meetingDate_c_date
,meetingDate_c_hour
,meetingDate_c_minutes
. Additionally, there will be a field namedmeetingDate_c_compositeType
with a value of "Timestamp". where "_c_" is theCOMPOSITE_DELIMITER
. These parameters will then be re-composed into a Timestamp object from the composite fields.- Parameters:
request
- the HTTP request containing the parametersprefix
- 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
Obtains the session ID from the request, or "unknown" if no session pressent. -
isJavaScriptEnabled
Returns true if the user has JavaScript enabled.- Parameters:
request
-- Returns:
- whether javascript is enabled
-
getMultiFormRowCount
Returns the number or rows submitted by a multi form. -
getMultiFormRowCount
Returns the number or rows submitted by a multi form. -
stashParameterMap
-
restoreStashedParameterMap
-
getNextUniqueId
Returns a unique Id for the current request- Parameters:
request
- An HttpServletRequest to get the name info from- Returns:
- String
-
setContentDisposition
-
getAllowAllHttpClient
public static org.apache.http.impl.client.CloseableHttpClient getAllowAllHttpClient() -
getAllowAllHttpClient
-
getMultiRowDelimiter
-
getRowSubmitPrefix
-
extractUrls
-