jodd.servlet
Class ServletUtil

java.lang.Object
  extended by jodd.servlet.ServletUtil

public class ServletUtil
extends java.lang.Object

Miscellaneous servlet utilities.


Field Summary
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 Summary
ServletUtil()
           
 
Method Summary
static java.lang.Object attrValue(javax.servlet.http.HttpServletRequest request, java.lang.String name)
          Returns non-null attribute value.
static java.lang.Object attrValue(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
          Returns non-null attribute value.
static void copyParamsToAttributes(javax.servlet.http.HttpServletRequest servletRequest, boolean trimParams, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams)
          Copies all request parameters to attributes.
static java.lang.String debug(javax.servlet.http.HttpServletRequest request)
          Returns a string with debug info from all servlet objects.
protected static java.lang.String debug(javax.servlet.http.HttpServletRequest request, javax.servlet.jsp.PageContext pageContext)
          Returns a string with debug info from all servlet objects, including the page context.
static java.lang.String debug(javax.servlet.jsp.PageContext pageContext)
          Returns a string with debug info from all servlet objects.
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 getAuthPassword(javax.servlet.http.HttpServletRequest request)
          Decodes the "Authorization" header and retrieves the password from it.
static java.lang.String getAuthUsername(javax.servlet.http.HttpServletRequest request)
          Decodes the "Authorization" header and retrieves the user's name from it.
static java.lang.String getContextPath()
           
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 key)
          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 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 specified mime type.
static void prepareDownload(javax.servlet.http.HttpServletResponse response, java.lang.String fileName, int fileSize)
          Prepares response for file download.
static void prepareDownload(javax.servlet.http.HttpServletResponse response, java.lang.String fileName, java.lang.String mimeType, int fileSize)
          Prepares response for file download.
static java.lang.String[] prepareParameters(java.lang.String[] paramValues, boolean trimParams, boolean treatEmptyParamsAsNull, boolean ignoreEmptyRequestParams)
          Prepares parameters for further processing.
static void preventCaching(javax.servlet.http.HttpServletResponse response)
          Prevents HTTP cache.
static java.lang.String readRequestBody(javax.servlet.http.HttpServletRequest request)
          Reads HTTP request body.
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 resolveUrl(java.lang.String url, javax.servlet.http.HttpServletRequest request)
           
static java.lang.String resolveUrl(java.lang.String url, javax.servlet.jsp.PageContext pageContext)
           
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 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 non-null value of property/attribute.
static java.lang.Object value(javax.servlet.jsp.PageContext pageContext, java.lang.String name)
          Returns non-null value of property/attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_CONTENT_TYPE

public static final java.lang.String HEADER_CONTENT_TYPE
See Also:
Constant Field Values

TYPE_MULTIPART_FORM_DATA

public static final java.lang.String TYPE_MULTIPART_FORM_DATA
See Also:
Constant Field Values

HEADER_AUTHORIZATION

public static final java.lang.String HEADER_AUTHORIZATION
See Also:
Constant Field Values

HEADER_ACCEPT_ENCODING

public static final java.lang.String HEADER_ACCEPT_ENCODING
See Also:
Constant Field Values

CONTENT_DISPOSITION

public static final java.lang.String CONTENT_DISPOSITION
See Also:
Constant Field Values

WWW_AUTHENTICATE

public static final java.lang.String WWW_AUTHENTICATE
See Also:
Constant Field Values

VALID_SCHEME_CHARS

public static final java.lang.String VALID_SCHEME_CHARS
Valid characters in a scheme, as specified by RFC 1738.

See Also:
Constant Field Values
Constructor Detail

ServletUtil

public ServletUtil()
Method Detail

isMultipartRequest

public static boolean isMultipartRequest(javax.servlet.http.HttpServletRequest request)
Returns true if a request is multi-part request.


isGzipSupported

public static boolean isGzipSupported(javax.servlet.http.HttpServletRequest request)
Returns true if client supports gzip encoding.


getAuthUsername

public static java.lang.String getAuthUsername(javax.servlet.http.HttpServletRequest request)
Decodes the "Authorization" header and retrieves the user's name from it. Returns null if the header is not present.


getAuthPassword

public static java.lang.String getAuthPassword(javax.servlet.http.HttpServletRequest request)
Decodes the "Authorization" header and retrieves the password from it. Returns null if the header is not present.


requireAuthentication

public static void requireAuthentication(javax.servlet.http.HttpServletResponse resp,
                                         java.lang.String realm)
                                  throws java.io.IOException
Sends correct headers to require basic authentication for the given realm.

Throws:
java.io.IOException

prepareDownload

public static void prepareDownload(javax.servlet.http.HttpServletResponse response,
                                   java.io.File file)
Prepares response for file download. Mime type and size is resolved from the file.


prepareDownload

public static void prepareDownload(javax.servlet.http.HttpServletResponse response,
                                   java.io.File file,
                                   java.lang.String mimeType)
Prepares response for file download with specified mime type.


prepareDownload

public static void prepareDownload(javax.servlet.http.HttpServletResponse response,
                                   java.lang.String fileName,
                                   int fileSize)
Prepares response for file download. Resolves mime type from file name extension.


prepareDownload

public static void prepareDownload(javax.servlet.http.HttpServletResponse response,
                                   java.lang.String fileName,
                                   java.lang.String mimeType,
                                   int fileSize)
Prepares response for file download.

Parameters:
response - http response
fileName - file name, if full path then file name will be stripped, if null, will be ignored.
mimeType - may be null
fileSize - if less then 0 will be ignored

getCookie

public static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest request,
                                                  java.lang.String cookieName)
