Package spark
Class Request
java.lang.Object
spark.Request
- Direct Known Subclasses:
RequestWrapper
Provides information about the HTTP request
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
private byte[]
private static final org.slf4j.Logger
private String
private QueryParamsMap
private javax.servlet.http.HttpServletRequest
private Session
private static final String
private boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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
Modifier and TypeMethodDescription<T> T
Gets the value of the provided attributevoid
Sets an attribute on the request (can be fetched in filters/routes later in the chain)body()
byte[]
protected void
changeMatch
(RouteMatch match) int
Gets cookie by name.cookies()
headers()
Gets the value for the provided headerhost()
private void
ip()
params()
Returns the map containing all route paramsReturns the value of the provided route pattern parameter.pathInfo()
int
port()
protocol()
queryMap()
queryParamOrDefault
(String queryParam, String defaultValue) Gets the query param, or returns default valuequeryParams
(String queryParam) Gets the query paramqueryParamsSafe
(String queryParam) Gets the query param and encode itString[]
queryParamsValues
(String queryParam) Gets all the values of the query param Example: query parameter 'id' from the following request URI: /hello?id=foo&id=barjavax.servlet.http.HttpServletRequest
raw()
private void
scheme()
session()
Returns the current session associated with this request, or if the request does not have a session, creates one.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.String[]
splat()
uri()
url()
(package private) void
validSession
(boolean validSession) Set the session validity
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
USER_AGENT
- See Also:
-
params
-
splat
-
queryMap
-
servletRequest
private javax.servlet.http.HttpServletRequest servletRequest -
session
-
validSession
private boolean validSession -
matchedPath
-
body
-
bodyAsBytes
private byte[] bodyAsBytes -
headers
-
-
Constructor Details
-
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 Details
-
changeMatch
-
params
Returns the map containing all route params- Returns:
- a map containing all route params
-
params
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
- Returns:
- an array containing the splat (wildcard) parameters
-
requestMethod
- Returns:
- request method e.g. GET, POST, PUT, ...
-
scheme
- Returns:
- the scheme
-
host
- Returns:
- the host
-
userAgent
- Returns:
- the user-agent
-
port
public int port()- Returns:
- the server port
-
pathInfo
- Returns:
- the path info Example return: "/example/foo"
-
matchedPath
- Returns:
- the matched route Example return: "/account/:accountId"
-
servletPath
- Returns:
- the servlet path
-
contextPath
- Returns:
- the context path
-
url
- Returns:
- the URL string
-
contentType
- Returns:
- the content type of the body
-
ip
- Returns:
- the client's IP address
-
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
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
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
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
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
Gets the value for the provided header- Parameters:
header
- the header- Returns:
- the value of the provided header
-
queryParams
- Returns:
- all query parameters
-
headers
- Returns:
- all headers
-
queryString
- Returns:
- the query string
-
attribute
Sets an attribute on the request (can be fetched in filters/routes later in the chain)- Parameters:
attribute
- The attributevalue
- The attribute value
-
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
- Returns:
- all attributes
-
raw
public javax.servlet.http.HttpServletRequest raw()- Returns:
- the raw HttpServletRequest object handed in by Jetty
-
queryMap
- Returns:
- the query map
-
queryMap
- Parameters:
key
- the key- Returns:
- the query map
-
initQueryMap
private void initQueryMap() -
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
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
- Returns:
- request cookies (or empty Map if cookies aren't present)
-
cookie
Gets cookie by name.- Parameters:
name
- name of the cookie- Returns:
- cookie value or null if the cookie was not found
-
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
- Returns:
- Returns the name and version of the protocol the request uses
-
getParams
-
getSplat
-
validSession
void validSession(boolean validSession) Set the session validity- Parameters:
validSession
- the session validity
-