Class WebUtils
- Since:
- 0.9
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final StringDefault character encoding to use whenrequest.getCharacterEncodingreturnsnull, according to the Servlet spec.static final Stringstatic final Stringstatic final Stringstatic final StringStandard Servlet 2.4+ spec request attributes for forward URI and paths.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringStandard Servlet 2.3+ spec request attributes for include URI and paths.static final Stringstatic final StringSessionkey used to save a request and later restore it, for example when redirecting to a requested page after login, equal toshiroSavedRequest.static final Stringstatic final String -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdecodeRequestString(javax.servlet.http.HttpServletRequest request, String source) Decode the given source string with a URLDecoder.protected static StringdetermineEncoding(javax.servlet.http.HttpServletRequest request) Determine the encoding for the given request.static SavedRequestgetAndClearSavedRequest(javax.servlet.ServletRequest request) static StringgetCleanParam(javax.servlet.ServletRequest request, String paramName) Convenience method that returns a request parameter value, first running it throughStringUtils.clean(String).static StringgetContextPath(javax.servlet.http.HttpServletRequest request) Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.static javax.servlet.http.HttpServletRequestgetHttpRequest(Object requestPairSource) static javax.servlet.http.HttpServletResponsegetHttpResponse(Object requestPairSource) static StringgetPathWithinApplication(javax.servlet.http.HttpServletRequest request) Return the path within the web application for the given request.static javax.servlet.ServletRequestgetRequest(Object requestPairSource) static StringgetRequestUri(javax.servlet.http.HttpServletRequest request) Deprecated.use getPathWithinApplication() to get the path minus the context path, or call HttpServletRequest.getRequestURI() directly from your code.static WebEnvironmentgetRequiredWebEnvironment(javax.servlet.ServletContext sc) Find the ShiroWebEnvironmentfor this web application, which is typically loaded via theEnvironmentLoaderListener.static javax.servlet.ServletResponsegetResponse(Object requestPairSource) static SavedRequestgetSavedRequest(javax.servlet.ServletRequest request) static WebEnvironmentgetWebEnvironment(javax.servlet.ServletContext sc) Find the ShiroWebEnvironmentfor this web application, which is typically loaded viaEnvironmentLoaderListener.static WebEnvironmentgetWebEnvironment(javax.servlet.ServletContext sc, String attrName) Find the ShiroWebEnvironmentfor this web application.static booleanstatic booleanstatic booleanisSessionCreationEnabled(Object requestPairSource) Returnstrueif a session is allowed to be created for a subject-associated request,falseotherwise.static booleanisSessionCreationEnabled(javax.servlet.ServletRequest request) Returnstrueif a session is allowed to be created for a subject-associated request,falseotherwise.static voidissueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url) Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.static voidissueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams) Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.static voidissueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams, boolean contextRelative) Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.static voidissueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams, boolean contextRelative, boolean http10Compatible) Redirects the current request to a new URL based on the given parameters.static booleanChecks 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 booleanstatic StringNormalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it.static voidredirectToSavedRequest(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String fallbackUrl) Redirects the to the request url from a previouslysavedrequest, or if there is no saved request, redirects the end user to the specifiedfallbackUrl.static voidReload System properties.static voidsaveRequest(javax.servlet.ServletRequest request) static javax.servlet.http.HttpServletRequesttoHttp(javax.servlet.ServletRequest request) A convenience method that merely casts the incomingServletRequestto anHttpServletRequest:static javax.servlet.http.HttpServletResponsetoHttp(javax.servlet.ServletResponse response) A convenience method that merely casts the incomingServletResponseto anHttpServletResponse:
-
Field Details
-
SERVLET_REQUEST_KEY
-
SERVLET_RESPONSE_KEY
-
ALLOW_BACKSLASH
- See Also:
-
SAVED_REQUEST_KEY
Sessionkey used to save a request and later restore it, for example when redirecting to a requested page after login, equal toshiroSavedRequest.- See Also:
-
INCLUDE_REQUEST_URI_ATTRIBUTE
Standard Servlet 2.3+ spec request attributes for include URI and paths.If included via a RequestDispatcher, the current resource will see the originating request. Its own URI and paths are exposed as request attributes.
- See Also:
-
INCLUDE_CONTEXT_PATH_ATTRIBUTE
- See Also:
-
INCLUDE_SERVLET_PATH_ATTRIBUTE
- See Also:
-
INCLUDE_PATH_INFO_ATTRIBUTE
- See Also:
-
INCLUDE_QUERY_STRING_ATTRIBUTE
- See Also:
-
FORWARD_REQUEST_URI_ATTRIBUTE
Standard Servlet 2.4+ spec request attributes for forward URI and paths.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.
- See Also:
-
FORWARD_CONTEXT_PATH_ATTRIBUTE
- See Also:
-
FORWARD_SERVLET_PATH_ATTRIBUTE
- See Also:
-
FORWARD_PATH_INFO_ATTRIBUTE
- See Also:
-
FORWARD_QUERY_STRING_ATTRIBUTE
- See Also:
-
DEFAULT_CHARACTER_ENCODING
Default character encoding to use whenrequest.getCharacterEncodingreturnsnull, according to the Servlet spec.- See Also:
-
-
Method Details
-
getPathWithinApplication
Return the path within the web application for the given request. Detects include request URL if called within a RequestDispatcher include. For example, for a request to URLhttp://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.- Parameters:
request- current HTTP request- Returns:
- the path within the web application
-
getRequestUri
Deprecated.use getPathWithinApplication() to get the path minus the context path, or call HttpServletRequest.getRequestURI() directly from your code.Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.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.
- Parameters:
request- current HTTP request- Returns:
- the request URI
-
normalize
Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it. WARNING - This method is useful only for normalizing application-generated paths. It does not try to perform security checks for malicious input. Normalize operations were was happily taken from org.apache.catalina.util.RequestUtil in Tomcat trunk, r939305- Parameters:
path- Relative path to be normalized- Returns:
- normalized path
-
getContextPath
Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.As the value returned by
request.getContextPath()is not decoded by the servlet container, this method will decode it.- Parameters:
request- current HTTP request- Returns:
- the context path
-
getRequiredWebEnvironment
public static WebEnvironment getRequiredWebEnvironment(javax.servlet.ServletContext sc) throws IllegalStateException Find the ShiroWebEnvironmentfor this web application, which is typically loaded via theEnvironmentLoaderListener. This implementation rethrows an exception that happened on environment startup to differentiate between a failed environment startup and no environment at all.- Parameters:
sc- ServletContext to find the web application context for- Returns:
- the root WebApplicationContext for this web app
- Throws:
IllegalStateException- if the root WebApplicationContext could not be found- Since:
- 1.2
- See Also:
-
getWebEnvironment
Find the ShiroWebEnvironmentfor this web application, which is typically loaded viaEnvironmentLoaderListener. This implementation rethrows an exception that happened on environment startup to differentiate between a failed environment startup and no environment at all.- Parameters:
sc- ServletContext to find the web application context for- Returns:
- the root WebApplicationContext for this web app, or
nullif none - Since:
- 1.2
- See Also:
-
getWebEnvironment
Find the ShiroWebEnvironmentfor this web application.- Parameters:
sc- ServletContext to find the web application context forattrName- the name of the ServletContext attribute to look for- Returns:
- the desired WebEnvironment for this web app, or
nullif none - Since:
- 1.2
-
decodeRequestString
public static String decodeRequestString(javax.servlet.http.HttpServletRequest request, String source) Decode the given source string with a URLDecoder. The encoding will be taken from the request, falling back to the default "ISO-8859-1".The default implementation uses
URLDecoder.decode(input, enc).- Parameters:
request- current HTTP requestsource- the String to decode- Returns:
- the decoded String
- See Also:
-
determineEncoding
Determine the encoding for the given request. Can be overridden in subclasses.The default implementation checks the request's
character encoding, and if thatnull, falls back to theDEFAULT_CHARACTER_ENCODING.- Parameters:
request- current HTTP request- Returns:
- the encoding for the request (never
null) - See Also:
-
isWeb
-
isHttp
-
getRequest
-
getResponse
-
getHttpRequest
-
getHttpResponse
-
isSessionCreationEnabled
Returnstrueif a session is allowed to be created for a subject-associated request,falseotherwise. 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.- Parameters:
requestPairSource- aRequestPairSourceinstance, almost always aWebSubjectinstance.- Returns:
trueif a session is allowed to be created for a subject-associated request,falseotherwise.
-
isSessionCreationEnabled
Returnstrueif a session is allowed to be created for a subject-associated request,falseotherwise. 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.- Parameters:
request- incoming servlet request.- Returns:
trueif a session is allowed to be created for a subject-associated request,falseotherwise.
-
toHttp
A convenience method that merely casts the incomingServletRequestto anHttpServletRequest: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).- Parameters:
request- the incoming ServletRequest- Returns:
- the
requestargument casted to anHttpServletRequest.
-
toHttp
A convenience method that merely casts the incomingServletResponseto anHttpServletResponse: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).- Parameters:
response- the outgoing ServletResponse- Returns:
- the
responseargument casted to anHttpServletResponse.
-
issueRedirect
public static void issueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams, boolean contextRelative, boolean http10Compatible) throws IOException Redirects the current request to a new URL based on the given parameters.- Parameters:
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.- Throws:
IOException- if thrown by response methods.
-
issueRedirect
public static void issueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url) throws IOException Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.- Parameters:
request- the servlet request.response- the servlet response.url- the URL to redirect the user to.- Throws:
IOException- if thrown by response methods.
-
issueRedirect
public static void issueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams) throws IOException Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.- Parameters:
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.- Throws:
IOException- if thrown by response methods.
-
issueRedirect
public static void issueRedirect(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String url, Map queryParams, boolean contextRelative) throws IOException Redirects the current request to a new URL based on the given parameters and default values for unspecified parameters.- Parameters:
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.- Throws:
IOException- if thrown by response methods.
-
isTrue
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.
- Parameters:
request- the servlet requestparamName- @return true if the param value is considered true or false if it isn't.- Returns:
- true if the given parameter is considered "true" - false otherwise.
-
getCleanParam
Convenience method that returns a request parameter value, first running it throughStringUtils.clean(String).- Parameters:
request- the servlet request.paramName- the parameter name.- Returns:
- the clean param value, or null if the param does not exist or is empty.
-
saveRequest
-
getAndClearSavedRequest
-
getSavedRequest
-
redirectToSavedRequest
public static void redirectToSavedRequest(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, String fallbackUrl) throws IOException Redirects the to the request url from a previouslysavedrequest, or if there is no saved request, redirects the end user to the specifiedfallbackUrl. If there is no saved request or fallback url, this method throws anIllegalStateException. 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 issavedfirst 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.- Parameters:
request- the incoming requestresponse- the outgoing responsefallbackUrl- the fallback url to redirect to if there is no saved request available.- Throws:
IllegalStateException- if there is no saved request and thefallbackUrlisnull.IOException- if there is an error redirecting- Since:
- 1.0
-
isAllowBackslash
-
reloadSystemProperties
Reload System properties. If relevant system property is modified ,this method needs to be called to take effect. There is a propertyorg.apache.shiro.web.ALLOW_BACKSLASHthat needs attention.
-