Package io.microsphere.spring.web.util
Class WebRequestUtils
java.lang.Object
io.microsphere.spring.web.util.WebRequestUtils
WebRequest Utilities class- Since:
- 1.0.0
- Author:
- Mercy
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddCookie(org.springframework.web.context.request.NativeWebRequest request, String cookieName, String cookieValue) Adds the specified cookie to the response.static voidaddHeader(org.springframework.web.context.request.NativeWebRequest request, String headerName, String... headerValues) Adds a response header with the given name and value.static ObjectgetBestMatchingHandler(org.springframework.web.context.request.NativeWebRequest request) Get the mapped handler for the best matching pattern.static StringgetBestMatchingPattern(org.springframework.web.context.request.NativeWebRequest request) Get the best matching pattern within the handler mapping.static StringgetContentType(org.springframework.web.context.request.NativeWebRequest request) static StringgetCookieValue(org.springframework.web.context.request.NativeWebRequest request, String cookieName) Get the cookie value for the given cookie name.static StringGet the header value from the request by the given header name.static String[]getHeaderValues(org.springframework.web.context.request.NativeWebRequest request, String headerName) Get the header values from the request by the given header name.getMatrixVariables(org.springframework.web.context.request.NativeWebRequest request) Get a map with URI variable names and a correspondingMultiValueMapof URI matrix variables for each.static StringgetMethod(org.springframework.web.context.request.NativeWebRequest request) Get the HTTP method of the current request.static StringgetPathWithinHandlerMapping(org.springframework.web.context.request.NativeWebRequest request) Get the path within the handler mapping, in case of a pattern match, or the full relevant URI (typically within the DispatcherServlet's mapping) else.static Set<org.springframework.http.MediaType>getProducibleMediaTypes(org.springframework.web.context.request.NativeWebRequest request) Get the set of producible MediaTypes applicable to the mapped handler.static <T> TgetRequestBody(org.springframework.web.context.request.NativeWebRequest request, Class<T> requestBodyType) static StringgetResolvedLookupPath(org.springframework.web.context.request.NativeWebRequest request) Return a previouslyresolvedlookupPath.protected static SpringWebHelpergetSpringWebHelper(org.springframework.web.context.request.NativeWebRequest request) getUriTemplateVariables(org.springframework.web.context.request.NativeWebRequest request) Get the URI templates map, mapping variable names to values.static booleanhasBody(org.springframework.web.context.request.NativeWebRequest request) static booleanisPreFlightRequest(org.springframework.web.context.request.NativeWebRequest request) Returnstrueif the request is a valid CORS pre-flight one by checkingOPTIONSmethod withOriginandAccess-Control-Request-Methodheaders presence.static org.springframework.http.MediaTypeparseContentType(org.springframework.web.context.request.NativeWebRequest request) static voidsetHeader(org.springframework.web.context.request.NativeWebRequest request, String headerName, String headerValue) Sets a response header with the given name and value.static voidwriteResponseBody(org.springframework.web.context.request.NativeWebRequest request, String name, String value) Write value to the response body.
-
Field Details
-
METHOD_HEADER_NAME
The header name for HTTP method- See Also:
-
PATH_ATTRIBUTE
Name of Servlet request attribute that holds aresolvedlookupPath.- Since:
- Spring Framework 5.3
-
-
Method Details
-
getMethod
@Nullable public static String getMethod(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the HTTP method of the current request.- Parameters:
request- theNativeWebRequest- Returns:
- the HTTP method if found, otherwise
null - See Also:
-
HttpServletRequest.getMethod()HttpRequest.getMethod()
-
isPreFlightRequest
public static boolean isPreFlightRequest(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Returnstrueif the request is a valid CORS pre-flight one by checkingOPTIONSmethod withOriginandAccess-Control-Request-Methodheaders presence. -
getContentType
public static String getContentType(@Nonnull org.springframework.web.context.request.NativeWebRequest request) -
parseContentType
@Nullable public static org.springframework.http.MediaType parseContentType(@Nonnull org.springframework.web.context.request.NativeWebRequest request) -
hasBody
public static boolean hasBody(@Nonnull org.springframework.web.context.request.NativeWebRequest request) -
getResolvedLookupPath
public static String getResolvedLookupPath(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Return a previouslyresolvedlookupPath.- Parameters:
request- the current request- Returns:
- the previously resolved lookupPath
- Throws:
IllegalArgumentException- if the lookup path is not found
-
getHeader
@Nullable public static String getHeader(@Nonnull org.springframework.web.context.request.NativeWebRequest request, String headerName) Get the header value from the request by the given header name.- Parameters:
request- theNativeWebRequestheaderName- the name of header- Returns:
- the header value if found, otherwise
null - See Also:
-
HttpServletRequest.getHeader(String)HttpMessage.getHeaders()HttpHeaders.getFirst(String)
-
getHeaderValues
@Nullable public static String[] getHeaderValues(@Nonnull org.springframework.web.context.request.NativeWebRequest request, String headerName) Get the header values from the request by the given header name.- Parameters:
request- theNativeWebRequestheaderName- the name of header- Returns:
- the header values if found, otherwise
null - See Also:
-
HttpServletRequest.getHeaders(String)HttpMessage.getHeaders()HttpHeaders.get(Object)
-
setHeader
public static void setHeader(@Nonnull org.springframework.web.context.request.NativeWebRequest request, @Nullable String headerName, @Nullable String headerValue) Sets a response header with the given name and value. If the header had already been set, the new value overwrites all previous values.- Parameters:
request- theNativeWebRequestheaderName- the name of headerheaderValue- the header value- See Also:
-
HttpServletResponse.setHeader(String, String)HttpHeaders.set(String, String)
-
addHeader
public static void addHeader(@Nonnull org.springframework.web.context.request.NativeWebRequest request, @Nullable String headerName, @Nullable String... headerValues) Adds a response header with the given name and value. This method allows response headers to have multiple values.- Parameters:
request- theNativeWebRequestheaderName- the name of headerheaderValues- the header values- See Also:
-
HttpServletResponse.addHeader(String, String)HttpHeaders.add(String, String)
-
getCookieValue
@Nullable public static String getCookieValue(@Nonnull org.springframework.web.context.request.NativeWebRequest request, String cookieName) Get the cookie value for the given cookie name.- Parameters:
request- theNativeWebRequestcookieName- the name of Cookie- Returns:
- the cookie value if found, otherwise
null
-
addCookie
public static void addCookie(@Nonnull org.springframework.web.context.request.NativeWebRequest request, String cookieName, String cookieValue) Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie.- Parameters:
request- theNativeWebRequestcookieName- the name of CookiecookieValue- the cookie value- See Also:
-
HttpServletResponse.addCookie(Cookie)ServerHttpResponse.addCookie(ResponseCookie)
-
getRequestBody
@Nullable public static <T> T getRequestBody(@Nonnull org.springframework.web.context.request.NativeWebRequest request, Class<T> requestBodyType) -
writeResponseBody
public static void writeResponseBody(@Nonnull org.springframework.web.context.request.NativeWebRequest request, String name, String value) Write value to the response body.- Parameters:
request- theNativeWebRequestname- the namevalue- the value
-
getBestMatchingHandler
@Nullable public static Object getBestMatchingHandler(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the mapped handler for the best matching pattern.- Parameters:
request- theNativeWebRequest- Returns:
- the mapped handler for the best matching pattern if found, otherwise
null - See Also:
-
HandlerMethodorg.springframework.web.servlet.function.RouterFunctionorg.springframework.web.reactive.function.server.RouterFunctionorg.springframework.web.servlet.HandlerMapping#getHandlerorg.springframework.web.reactive.HandlerMapping#getHandlerorg.springframework.web.servlet.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#BEST_MATCHING_HANDLER_ATTRIBUTE
-
getPathWithinHandlerMapping
@Nullable public static String getPathWithinHandlerMapping(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the path within the handler mapping, in case of a pattern match, or the full relevant URI (typically within the DispatcherServlet's mapping) else.- Parameters:
request- theNativeWebRequest- Returns:
- the path within handler mapping if found, otherwise
null - See Also:
-
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposePathWithinMappingorg.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandlerorg.springframework.web.servlet.HandlerMapping#PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
-
getBestMatchingPattern
@Nullable public static String getBestMatchingPattern(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the best matching pattern within the handler mapping.- Parameters:
request- theNativeWebRequest- Returns:
- the best matching pattern within the handler mapping if found, otherwise
null - See Also:
-
RequestMapping.path()org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposePathWithinMappingorg.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandlerorg.springframework.web.servlet.HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#BEST_MATCHING_PATTERN_ATTRIBUTE
-
getUriTemplateVariables
@Nullable public static Map<String,String> getUriTemplateVariables(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the URI templates map, mapping variable names to values.- Parameters:
request- theNativeWebRequest- Returns:
- the URI template variables if found, otherwise
null - See Also:
-
PathVariableorg.springframework.web.servlet.handler.AbstractUrlHandlerMapping#exposeUriTemplateVariablesorg.springframework.web.reactive.handler.AbstractUrlHandlerMapping#lookupHandlerorg.springframework.web.servlet.HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE
-
getMatrixVariables
@Nullable public static Map<String,org.springframework.util.MultiValueMap<String, getMatrixVariablesString>> (org.springframework.web.context.request.NativeWebRequest request) Get a map with URI variable names and a correspondingMultiValueMapof URI matrix variables for each.- Parameters:
request- theNativeWebRequest- Returns:
- the matrix variables if found, otherwise
null - See Also:
-
MatrixVariableorg.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver#resolveArgumentorg.springframework.web.reactive.result.method.annotation.MatrixVariableMapMethodArgumentResolver#resolveArgumentValueorg.springframework.web.servlet.HandlerMapping#MATRIX_VARIABLES_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#MATRIX_VARIABLES_ATTRIBUTE
-
getProducibleMediaTypes
@Nullable public static Set<org.springframework.http.MediaType> getProducibleMediaTypes(@Nonnull org.springframework.web.context.request.NativeWebRequest request) Get the set of producible MediaTypes applicable to the mapped handler.- Parameters:
request- theNativeWebRequest- Returns:
- the producible media types if found, otherwise
null - See Also:
-
RequestMapping.produces()org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatchorg.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping#handleMatchorg.springframework.web.servlet.HandlerMapping#PRODUCIBLE_MEDIA_TYPES_ATTRIBUTEorg.springframework.web.reactive.HandlerMapping#PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE
-
getSpringWebHelper
protected static SpringWebHelper getSpringWebHelper(org.springframework.web.context.request.NativeWebRequest request)
-