Package spark
Class Request
- java.lang.Object
-
- spark.Request
-
- Direct Known Subclasses:
RequestWrapper
public class Request extends java.lang.Object
Provides information about the HTTP request
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
body
private byte[]
bodyAsBytes
private java.util.Set<java.lang.String>
headers
private static org.slf4j.Logger
LOG
private java.lang.String
matchedPath
private java.util.Map<java.lang.String,java.lang.String>
params
private QueryParamsMap
queryMap
private javax.servlet.http.HttpServletRequest
servletRequest
private Session
session
private java.util.List<java.lang.String>
splat
private static java.lang.String
USER_AGENT
private boolean
validSession
-
Constructor Summary
Constructors Modifier Constructor Description protected
Request()
(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> T
attribute(java.lang.String attribute)
Gets the value of the provided attributevoid
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)java.util.Set<java.lang.String>
attributes()
java.lang.String
body()
byte[]
bodyAsBytes()
protected void
changeMatch(RouteMatch match)
int
contentLength()
java.lang.String
contentType()
java.lang.String
contextPath()
java.lang.String
cookie(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.String
headers(java.lang.String header)
Gets the value for the provided headerjava.lang.String
host()
private void
initQueryMap()
java.lang.String
ip()
java.lang.String
matchedPath()
java.util.Map<java.lang.String,java.lang.String>
params()
Returns the map containing all route paramsjava.lang.String
params(java.lang.String param)
Returns the value of the provided route pattern parameter.java.lang.String
pathInfo()
int
port()
java.lang.String
protocol()
QueryParamsMap
queryMap()
QueryParamsMap
queryMap(java.lang.String key)
java.lang.String
queryParamOrDefault(java.lang.String queryParam, java.lang.String defaultValue)
Gets the query param, or returns default valuejava.util.Set<java.lang.String>
queryParams()
java.lang.String
queryParams(java.lang.String queryParam)
Gets the query paramjava.lang.String
queryParamsSafe(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.String
queryString()
javax.servlet.http.HttpServletRequest
raw()
private void
readBodyAsBytes()
java.lang.String
requestMethod()
java.lang.String
scheme()
java.lang.String
servletPath()
Session
session()
Returns the current session associated with this request, or if the request does not have a session, creates one.Session
session(boolean create)
Returns the current session associated with this request, or if there is no current session andcreate
is true, returns a new session.java.lang.String[]
splat()
java.lang.String
uri()
java.lang.String
url()
java.lang.String
userAgent()
(package private) void
validSession(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 andcreate
is true, returns a new session.- Parameters:
create
-true
to create a new session for this request if necessary;false
to return null if there's no current session- Returns:
- the session associated with this request or
null
ifcreate
isfalse
and 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
-
-