Finds and returns cookie from client by its name. Only the first cookie is returned.

Returns:
cookie value or null if cookie with specified name doesn't exist.
See Also:
getAllCookies(javax.servlet.http.HttpServletRequest, String)

getAllCookies

public static javax.servlet.http.Cookie[] getAllCookies(javax.servlet.http.HttpServletRequest request,
                                                        java.lang.String cookieName)
Returns all cookies from client that matches provided name.

See Also:
getCookie(javax.servlet.http.HttpServletRequest, String)

readRequestBody

public static java.lang.String readRequestBody(javax.servlet.http.HttpServletRequest request)
                                        throws java.io.IOException
Reads HTTP request body. Useful only with POST requests. Once body is read, it cannot be read again!

Throws:
java.io.IOException

getContextPath

public static java.lang.String getContextPath(javax.servlet.http.HttpServletRequest request)
Returns correct context path, as by Servlet definition. Different application servers return all variants: "", null, "/".

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 "".


getContextPath

public static java.lang.String getContextPath(javax.servlet.ServletContext servletContext)
Returns correct context path, as by Servlet definition. Different application servers return all variants: "", null, "/".

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 "".


getContextPath

public static java.lang.String getContextPath(javax.servlet.jsp.PageContext pageContext)
See Also:
getContextPath(javax.servlet.ServletContext)

getContextPath

public static java.lang.String getContextPath()
See Also:
getContextPath(javax.servlet.ServletContext)

getRequestParameter

public static java.lang.Object getRequestParameter(javax.servlet.ServletRequest request,
                                                   java.lang.String name)
Returns HTTP request parameter as String or String[].


attrValue

public static java.lang.Object attrValue(javax.servlet.jsp.PageContext pageContext,
                                         java.lang.String name)
Returns non-null attribute value. Scopes are examined in the following order: page, request, session, application.


attrValue

public static java.lang.Object attrValue(javax.servlet.http.HttpServletRequest request,
                                         java.lang.String name)
Returns non-null attribute value. Scopes are examined in the following order: request, session, application.


value

public static java.lang.Object value(javax.servlet.jsp.PageContext pageContext,
                                     java.lang.String name)
Returns non-null value of property/attribute. Scopes are examined in the following order: page (if exist), request, request parameters, session, application.


value

public static java.lang.Object value(javax.servlet.http.HttpServletRequest request,
                                     java.lang.String name)
Returns non-null value of property/attribute. Scopes are examined in the following order: page (if exist), request, request parameters, session, application.


setScopeAttribute

public static void setScopeAttribute(java.lang.String name,
                                     java.lang.Object value,
                                     java.lang.String scope,
                                     javax.servlet.jsp.PageContext pageContext)
Sets scope attribute.


removeScopeAttribute

public static void removeScopeAttribute(java.lang.String name,
                                        java.lang.String scope,
                                        javax.servlet.jsp.PageContext pageContext)
Removes scope attribute.


isAbsoluteUrl

public static boolean isAbsoluteUrl(java.lang.String url)
Returns true if current URL is absolute, false otherwise.


stripSessionId

public static java.lang.String stripSessionId(java.lang.String url)
Strips a servlet session ID from 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).


resolveUrl

public static java.lang.String resolveUrl(java.lang.String url,
                                          javax.servlet.jsp.PageContext pageContext)

resolveUrl

public static java.lang.String resolveUrl(java.lang.String url,
                                          javax.servlet.http.HttpServletRequest request)

resolveUrl

public static java.lang.String resolveUrl(java.lang.String url,
                                          java.lang.String context)

isGetParameter

public boolean isGetParameter(javax.servlet.http.HttpServletRequest request,
                              java.lang.String key)
Checks if some parameter is in GET parameters.


prepareParameters

public static java.lang.String[] prepareParameters(java.lang.String[] paramValues,
                                                   boolean trimParams,
                                                   boolean treatEmptyParamsAsNull,
                                                   boolean ignoreEmptyRequestParams)
Prepares parameters for further processing.

Parameters:
paramValues - string array of param values
trimParams - trim parameters
treatEmptyParamsAsNull - empty parameters should be treated as null
ignoreEmptyRequestParams - if all parameters are empty, return null

copyParamsToAttributes

public static void copyParamsToAttributes(javax.servlet.http.HttpServletRequest servletRequest,
                                          boolean trimParams,
                                          boolean treatEmptyParamsAsNull,
                                          boolean ignoreEmptyRequestParams)
Copies all request parameters to attributes.


isServletsVersion2_5

public static boolean isServletsVersion2_5()
Returns true if current servlets version is 2.5 or higher.


getServletError

public static java.lang.Throwable getServletError(javax.servlet.ServletRequest request)
Returns servlet error.


setServletError

public static void setServletError(javax.servlet.ServletRequest request,
                                   java.lang.Throwable throwable)
Sets servlet error.


debug

public static java.lang.String debug(javax.servlet.http.HttpServletRequest request)
Returns a string with debug info from all servlet objects.

See Also:
debug(HttpServletRequest, PageContext)

debug

public static java.lang.String debug(javax.servlet.jsp.PageContext pageContext)
Returns a string with debug info from all servlet objects.

See Also:
debug(HttpServletRequest, PageContext)

debug

protected static java.lang.String debug(javax.servlet.http.HttpServletRequest request,
                                        javax.servlet.jsp.PageContext pageContext)
Returns a string with debug info from all servlet objects, including the page context.


preventCaching

public static void preventCaching(javax.servlet.http.HttpServletResponse response)
Prevents HTTP cache.



Copyright © 2003-2012 Jodd Team