Package org.grails.web.util
Class WebUtils
- java.lang.Object
-
- org.springframework.web.util.WebUtils
-
- org.grails.web.util.WebUtils
-
public class WebUtils extends org.springframework.web.util.WebUtilsUtility methods to access commons objects and perform common web related functions for the internal framework.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringASYNC_REQUEST_URI_ATTRIBUTEstatic java.lang.StringDISPATCH_ACTION_PARAMETERstatic java.lang.StringENABLE_FILE_EXTENSIONSstatic java.lang.StringEXCEPTION_ATTRIBUTEstatic java.lang.StringGRAILS_DISPATCH_EXTENSIONstatic java.lang.StringGRAILS_SERVLET_PATHstatic java.lang.StringLAYOUT_ATTRIBUTEstatic java.lang.StringRENDERING_VIEWstatic java.lang.StringSEND_ALLOW_HEADER_FOR_INVALID_HTTP_METHODstatic charSLASH-
Fields inherited from class org.springframework.web.util.WebUtils
CONTENT_TYPE_CHARSET_PREFIX, DEFAULT_CHARACTER_ENCODING, DEFAULT_WEB_APP_ROOT_KEY, ERROR_EXCEPTION_ATTRIBUTE, ERROR_EXCEPTION_TYPE_ATTRIBUTE, ERROR_MESSAGE_ATTRIBUTE, ERROR_REQUEST_URI_ATTRIBUTE, ERROR_SERVLET_NAME_ATTRIBUTE, ERROR_STATUS_CODE_ATTRIBUTE, FORWARD_CONTEXT_PATH_ATTRIBUTE, FORWARD_PATH_INFO_ATTRIBUTE, FORWARD_QUERY_STRING_ATTRIBUTE, FORWARD_REQUEST_URI_ATTRIBUTE, FORWARD_SERVLET_PATH_ATTRIBUTE, HTML_ESCAPE_CONTEXT_PARAM, INCLUDE_CONTEXT_PATH_ATTRIBUTE, INCLUDE_PATH_INFO_ATTRIBUTE, INCLUDE_QUERY_STRING_ATTRIBUTE, INCLUDE_REQUEST_URI_ATTRIBUTE, INCLUDE_SERVLET_PATH_ATTRIBUTE, RESPONSE_ENCODED_HTML_ESCAPE_CONTEXT_PARAM, SESSION_MUTEX_ATTRIBUTE, SUBMIT_IMAGE_SUFFIXES, TEMP_DIR_CONTEXT_ATTRIBUTE, WEB_APP_ROOT_KEY_PARAM
-
-
Constructor Summary
Constructors Constructor Description WebUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.StringaddViewPrefix(java.lang.String viewName)Deprecated.Does not take into account the url converterstatic java.lang.StringaddViewPrefix(java.lang.String viewName, java.lang.String controllerName)static booleanareFileExtensionsEnabled()Returns the value of the "grails.mime.file.extensions" setting configured in application.groovystatic voidcleanupIncludeRequestAttributes(javax.servlet.http.HttpServletRequest request, java.util.Map<java.lang.String,java.lang.Object> toRestore)static voidclearGrailsWebRequest()Removes any GrailsWebRequest instance from the current request.static voidexposeIncludeRequestAttributes(javax.servlet.http.HttpServletRequest request)Expose the current request URI and paths asHttpServletRequestattributes under the keys defined in the Servlet 2.4 specification, for containers that implement 2.3 or an earlier version of the Servlet API:javax.servlet.forward.request_uri,javax.servlet.forward.context_path,javax.servlet.forward.servlet_path,javax.servlet.forward.path_info,javax.servlet.forward.query_string.static java.util.Map<java.lang.String,java.lang.Object>exposeRequestAttributesAndReturnOldValues(javax.servlet.http.HttpServletRequest request, java.util.Map<java.lang.String,?> attributes)static grails.core.GrailsApplicationfindApplication(javax.servlet.ServletContext servletContext)Looks up the GrailsApplication instancestatic org.springframework.context.ApplicationContextfindApplicationContext(javax.servlet.ServletContext servletContext)Locates the ApplicationContext, returns null if not foundstatic java.util.Map<java.lang.String,java.lang.Object>fromQueryString(java.lang.String queryString)Takes a query string and returns the results as a map where the values are either a single entry or a list of valuesstatic java.lang.StringgetFormatFromURI(java.lang.String uri)Obtains the format from the URI.static java.lang.StringgetFormatFromURI(java.lang.String uri, grails.web.mime.MimeType[] mimeTypes)Obtains the format from the URI.static java.lang.StringgetForwardURI(javax.servlet.http.HttpServletRequest request)Obtains the forwardURI from the request, since Grails uses a forwarding technique for URL mappings.static java.lang.StringgetRequestURIForGrailsDispatchURI(javax.servlet.http.HttpServletRequest request)The Grails dispatch servlet maps URIs like /app/grails/example/index.dispatch.static booleanisAsync(javax.servlet.http.HttpServletRequest request)Check whether the given request is a forward requeststatic booleanisError(javax.servlet.http.HttpServletRequest request)Check whether the given request is a forward requeststatic booleanisForward(javax.servlet.http.HttpServletRequest request)Check whether the given request is a forward requeststatic booleanisForwardOrInclude(javax.servlet.http.HttpServletRequest request)Check whether the given request is an include or forward requeststatic booleanisInclude(javax.servlet.http.HttpServletRequest request)Check whether the given request is an include requeststatic grails.core.GrailsApplicationlookupApplication(javax.servlet.ServletContext servletContext)Looks up the GrailsApplication instancestatic org.springframework.web.servlet.HandlerInterceptor[]lookupHandlerInterceptors(javax.servlet.ServletContext servletContext)Looks up all of the HandlerInterceptor instances registered for the applicationstatic org.springframework.web.servlet.ViewResolverlookupViewResolver(javax.servlet.ServletContext servletContext)static org.springframework.web.servlet.ViewResolverlookupViewResolver(org.springframework.context.ApplicationContext wac)static org.springframework.web.context.request.WebRequestInterceptor[]lookupWebRequestInterceptors(javax.servlet.ServletContext servletContext)Looks up all of the WebRequestInterceptor instances registered with the applicationstatic org.springframework.web.servlet.ViewresolveView(javax.servlet.http.HttpServletRequest request, java.lang.String viewName, java.lang.String controllerName, org.springframework.web.servlet.ViewResolver viewResolver)Resolves a view for the given view name and controller namestatic GrailsWebRequestretrieveGrailsWebRequest()Returns the GrailsWebRequest associated with the current request.static voidstoreGrailsWebRequest(GrailsWebRequest webRequest)Helper method to store the given GrailsWebRequest for the current request.static java.lang.StringtoQueryString(java.util.Map parameters)Converts the given parameters to a query string using the default UTF-8 encodingstatic java.lang.StringtoQueryString(java.util.Map params, java.lang.String encoding)Converts the given params into a query string started with ?-
Methods inherited from class org.springframework.web.util.WebUtils
clearErrorRequestAttributes, exposeErrorRequestAttributes, findParameterValue, findParameterValue, getCookie, getDefaultHtmlEscape, getNativeRequest, getNativeResponse, getParametersStartingWith, getRealPath, getRequiredSessionAttribute, getResponseEncodedHtmlEscape, getSessionAttribute, getSessionId, getSessionMutex, getTempDir, hasSubmitParameter, isIncludeRequest, isSameOrigin, isValidOrigin, parseMatrixVariables, removeWebAppRootSystemProperty, setSessionAttribute, setWebAppRootSystemProperty
-
-
-
-
Field Detail
-
SLASH
public static final char SLASH
- See Also:
- Constant Field Values
-
ENABLE_FILE_EXTENSIONS
public static final java.lang.String ENABLE_FILE_EXTENSIONS
- See Also:
- Constant Field Values
-
DISPATCH_ACTION_PARAMETER
public static final java.lang.String DISPATCH_ACTION_PARAMETER
- See Also:
- Constant Field Values
-
SEND_ALLOW_HEADER_FOR_INVALID_HTTP_METHOD
public static final java.lang.String SEND_ALLOW_HEADER_FOR_INVALID_HTTP_METHOD
- See Also:
- Constant Field Values
-
LAYOUT_ATTRIBUTE
public static final java.lang.String LAYOUT_ATTRIBUTE
- See Also:
- Constant Field Values
-
RENDERING_VIEW
public static final java.lang.String RENDERING_VIEW
- See Also:
- Constant Field Values
-
GRAILS_DISPATCH_EXTENSION
public static final java.lang.String GRAILS_DISPATCH_EXTENSION
- See Also:
- Constant Field Values
-
GRAILS_SERVLET_PATH
public static final java.lang.String GRAILS_SERVLET_PATH
- See Also:
- Constant Field Values
-
EXCEPTION_ATTRIBUTE
public static final java.lang.String EXCEPTION_ATTRIBUTE
- See Also:
- Constant Field Values
-
ASYNC_REQUEST_URI_ATTRIBUTE
public static final java.lang.String ASYNC_REQUEST_URI_ATTRIBUTE
- See Also:
- Constant Field Values
-
-
Method Detail
-
lookupViewResolver
public static org.springframework.web.servlet.ViewResolver lookupViewResolver(javax.servlet.ServletContext servletContext)
-
lookupViewResolver
public static org.springframework.web.servlet.ViewResolver lookupViewResolver(org.springframework.context.ApplicationContext wac)
-
lookupHandlerInterceptors
public static org.springframework.web.servlet.HandlerInterceptor[] lookupHandlerInterceptors(javax.servlet.ServletContext servletContext)
Looks up all of the HandlerInterceptor instances registered for the application- Parameters:
servletContext- The ServletContext instance- Returns:
- An array of HandlerInterceptor instances
-
lookupWebRequestInterceptors
public static org.springframework.web.context.request.WebRequestInterceptor[] lookupWebRequestInterceptors(javax.servlet.ServletContext servletContext)
Looks up all of the WebRequestInterceptor instances registered with the application- Parameters:
servletContext- The ServletContext instance- Returns:
- An array of WebRequestInterceptor instances
-
getRequestURIForGrailsDispatchURI
public static java.lang.String getRequestURIForGrailsDispatchURI(javax.servlet.http.HttpServletRequest request)
The Grails dispatch servlet maps URIs like /app/grails/example/index.dispatch. This method infers the controller URI for the dispatch URI so that /app/grails/example/index.dispatch becomes /app/example/index- Parameters:
request- The request
-
lookupApplication
public static grails.core.GrailsApplication lookupApplication(javax.servlet.ServletContext servletContext)
Looks up the GrailsApplication instance- Returns:
- The GrailsApplication instance
-
findApplication
public static grails.core.GrailsApplication findApplication(javax.servlet.ServletContext servletContext)
Looks up the GrailsApplication instance- Returns:
- The GrailsApplication instance
-
findApplicationContext
public static org.springframework.context.ApplicationContext findApplicationContext(javax.servlet.ServletContext servletContext)
Locates the ApplicationContext, returns null if not found- Parameters:
servletContext- The servlet context- Returns:
- The ApplicationContext
-
resolveView
public static org.springframework.web.servlet.View resolveView(javax.servlet.http.HttpServletRequest request, java.lang.String viewName, java.lang.String controllerName, org.springframework.web.servlet.ViewResolver viewResolver) throws java.lang.ExceptionResolves a view for the given view name and controller name- Parameters:
request- The requestviewName- The view namecontrollerName- The controller nameviewResolver- The resolver- Returns:
- A View or null
- Throws:
java.lang.Exception- Thrown if an error occurs
-
addViewPrefix
@Deprecated public static java.lang.String addViewPrefix(java.lang.String viewName)
Deprecated.Does not take into account the url converter
-
addViewPrefix
public static java.lang.String addViewPrefix(java.lang.String viewName, java.lang.String controllerName)
-
exposeRequestAttributesAndReturnOldValues
public static java.util.Map<java.lang.String,java.lang.Object> exposeRequestAttributesAndReturnOldValues(javax.servlet.http.HttpServletRequest request, java.util.Map<java.lang.String,?> attributes)
-
cleanupIncludeRequestAttributes
public static void cleanupIncludeRequestAttributes(javax.servlet.http.HttpServletRequest request, java.util.Map<java.lang.String,java.lang.Object> toRestore)
-
exposeIncludeRequestAttributes
public static void exposeIncludeRequestAttributes(javax.servlet.http.HttpServletRequest request)
Expose the current request URI and paths asHttpServletRequestattributes under the keys defined in the Servlet 2.4 specification, for containers that implement 2.3 or an earlier version of the Servlet API:javax.servlet.forward.request_uri,javax.servlet.forward.context_path,javax.servlet.forward.servlet_path,javax.servlet.forward.path_info,javax.servlet.forward.query_string.Does not override values if already present, to not cause conflicts with the attributes exposed by Servlet 2.4+ containers themselves.
- Parameters:
request- current servlet request
-
fromQueryString
public static java.util.Map<java.lang.String,java.lang.Object> fromQueryString(java.lang.String queryString)
Takes a query string and returns the results as a map where the values are either a single entry or a list of values- Parameters:
queryString- The query String- Returns:
- A map
-
toQueryString
public static java.lang.String toQueryString(java.util.Map params, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionConverts the given params into a query string started with ?- Parameters:
params- The paramsencoding- The encoding to use- Returns:
- The query string
- Throws:
java.io.UnsupportedEncodingException- If the given encoding is not supported
-
toQueryString
public static java.lang.String toQueryString(java.util.Map parameters) throws java.io.UnsupportedEncodingExceptionConverts the given parameters to a query string using the default UTF-8 encoding- Parameters:
parameters- The parameters- Returns:
- The query string
- Throws:
java.io.UnsupportedEncodingException- If UTF-8 encoding is not supported
-
getFormatFromURI
public static java.lang.String getFormatFromURI(java.lang.String uri)
Obtains the format from the URI. The format is the string following the . file extension in the last token of the URI. If nothing comes after the ".", this method assumes that there is no format and returnsnull.- Parameters:
uri- The URI- Returns:
- The format or null if none
-
getFormatFromURI
public static java.lang.String getFormatFromURI(java.lang.String uri, grails.web.mime.MimeType[] mimeTypes)Obtains the format from the URI. The format is the string following the . file extension in the last token of the URI. If nothing comes after the ".", this method assumes that there is no format and returnsnull.- Parameters:
uri- The URImimeTypes- The configured mime types- Returns:
- The format or null if none
-
areFileExtensionsEnabled
public static boolean areFileExtensionsEnabled()
Returns the value of the "grails.mime.file.extensions" setting configured in application.groovy- Returns:
- true if file extensions are enabled
-
retrieveGrailsWebRequest
public static GrailsWebRequest retrieveGrailsWebRequest()
Returns the GrailsWebRequest associated with the current request. This is the preferred means of accessing the GrailsWebRequest instance. If the exception is undesired, you can use RequestContextHolder.getRequestAttributes() instead.- Throws:
java.lang.IllegalStateException- if this is called outside of a request.
-
storeGrailsWebRequest
public static void storeGrailsWebRequest(GrailsWebRequest webRequest)
Helper method to store the given GrailsWebRequest for the current request. Ensures consistency between RequestContextHolder and the relevant request attribute. This is the preferred means of updating the current web request.
-
clearGrailsWebRequest
public static void clearGrailsWebRequest()
Removes any GrailsWebRequest instance from the current request.
-
getForwardURI
public static java.lang.String getForwardURI(javax.servlet.http.HttpServletRequest request)
Obtains the forwardURI from the request, since Grails uses a forwarding technique for URL mappings. The actual request URI is held within a request attribute- Parameters:
request- The request- Returns:
- The forward URI
-
isForward
public static boolean isForward(javax.servlet.http.HttpServletRequest request)
Check whether the given request is a forward request- Parameters:
request- The request- Returns:
- True if it is a forward request
-
isAsync
public static boolean isAsync(javax.servlet.http.HttpServletRequest request)
Check whether the given request is a forward request- Parameters:
request- The request- Returns:
- True if it is a forward request
-
isError
public static boolean isError(javax.servlet.http.HttpServletRequest request)
Check whether the given request is a forward request- Parameters:
request- The request- Returns:
- True if it is a forward request
-
isInclude
public static boolean isInclude(javax.servlet.http.HttpServletRequest request)
Check whether the given request is an include request- Parameters:
request- The request- Returns:
- True if it is an include request
-
isForwardOrInclude
public static boolean isForwardOrInclude(javax.servlet.http.HttpServletRequest request)
Check whether the given request is an include or forward request- Parameters:
request- The request- Returns:
- True if it is an include or forward request
-
-