public class ServletUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CONTENT_DISPOSITION |
static java.lang.String |
HEADER_ACCEPT_ENCODING |
static java.lang.String |
HEADER_AUTHORIZATION |
static java.lang.String |
HEADER_CONTENT_TYPE |
static java.lang.String |
TYPE_MULTIPART_FORM_DATA |
static java.lang.String |
VALID_SCHEME_CHARS
Valid characters in a scheme, as specified by RFC 1738.
|
static java.lang.String |
WWW_AUTHENTICATE |
Constructor and Description |
---|
ServletUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
attribute(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
Returns non-
null attribute value. |
static java.lang.Object |
attribute(javax.servlet.jsp.PageContext pageContext,
java.lang.String name)
Returns non-
null attribute value. |
static void |
copyParamsToAttributes(javax.servlet.http.HttpServletRequest servletRequest,
boolean treatEmptyParamsAsNull,
boolean ignoreEmptyRequestParams)
Copies all request parameters to attributes.
|
static javax.servlet.http.Cookie[] |
getAllCookies(javax.servlet.http.HttpServletRequest request,
java.lang.String cookieName)
Returns all cookies from client that matches provided name.
|
static java.lang.String |
getContextPath(javax.servlet.http.HttpServletRequest request)
Returns correct context path, as by Servlet definition.
|
static java.lang.String |
getContextPath(javax.servlet.jsp.PageContext pageContext) |
static java.lang.String |
getContextPath(javax.servlet.ServletContext servletContext)
Returns correct context path, as by Servlet definition.
|
static javax.servlet.http.Cookie |
getCookie(javax.servlet.http.HttpServletRequest request,
java.lang.String cookieName)
Finds and returns cookie from client by its name.
|
static java.lang.Object |
getRequestParameter(javax.servlet.ServletRequest request,
java.lang.String name)
Returns HTTP request parameter as String or String[].
|
static java.lang.Throwable |
getServletError(javax.servlet.ServletRequest request)
Returns servlet error.
|
static boolean |
isAbsoluteUrl(java.lang.String url)
Returns
true if current URL is absolute, false otherwise. |
boolean |
isGetParameter(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
Checks if some parameter is in GET parameters.
|
static boolean |
isGzipSupported(javax.servlet.http.HttpServletRequest request)
Returns
true if client supports gzip encoding. |
static boolean |
isJsonRequest(javax.servlet.http.HttpServletRequest servletRequest)
Returns
true if request has JSON content type. |
static boolean |
isMultipartRequest(javax.servlet.http.HttpServletRequest request)
Returns
true if a request is multi-part request. |
static boolean |
isServletsVersion2_5()
Returns
true if current servlets version is 2.5 or higher. |
static void |
prepareDownload(javax.servlet.http.HttpServletResponse response,
java.io.File file)
Prepares response for file download.
|
static void |
prepareDownload(javax.servlet.http.HttpServletResponse response,
java.io.File file,
java.lang.String mimeType)
Prepares response for file download with provided mime type.
|
static java.lang.String[] |
prepareParameters(java.lang.String[] paramValues,
boolean treatEmptyParamsAsNull,
boolean ignoreEmptyRequestParams)
Prepares parameters for further processing.
|
static void |
prepareResponse(javax.servlet.http.HttpServletResponse response,
java.lang.String fileName,
java.lang.String mimeType,
int fileSize)
Prepares response for various provided data.
|
static void |
preventCaching(javax.servlet.http.HttpServletResponse response)
Prevents HTTP cache.
|
static java.lang.String |
readRequestBodyFromReader(javax.servlet.http.HttpServletRequest request)
Reads HTTP request body using the request reader.
|
static java.lang.String |
readRequestBodyFromStream(javax.servlet.http.HttpServletRequest request)
Reads HTTP request body using the request stream.
|
static void |
removeScopeAttribute(java.lang.String name,
java.lang.String scope,
javax.servlet.jsp.PageContext pageContext)
Removes scope attribute.
|
static void |
requireAuthentication(javax.servlet.http.HttpServletResponse resp,
java.lang.String realm)
Sends correct headers to require basic authentication for the given realm.
|
static java.lang.String |
resolveAuthBearerToken(javax.servlet.http.HttpServletRequest request)
Returns Bearer token.
|
static java.lang.String |
resolveAuthPassword(javax.servlet.http.HttpServletRequest request)
Decodes the "Authorization" header and retrieves the
password from it.
|
static java.lang.String |
resolveAuthUsername(javax.servlet.http.HttpServletRequest request)
Decodes the "Authorization" header and retrieves the
user's name from it.
|
static java.lang.String |
resolveUrl(java.lang.String url,
javax.servlet.http.HttpServletRequest request) |
static java.lang.String |
resolveUrl(java.lang.String url,
java.lang.String context) |
static void |
setScopeAttribute(java.lang.String name,
java.lang.Object value,
java.lang.String scope,
javax.servlet.jsp.PageContext pageContext)
Sets scope attribute.
|
static void |
setServletError(javax.servlet.ServletRequest request,
java.lang.Throwable throwable)
Sets servlet error.
|
static void |
storeContextPath(javax.servlet.jsp.PageContext pageContext,
java.lang.String contextPathVariableName)
Stores context path in server context and request scope.
|
static void |
storeContextPath(javax.servlet.ServletContext servletContext,
java.lang.String contextPathVariableName)
Stores context path in page context and request scope.
|
static java.lang.String |
stripSessionId(java.lang.String url)
Strips a servlet session ID from
url . |
static java.lang.Object |
value(javax.servlet.http.HttpServletRequest request,
java.lang.String name)
Returns value of property/attribute.
|
static java.lang.Object |
value(javax.servlet.jsp.PageContext pageContext,
java.lang.String name)
Returns value of property/attribute.
|
public static final java.lang.String HEADER_CONTENT_TYPE
public static final java.lang.String TYPE_MULTIPART_FORM_DATA
public static final java.lang.String HEADER_AUTHORIZATION
public static final java.lang.String HEADER_ACCEPT_ENCODING
public static final java.lang.String CONTENT_DISPOSITION
public static final java.lang.String WWW_AUTHENTICATE
public static final java.lang.String VALID_SCHEME_CHARS
public static boolean isMultipartRequest(javax.servlet.http.HttpServletRequest request)
true
if a request is multi-part request.public static boolean isGzipSupported(javax.servlet.http.HttpServletRequest request)
true
if client supports gzip encoding.public static java.lang.String resolveAuthUsername(javax.servlet.http.HttpServletRequest request)
null
if the header is not present.public static java.lang.String resolveAuthPassword(javax.servlet.http.HttpServletRequest request)
null
if the header is not present.public static java.lang.String resolveAuthBearerToken(javax.servlet.http.HttpServletRequest request)
public static void requireAuthentication(javax.servlet.http.HttpServletResponse resp, java.lang.String realm) throws java.io.IOException
java.io.IOException
public static void prepareDownload(javax.servlet.http.HttpServletResponse response, java.io.File file)
public static void prepareDownload(javax.servlet.http.HttpServletResponse response, java.io.File file, java.lang.String mimeType)
public static void prepareResponse(javax.servlet.http.HttpServletResponse response, java.lang.String fileName, java.lang.String mimeType, int fileSize)
response
- http responsefileName
- file name, if full path then file name will be stripped, if null, will be ignored.mimeType
- mime type with optional charset, may be null
fileSize
- if less then 0 it will be ignoredpublic static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest request, java.lang.String cookieName)
null
if cookie with specified name doesn't exist.getAllCookies(javax.servlet.http.HttpServletRequest, String)
public static javax.servlet.http.Cookie[] getAllCookies(javax.servlet.http.HttpServletRequest request, java.lang.String cookieName)
public static java.lang.String readRequestBodyFromReader(javax.servlet.http.HttpServletRequest request) throws java.io.IOException
java.io.IOException
public static java.lang.String readRequestBodyFromStream(javax.servlet.http.HttpServletRequest request) throws java.io.IOException
java.io.IOException
public static java.lang.String getContextPath(javax.servlet.http.HttpServletRequest request)
The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".
public static java.lang.String getContextPath(javax.servlet.ServletContext servletContext)
The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "".
public static java.lang.String getContextPath(javax.servlet.jsp.PageContext pageContext)
public static void storeContextPath(javax.servlet.jsp.PageContext pageContext, java.lang.String contextPathVariableName)
public static void storeContextPath(javax.servlet.ServletContext servletContext, java.lang.String contextPathVariableName)
public static java.lang.Object attribute(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
null
attribute value. Scopes are examined in the
following order: page, request, session, application.public static java.lang.Object attribute(javax.servlet.http.HttpServletRequest request, java.lang.String name)
null
attribute value. Scopes are examined in the
following order: request, session, application.public static java.lang.Object value(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
public static java.lang.Object value(javax.servlet.http.HttpServletRequest request, java.lang.String name)
public static void setScopeAttribute(java.lang.String name, java.lang.Object value, java.lang.String scope, javax.servlet.jsp.PageContext pageContext)
public static void removeScopeAttribute(java.lang.String name, java.lang.String scope, javax.servlet.jsp.PageContext pageContext)
public static boolean isAbsoluteUrl(java.lang.String url)
true
if current URL is absolute, false
otherwise.public static java.lang.String stripSessionId(java.lang.String url)
url
. The session ID
is encoded as a URL "path parameter" beginning with "jsessionid=".
We thus remove anything we find between ";jsessionid=" (inclusive)
and either EOS or a subsequent ';' (exclusive).public static java.lang.String resolveUrl(java.lang.String url, javax.servlet.http.HttpServletRequest request)
public static java.lang.String resolveUrl(java.lang.String url, java.lang.String context)
public static java.lang.Object getRequestParameter(javax.servlet.ServletRequest request, java.lang.String name)
public boolean isGetParameter(javax.servlet.http.HttpServletRequest request, java.lang.String name)
public static java.lang.String[] prepareParameters(java.lang.String[] paramValues, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams)
paramValues
- string array of param valuestreatEmptyParamsAsNull
- empty parameters should be treated as null
ignoreEmptyRequestParams
- if all parameters are empty, return null
public static boolean isJsonRequest(javax.servlet.http.HttpServletRequest servletRequest)
true
if request has JSON content type.public static void copyParamsToAttributes(javax.servlet.http.HttpServletRequest servletRequest, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams)
public static boolean isServletsVersion2_5()
true
if current servlets version is 2.5 or higher.public static java.lang.Throwable getServletError(javax.servlet.ServletRequest request)
public static void setServletError(javax.servlet.ServletRequest request, java.lang.Throwable throwable)
public static void preventCaching(javax.servlet.http.HttpServletResponse response)
Copyright © 2003-present Jodd Team