public class WebUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_CHARACTER_ENCODING
Default character encoding to use when
request.getCharacterEncoding returns
null, according to the Servlet spec.
|
static String |
FORWARD_CONTEXT_PATH_ATTRIBUTE
|
static String |
FORWARD_PATH_INFO_ATTRIBUTE
|
static String |
FORWARD_QUERY_STRING_ATTRIBUTE
|
static String |
FORWARD_REQUEST_URI_ATTRIBUTE
Standard Servlet 2.4+ spec request attributes for forward URI and paths.
|
static String |
FORWARD_SERVLET_PATH_ATTRIBUTE
|
static String |
INCLUDE_CONTEXT_PATH_ATTRIBUTE
|
static String |
INCLUDE_PATH_INFO_ATTRIBUTE
|
static String |
INCLUDE_QUERY_STRING_ATTRIBUTE
|
static String |
INCLUDE_REQUEST_URI_ATTRIBUTE
Standard Servlet 2.3+ spec request attributes for include URI and paths.
|
static String |
INCLUDE_SERVLET_PATH_ATTRIBUTE
|
static String |
SAVED_REQUEST_KEY
Session key used to save a request and later restore it, for example when redirecting to a requested page after login, equal to
shiroSavedRequest.
|
static String |
SERVLET_REQUEST_KEY
|
static String |
SERVLET_RESPONSE_KEY
|
| Constructor and Description |
|---|
WebUtils()
|
| Modifier and Type | Method and Description |
|---|---|
static boolean |
_isSessionCreationEnabled(Object
Returns
true if a session is allowed to be created for a subject-associated request,
false otherwise.
|
static boolean |
_isSessionCreationEnabled(ServletRequest request)
Returns
true if a session is allowed to be created for a subject-associated request,
false otherwise.
|
static String |
decodeRequestString(HttpServletRequest request, String
Decode the given source string with a URLDecoder.
|
protected static String |
determineEncoding(HttpServletRequest request)
Determine the encoding for the given request.
|
static SavedRequest |
getAndClearSavedRequest(ServletRequest request)
|
static String |
getCleanParam(ServletRequest request, String
Convenience method that returns a request parameter value, first running it through
StringUtils.
|
static String |
getContextPath(HttpServletRequest request)
Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.
|
static HttpServletRequest |
getHttpRequest(Object
|
static HttpServletResponse |
getHttpResponse(Object
|
static String |
getPathWithinApplication(HttpServletRequest request)
Return the path within the web application for the given request.
|
static ServletRequest |
getRequest(Object
|
static String |
getRequestUri(HttpServletRequest request)
Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.
|
static WebEnvironment |
getRequiredWebEnvironment(ServletContext sc)
Find the Shiro
WebEnvironment for this web application, which is typically loaded via the
EnvironmentLoaderListener.
|
static ServletResponse |
getResponse(Object
|
static SavedRequest |
getSavedRequest(ServletRequest request)
|
static WebEnvironment |
getWebEnvironment(ServletContext sc)
Find the Shiro
WebEnvironment for this web application, which is typically loaded via
EnvironmentLoaderListener.
|
static WebEnvironment |
getWebEnvironment(ServletContext sc, String
Find the Shiro
WebEnvironment for this web application.
|
static boolean |
isHttp(Object
|
static void |
issueRedirect(ServletRequest request, ServletResponse response, String
Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.
|
static void |
issueRedirect(ServletRequest request, ServletResponse response, String
Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.
|
static void |
issueRedirect(ServletRequest request, ServletResponse response, String
Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.
|
static void |
issueRedirect(ServletRequest request, ServletResponse response, String
Redirects the current request to a new URL based on the given parameters.
|
static boolean |
isTrue(ServletRequest request, String
Checks to see if a request param is considered true using a loose matching strategy for general values that indicate that something is true or enabled, etc.
|
static boolean |
isWeb(Object
|
static String |
normalize(String
Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it.
|
static void |
redirectToSavedRequest(ServletRequest request, ServletResponse response, String
Redirects the to the request url from a previously
saved request, or if there is no saved request, redirects the end user to the specified
fallbackUrl.
|
static void |
saveRequest(ServletRequest request)
|
static HttpServletRequest |
toHttp(ServletRequest request)
A convenience method that merely casts the incoming
ServletRequest to an
HttpServletRequest:
return (HttpServletRequest)request;
Logic could be changed in the future for logging or throwing an meaningful exception in non HTTP request environments (e.g.
|
static HttpServletResponse |
toHttp(ServletResponse response)
A convenience method that merely casts the incoming
ServletResponse to an
HttpServletResponse:
return (HttpServletResponse)response;
Logic could be changed in the future for logging or throwing an meaningful exception in non HTTP request environments (e.g.
|
public static final StringSERVLET_REQUEST_KEY
public static final StringSERVLET_RESPONSE_KEY
public static final StringSAVED_REQUEST_KEY
Session key used to save a request and later restore it, for example when redirecting to a requested page after login, equal to
shiroSavedRequest.
public static final StringINCLUDE_REQUEST_URI_ATTRIBUTE
If included via a RequestDispatcher, the current resource will see the originating request. Its own URI and paths are exposed as request attributes.
public static final StringINCLUDE_CONTEXT_PATH_ATTRIBUTE
public static final StringINCLUDE_SERVLET_PATH_ATTRIBUTE
public static final StringINCLUDE_PATH_INFO_ATTRIBUTE
public static final StringINCLUDE_QUERY_STRING_ATTRIBUTE
public static final StringFORWARD_REQUEST_URI_ATTRIBUTE
If forwarded to via a RequestDispatcher, the current resource will see its own URI and paths. The originating URI and paths are exposed as request attributes.
public static final StringFORWARD_CONTEXT_PATH_ATTRIBUTE
public static final StringFORWARD_SERVLET_PATH_ATTRIBUTE
public static final StringFORWARD_PATH_INFO_ATTRIBUTE
public static final StringFORWARD_QUERY_STRING_ATTRIBUTE
public static final StringDEFAULT_CHARACTER_ENCODING
request.getCharacterEncoding returns
null, according to the Servlet spec.
javax.servlet.ServletRequest#getCharacterEncoding,
Constant Field Values
public static StringgetPathWithinApplication(HttpServletRequest request)
http://www.somehost.com/myapp/my/url.jsp,
for an application deployed to
/mayapp (the application's context path), this method would return
/my/url.jsp.
request - current HTTP request
public static StringgetRequestUri(HttpServletRequest request)
As the value returned by request.getRequestURI() is not decoded by the servlet container, this method will decode it.
The URI that the web container resolves should be correct, but some containers like JBoss/Jetty incorrectly include ";" strings like ";jsessionid" in the URI. This method cuts off such incorrect appendices.
request - current HTTP request
public static Stringnormalize(String path)
path - Relative path to be normalized
public static StringgetContextPath(HttpServletRequest request)
As the value returned by request.getContextPath() is not decoded by the servlet container, this method will decode it.
request - current HTTP request
public static WebEnvironmentgetRequiredWebEnvironment(ServletContext sc) throws IllegalStateException
WebEnvironment for this web application, which is typically loaded via the
EnvironmentLoaderListener.
This implementation rethrows an exception that happened on environment startup to differentiate between a failed environment startup and no environment at all.
sc - ServletContext to find the web application context for
IllegalStateException - if the root WebApplicationContext could not be found
EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY
public static WebEnvironmentgetWebEnvironment(ServletContext sc)
WebEnvironment for this web application, which is typically loaded via
EnvironmentLoaderListener.
This implementation rethrows an exception that happened on environment startup to differentiate between a failed environment startup and no environment at all.
sc - ServletContext to find the web application context for
null if none
EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY
public static WebEnvironmentgetWebEnvironment(ServletContext sc, String attrName)
WebEnvironment for this web application.
sc - ServletContext to find the web application context for
attrName - the name of the ServletContext attribute to look for
null if none
public static StringdecodeRequestString(HttpServletRequest request, String source)
The default implementation uses URLDecoder.decode(input, enc).
request - current HTTP request
source - the String to decode
DEFAULT_CHARACTER_ENCODING,
javax.servlet.ServletRequest#getCharacterEncoding,
URLDecoder.decode(String, String) ,
URLDecoder.decode(String)
protected static StringdetermineEncoding(HttpServletRequest request)
The default implementation checks the request's character encoding, and if that null, falls back to the DEFAULT_CHARACTER_ENCODING.
request - current HTTP request
null)
javax.servlet.ServletRequest#getCharacterEncoding()
public static boolean isWeb(ObjectrequestPairSource)
public static boolean isHttp(ObjectrequestPairSource)
public static ServletRequest getRequest(ObjectrequestPairSource)
public static ServletResponse getResponse(ObjectrequestPairSource)
public static HttpServletRequest getHttpRequest(ObjectrequestPairSource)
public static HttpServletResponse getHttpResponse(ObjectrequestPairSource)
public static boolean _isSessionCreationEnabled(ObjectrequestPairSource)
true if a session is allowed to be created for a subject-associated request,
false otherwise.
This method exists for Shiro's internal framework needs and should never be called by Shiro end-users. It could be changed/removed at any time.
requestPairSource - a
RequestPairSource instance, almost always a
WebSubject instance.
true if a session is allowed to be created for a subject-associated request,
false otherwise.
public static boolean _isSessionCreationEnabled(ServletRequest request)
true if a session is allowed to be created for a subject-associated request,
false otherwise.
This method exists for Shiro's internal framework needs and should never be called by Shiro end-users. It could be changed/removed at any time.
request - incoming servlet request.
true if a session is allowed to be created for a subject-associated request,
false otherwise.
public static HttpServletRequest toHttp(ServletRequest request)
ServletRequest to an
HttpServletRequest:
return (HttpServletRequest)request;
Logic could be changed in the future for logging or throwing an meaningful exception in non HTTP request environments (e.g. Portlet API).
request - the incoming ServletRequest
request argument casted to an
HttpServletRequest.
public static HttpServletResponse toHttp(ServletResponse response)
ServletResponse to an
HttpServletResponse:
return (HttpServletResponse)response;
Logic could be changed in the future for logging or throwing an meaningful exception in non HTTP request environments (e.g. Portlet API).
response - the outgoing ServletResponse
response argument casted to an
HttpServletResponse.
public static void issueRedirect(ServletRequest request,
ServletResponse response,
String url,
Map queryParams,
boolean contextRelative,
boolean http10Compatible)
throws IOException
request - the servlet request.
response - the servlet response.
url - the URL to redirect the user to.
queryParams - a map of parameters that should be set as request parameters for the new request.
contextRelative - true if the URL is relative to the servlet context path, or false if the URL is absolute.
http10Compatible - whether to stay compatible with HTTP 1.0 clients.
IOException - if thrown by response methods.
public static void issueRedirect(ServletRequest request,
ServletResponse response,
String url)
throws IOException
request - the servlet request.
response - the servlet response.
url - the URL to redirect the user to.
IOException - if thrown by response methods.
public static void issueRedirect(ServletRequest request,
ServletResponse response,
String url,
Map queryParams)
throws IOException
request - the servlet request.
response - the servlet response.
url - the URL to redirect the user to.
queryParams - a map of parameters that should be set as request parameters for the new request.
IOException - if thrown by response methods.
public static void issueRedirect(ServletRequest request,
ServletResponse response,
String url,
Map queryParams,
boolean contextRelative)
throws IOException
request - the servlet request.
response - the servlet response.
url - the URL to redirect the user to.
queryParams - a map of parameters that should be set as request parameters for the new request.
contextRelative - true if the URL is relative to the servlet context path, or false if the URL is absolute.
IOException - if thrown by response methods.
public static boolean isTrue(ServletRequest request,
String paramName)
Checks to see if a request param is considered true using a loose matching strategy for general values that indicate that something is true or enabled, etc.
Values that are considered "true" include (case-insensitive): true, t, 1, enabled, y, yes, on.
request - the servlet request
paramName - @return true if the param value is considered true or false if it isn't.
public static StringgetCleanParam(ServletRequest request, String paramName)
StringUtils.clean(String) .
request - the servlet request.
paramName - the parameter name.
public static void saveRequest(ServletRequest request)
public static SavedRequestgetAndClearSavedRequest(ServletRequest request)
public static SavedRequestgetSavedRequest(ServletRequest request)
public static void redirectToSavedRequest(ServletRequest request,
ServletResponse response,
String fallbackUrl)
throws IOException
saved request, or if there is no saved request, redirects the end user to the specified
fallbackUrl. If there is no saved request or fallback url, this method throws an
IllegalStateException.
This method is primarily used to support a common login scenario - if an unauthenticated user accesses a page that requires authentication, it is expected that request is
saved first and then redirected to the login page. Then, after a successful login, this method can be called to redirect them back to their originally requested URL, a nice usability feature.
request - the incoming request
response - the outgoing response
fallbackUrl - the fallback url to redirect to if there is no saved request available.
IllegalStateException - if there is no saved request and the
fallbackUrl is
null.
IOException - if there is an error redirecting