Package spark
Class Request
- java.lang.Object
-
- spark.Request
-
- Direct Known Subclasses:
RequestWrapper
public class Request extends java.lang.ObjectProvides information about the HTTP request
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringbodyprivate byte[]bodyAsBytesprivate java.util.Set<java.lang.String>headersprivate static org.slf4j.LoggerLOGprivate java.lang.StringmatchedPathprivate java.util.Map<java.lang.String,java.lang.String>paramsprivate QueryParamsMapqueryMapprivate javax.servlet.http.HttpServletRequestservletRequestprivate Sessionsessionprivate java.util.List<java.lang.String>splatprivate static java.lang.StringUSER_AGENTprivate booleanvalidSession
-
Constructor Summary
Constructors Modifier Constructor Description protectedRequest()(package private)Request(javax.servlet.http.HttpServletRequest request)Constructor - Used to create a request and no RouteMatch is available.(package private)Request(RouteMatch match, javax.servlet.http.HttpServletRequest request)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Tattribute(java.lang.String attribute)Gets the value of the provided attributevoidattribute(java.lang.String attribute, java.lang.Object value)Sets an attribute on the request (can be fetched in filters/routes later in the chain)java.util.Set<java.lang.String>attributes()java.lang.Stringbody()byte[]bodyAsBytes()protected voidchangeMatch(RouteMatch match)intcontentLength()java.lang.StringcontentType()java.lang.StringcontextPath()java.lang.Stringcookie(java.lang.String name)Gets cookie by name.java.util.Map<java.lang.String,java.lang.String>cookies()private static java.util.Map<java.lang.String,java.lang.String>getParams(java.util.List<java.lang.String> request, java.util.List<java.lang.String> matched)private static java.util.List<java.lang.String>getSplat(java.util.List<java.lang.String> request, java.util.List<java.lang.String> matched)java.util.Set<java.lang.String>headers()java.lang.Stringheaders(java.lang.String header)Gets the value for the provided headerjava.lang.Stringhost()private voidinitQueryMap()java.lang.Stringip()java.lang.StringmatchedPath()java.util.Map<java.lang.String,java.lang.String>params()Returns the map containing all route paramsjava.lang.Stringparams(java.lang.String param)Returns the value of the provided route pattern parameter.java.lang.StringpathInfo()intport()java.lang.Stringprotocol()QueryParamsMapqueryMap()QueryParamsMapqueryMap(java.lang.String key)java.lang.StringqueryParamOrDefault(java.lang.String queryParam, java.lang.String defaultValue)Gets the query param, or returns default valuejava.util.Set<java.lang.String>queryParams()java.lang.StringqueryParams(java.lang.String queryParam)Gets the query paramjava.lang.StringqueryParamsSafe(java.lang.String queryParam)Gets the query param and encode itjava.lang.String[]queryParamsValues(java.lang.String queryParam)Gets all the values of the query param Example: query parameter 'id' from the following request URI: /hello?id=foo&id=barjava.lang.StringqueryString()javax.servlet.http.HttpServletRequestraw()private voidreadBodyAsBytes()java.lang.StringrequestMethod()java.lang.Stringscheme()java.lang.StringservletPath()Sessionsession()Returns the current session associated with this request, or if the request does not have a session, creates one.Sessionsession(boolean create)Returns the current session associated with this request, or if there is no current session andcreateis true, returns a new session.java.lang.String[]splat()java.lang.Stringuri()java.lang.Stringurl()java.lang.StringuserAgent()(package private) voidvalidSession(boolean validSession)Set the session validity
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
USER_AGENT
private static final java.lang.String USER_AGENT
- See Also:
- Constant Field Values
-
params
private java.util.Map<java.lang.String,java.lang.String> params
-
splat
private java.util.List<java.lang.String> splat
-
queryMap
private QueryParamsMap queryMap
-
servletRequest
private javax.servlet.http.HttpServletRequest servletRequest
-
session
private Session session
-
validSession
private boolean validSession
-
matchedPath
private java.lang.String matchedPath
-
body
private java.lang.String body
-
bodyAsBytes
private byte[] bodyAsBytes
-
headers
private java.util.Set<java.lang.String> headers
-
-
Constructor Detail
-
Request
protected Request()
-
Request
Request(RouteMatch match, javax.servlet.http.HttpServletRequest request)
Constructor- Parameters:
match- the route matchrequest- the servlet request
-
Request
Request(javax.servlet.http.HttpServletRequest request)
Constructor - Used to create a request and no RouteMatch is available.- Parameters:
request- the servlet request
-
-
Method Detail
-
changeMatch
protected void changeMatch(RouteMatch match)
-
params
public java.util.Map<java.lang.String,java.lang.String> params()
Returns the map containing all route params- Returns:
- a map containing all route params
-
params
public java.lang.String params(java.lang.String param)
Returns the value of the provided route pattern parameter. Example: parameter 'name' from the following pattern: (get '/hello/:name')- Parameters:
param- the param- Returns:
- null if the given param is null or not found
-
splat
public java.lang.String[] splat()
- Returns:
- an array containing the splat (wildcard) parameters
-
requestMethod
public java.lang.String requestMethod()
- Returns:
- request method e.g. GET, POST, PUT, ...
-
scheme
public java.lang.String scheme()
- Returns:
- the scheme
-
host
public java.lang.String host()
- Returns:
- the host
-
userAgent
public java.lang.String userAgent()
- Returns:
- the user-agent
-
port
public int port()
- Returns:
- the server port
-
pathInfo
public java.lang.String pathInfo()
- Returns:
- the path info Example return: "/example/foo"
-
matchedPath
public java.lang.String matchedPath()
- Returns:
- the matched route Example return: "/account/:accountId"
-
servletPath
public java.lang.String servletPath()
- Returns:
- the servlet path
-
contextPath
public java.lang.String contextPath()
- Returns:
- the context path
-
url
public java.lang.String url()
- Returns:
- the URL string
-
contentType
public java.lang.String contentType()
- Returns:
- the content type of the body
-
ip
public java.lang.String ip()
- Returns:
- the client's IP address
-
body
public java.lang.String body()
- Returns:
- the request body sent by the client
-
bodyAsBytes
public byte[] bodyAsBytes()
-
readBodyAsBytes
private void readBodyAsBytes()
-
contentLength
public int contentLength()
- Returns:
- the length of request.body
-
queryParams
public java.lang.String queryParams(java.lang.String queryParam)
Gets the query param- Parameters:
queryParam- the query parameter- Returns:
- the value of the provided queryParam Example: query parameter 'id' from the following request URI: /hello?id=foo
-
queryParamsSafe
public java.lang.String queryParamsSafe(java.lang.String queryParam)
Gets the query param and encode it- Parameters:
queryParam- the query parameter- Returns:
- the encode value of the provided queryParam Example: query parameter 'me' from the URI: /hello?id=fool.
-
queryParamOrDefault
public java.lang.String queryParamOrDefault(java.lang.String queryParam, java.lang.String defaultValue)Gets the query param, or returns default value- Parameters:
queryParam- the query parameterdefaultValue- the default value- Returns:
- the value of the provided queryParam, or default if value is null Example: query parameter 'id' from the following request URI: /hello?id=foo
-
queryParamsValues
public java.lang.String[] queryParamsValues(java.lang.String queryParam)
Gets all the values of the query param Example: query parameter 'id' from the following request URI: /hello?id=foo&id=bar- Parameters:
queryParam- the query parameter- Returns:
- the values of the provided queryParam, null if it doesn't exists
-
headers
public java.lang.String headers(java.lang.String header)
Gets the value for the provided header- Parameters:
header- the header- Returns:
- the value of the provided header
-
queryParams
public java.util.Set<java.lang.String> queryParams()
- Returns:
- all query parameters
-
headers
public java.util.Set<java.lang.String> headers()
- Returns:
- all headers
-
queryString
public java.lang.String queryString()
- Returns:
- the query string
-
attribute
public void attribute(java.lang.String attribute, java.lang.Object value)Sets an attribute on the request (can be fetched in filters/routes later in the chain)- Parameters:
attribute- The attributevalue- The attribute value
-
attribute
public <T> T attribute(java.lang.String attribute)
Gets the value of the provided attribute- Type Parameters:
T- the type parameter.- Parameters:
attribute- The attribute value or null if not present- Returns:
- the value for the provided attribute
-
attributes
public java.util.Set<java.lang.String> attributes()
- Returns:
- all attributes
-
raw
public javax.servlet.http.HttpServletRequest raw()
- Returns:
- the raw HttpServletRequest object handed in by Jetty
-
queryMap
public QueryParamsMap queryMap()
- Returns:
- the query map
-
queryMap
public QueryParamsMap queryMap(java.lang.String key)
- Parameters:
key- the key- Returns:
- the query map
-
initQueryMap
private void initQueryMap()
-
session
public Session session()
Returns the current session associated with this request, or if the request does not have a session, creates one.- Returns:
- the session associated with this request
-
session
public Session session(boolean create)
Returns the current session associated with this request, or if there is no current session andcreateis true, returns a new session.- Parameters:
create-trueto create a new session for this request if necessary;falseto return null if there's no current session- Returns:
- the session associated with this request or
nullifcreateisfalseand the request has no valid session
-
cookies
public java.util.Map<java.lang.String,java.lang.String> cookies()
- Returns:
- request cookies (or empty Map if cookies aren't present)
-
cookie
public java.lang.String cookie(java.lang.String name)
Gets cookie by name.- Parameters:
name- name of the cookie- Returns:
- cookie value or null if the cookie was not found
-
uri
public java.lang.String uri()
- Returns:
- the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
-
protocol
public java.lang.String protocol()
- Returns:
- Returns the name and version of the protocol the request uses
-
getParams
private static java.util.Map<java.lang.String,java.lang.String> getParams(java.util.List<java.lang.String> request, java.util.List<java.lang.String> matched)
-
getSplat
private static java.util.List<java.lang.String> getSplat(java.util.List<java.lang.String> request, java.util.List<java.lang.String> matched)
-
validSession
void validSession(boolean validSession)
Set the session validity- Parameters:
validSession- the session validity
-
-