aerogear-controller 1.0.1

org.jboss.aerogear.controller.util
Class RequestUtils

java.lang.Object
  extended by org.jboss.aerogear.controller.util.RequestUtils

public class RequestUtils
extends Object

Utility methods for various HttpServletRequest operation.


Field Summary
private static Pattern ACCEPT_HEADER_PATTERN
           
private static String MEDIA_RANGE
           
private static Pattern PATH_PATTERN
           
private static Pattern PATH_PLACEHOLDER_PATTERN
           
private static Pattern PATH_SEGMENT_PATTERN
           
private static Pattern PLACEHOLDER_PATTERN
           
 
Constructor Summary
private RequestUtils()
           
 
Method Summary
static boolean acceptsMediaType(Set<String> acceptHeaders, Set<MediaType> produces)
          Determines if a request's 'Accept' header media types are compatible with the media types that a route produces.
static Set<String> extractAcceptHeader(javax.servlet.http.HttpServletRequest request)
          Returns the Accept header from the passed-in HttpServletRequest.
static RequestMethod extractMethod(javax.servlet.http.HttpServletRequest httpServletRequest)
          Returns the RequestMethod for the passed-in HttpServletRequest.
static String extractPath(javax.servlet.http.HttpServletRequest request)
          Returns the path of the current request with out the context path.
static Map<Integer,String> extractPathSegments(String path)
          Extracts the path elements and returns a map indexed by the order in which the path elements appear.
static Map<String,Integer> extractPathVariableNames(String path)
          Extracts path parameter placeholders from the passed in path.
static Set<String> extractPlaceHolders(String str)
          Will extract any placeholders, {name}, from the passed-in string.
static com.google.common.base.Optional<MediaType> getAcceptedMediaType(Set<String> acceptHeaders, Set<MediaType> produces)
          Gets the media type that is compatible with the requested media type and the media types that a route produces.
static String injectParamValues(String str, Map<String,Object> map)
          Injects/replaces the placeholders in the passed-in string with the corresponding values from the passed-in map.
static Map<String,String> mapPathParams(String requestPath, String configPath)
          Extracts path parameters from the passed-in request path.
static List<String> pathSegements(String path)
          Returns a List of segments for the passed-in path.
static boolean segmentsMatch(String placeHolderPath, String realPath)
          Determines whether to paths match in terms of having the same number of segments and the same segment names.
private static String trimPlaceHolder(String str)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PATH_SEGMENT_PATTERN

private static final Pattern PATH_SEGMENT_PATTERN

PATH_PLACEHOLDER_PATTERN

private static final Pattern PATH_PLACEHOLDER_PATTERN

PLACEHOLDER_PATTERN

private static final Pattern PLACEHOLDER_PATTERN

PATH_PATTERN

private static final Pattern PATH_PATTERN

MEDIA_RANGE

private static final String MEDIA_RANGE
See Also:
Constant Field Values

ACCEPT_HEADER_PATTERN

private static final Pattern ACCEPT_HEADER_PATTERN
Constructor Detail

RequestUtils

private RequestUtils()
Method Detail

extractPath

public static String extractPath(javax.servlet.http.HttpServletRequest request)
Returns the path of the current request with out the context path.

For example, if the web application was deployed with a context path of '/myapp', and the request submitted was '/myapp/cars/12', this method would return '/cars/12'.

Parameters:
request - the HttpServletRequest.
Returns:
String the request path without the context path (suffix)

extractMethod

public static RequestMethod extractMethod(javax.servlet.http.HttpServletRequest httpServletRequest)
Returns the RequestMethod for the passed-in HttpServletRequest.

Parameters:
httpServletRequest - the HttpServletRequest
Returns:
RequestMethod matching the Http Method of the request.

extractAcceptHeader

public static Set<String> extractAcceptHeader(javax.servlet.http.HttpServletRequest request)
Returns the Accept header from the passed-in HttpServletRequest.

Parameters:
request - the HttpServletRequest
Returns:
Set<String> of the values of the Http Accept Header, or an empty list if there was not Accept header

extractPathSegments

public static Map<Integer,String> extractPathSegments(String path)
Extracts the path elements and returns a map indexed by the order in which the path elements appear.

Parameters:
path - the path from which the path elements will be extracted/collected.
Returns:
Map<Integer, String indexed by the position of the path elements.

extractPathVariableNames

public static Map<String,Integer> extractPathVariableNames(String path)
Extracts path parameter placeholders from the passed in path.

Parameters:
path - the path from which the path elements will be extracted/collected.
Returns:
Map<String, Integer indexed name of the placeholder/variable name, and the value is the position of this name in the path.

trimPlaceHolder

private static String trimPlaceHolder(String str)

mapPathParams

public static Map<String,String> mapPathParams(String requestPath,
                                               String configPath)
Extracts path parameters from the passed-in request path.

Parameters:
requestPath - the actual request path.
configPath - the configuration time path. This path can contain path variable placeholders which will be mapped to the corresponding request path values.
Returns:
Map<String, String> index by the name of the parameter, and the value is the value of the corresponding request path parameter.

acceptsMediaType

public static boolean acceptsMediaType(Set<String> acceptHeaders,
                                       Set<MediaType> produces)
Determines if a request's 'Accept' header media types are compatible with the media types that a route produces.

Parameters:
acceptHeaders - the 'Accept' header media types for the current request.
produces - the MediaTypes that the route is capable of producing.
Returns:
true if the one of the accept media types are compatible with on of the media types.

getAcceptedMediaType

public static com.google.common.base.Optional<MediaType> getAcceptedMediaType(Set<String> acceptHeaders,
                                                                              Set<MediaType> produces)
Gets the media type that is compatible with the requested media type and the media types that a route produces.

Parameters:
acceptHeaders - the 'Accept' header media types for the current request.
produces - the MediaTypes that the route is capable of producing.
Returns:
Optional<MediaType an Optional instance of the MediaType accepted.

extractPlaceHolders

public static Set<String> extractPlaceHolders(String str)
Will extract any placeholders, {name}, from the passed-in string.

Parameters:
str - the string from with placeholder names should be extracted.
Returns:
Set containing the placeholder names.

injectParamValues

public static String injectParamValues(String str,
                                       Map<String,Object> map)
Injects/replaces the placeholders in the passed-in string with the corresponding values from the passed-in map.

Parameters:
str - the string containing placeholders that are to be replaced by values in the map
map - the map containing the mapping of param name to param value.
Returns:
String the string with the placesholders replaced by the values.

segmentsMatch

public static boolean segmentsMatch(String placeHolderPath,
                                    String realPath)
Determines whether to paths match in terms of having the same number of segments and the same segment names.

Parameters:
placeHolderPath - path that may contain placeholders.
realPath - the actual path of the request.
Returns:
true if the number of segments match and the segments names match.

pathSegements

public static List<String> pathSegements(String path)
Returns a List of segments for the passed-in path.

Parameters:
path - which can include placeholders which will be included as-is, for example {placeholderName}
Returns:
List<String> containing the segments of the path.

aerogear-controller 1.0.1

Copyright © 2013 JBoss, a division of Red Hat, Inc.. All Rights Reserved.