Package ninja
Interface Context
-
- All Known Subinterfaces:
Context.Impl
- All Known Implementing Classes:
AbstractContext
,WrappedContext
public interface Context
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Context.HTTP_STATUS
Deprecated.static interface
Context.Impl
Impl is used to hide stuff that a user should not see on code completion.
-
Field Summary
Fields Modifier and Type Field Description static String
CONTENT_TYPE
Content-Type: ...static String
NINJA_PROPERTIES_X_FORWARDED_FOR
Used to enable or disable usage of X-Forwarded-For header in getRemoteAddr().static String
X_FORWARD_HEADER
X Forwarded for header, used when behind fire walls and proxies.
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description void
addCookie(Cookie cookie)
Adds a cookie to the responsevoid
asyncRequestComplete()
Indicate that processing this request is complete.void
cleanup()
Cleanup contextResult
controllerReturned()
ResponseStreams
finalizeHeaders(Result result)
Finalizing the headers copies all stuff into the headers.ResponseStreams
finalizeHeadersWithoutFlashAndSessionCookie(Result result)
Finalizing the headers copies all stuff into the headers.String
getAcceptCharset()
Get the charset that is acceptable for the client.String
getAcceptContentType()
Get the content type that is acceptable for the client.String
getAcceptEncoding()
Get the encoding that is acceptable for the client.String
getAcceptLanguage()
Get the language that is acceptable for the client.Object
getAttribute(String name)
Gets an attribute value previously set bysetAttribute(java.lang.String, java.lang.Object)
.<T> T
getAttribute(String name, Class<T> clazz)
Gets an attribute value previously set bysetAttribute(java.lang.String, java.lang.Object)
.Map<String,Object>
getAttributes()
Get all the attributes from the requestString
getContextPath()
Get the context path on which the application is running That means: - when running on root the context path is empty - when running on context there is NEVER a trailing slash We conform to the following rules: Returns the portion of the request URI that indicates the context of the request.Cookie
getCookie(String cookieName)
Get cookie from context.List<Cookie>
getCookies()
Get all cookies from the context.String
getCookieValue(String name)
Get the cookie value from the request, if definedorg.apache.commons.fileupload.FileItemIterator
getFileItemIterator()
Deprecated.This method is kept for backward compatibility, useFileProvider
to specify whichFileItemProvider
should be used to handle uploaded files, and access them usinggetParameterAsFileItem(String)
.FlashScope
getFlashCookie()
Deprecated.FlashScope
getFlashScope()
Returns the flash cookie.String
getHeader(String name)
Get the (first) request header with the given nameMap<String,List<String>>
getHeaders()
Get all the headers from the requestList<String>
getHeaders(String name)
Get all the request headers with the given name.String
getHostname()
Returns the hostname as seen by the server.InputStream
getInputStream()
Get the input stream to read the request.String
getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.String
getParameter(String name)
Get the parameter with the given key from the request.String
getParameter(String name, String defaultValue)
Same likegetParameter(String)
, but returns given defaultValue instead of null in case parameter cannot be found.<T> T
getParameterAs(String name, Class<T> clazz)
Same likegetParameter(String)
, but converts the parameter to Class type if found.<T> T
getParameterAs(String name, Class<T> clazz, T defaultValue)
Same likegetParameter(String, String)
, but converts the parameter to Class type if found.FileItem
getParameterAsFileItem(String name)
Same likegetParameter(String, String)
, but converts the parameter to File if found.List<FileItem>
getParameterAsFileItems(String name)
Get the files parameter with the given key from the request.Integer
getParameterAsInteger(String name)
Same likegetParameter(String)
, but converts the parameter to Integer if found.Integer
getParameterAsInteger(String name, Integer defaultValue)
Same likegetParameter(String, String)
, but converts the parameter to Integer if found.Map<String,List<FileItem>>
getParameterFileItems()
Get all the file parameters from the requestMap<String,String[]>
getParameters()
Get all the parameters from the requestList<String>
getParameterValues(String name)
Get the parameter with the given key from the request.String
getPathParameter(String name)
Get the path parameter for the given key.Integer
getPathParameterAsInteger(String key)
Get the path parameter for the given key and convert it to Integer.String
getPathParameterEncoded(String name)
Get the path parameter for the given key.BufferedReader
getReader()
Get the reader to read the request.String
getRemoteAddr()
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.String
getRequestContentType()
The Content-Type header field indicates the media type of the request body sent to the recipient.String
getRequestParameterString()
Returns the parameter string that is contained in the request URL after the path.String
getRequestPath()
Returns the path that Ninja should act upon.String
getRequestUri()
Deprecated.Route
getRoute()
Get the route for this contextString
getScheme()
For instance: http://example.com/index returns "http".Session
getSession()
Returns the client side session.Session
getSessionCookie()
Deprecated.Validation
getValidation()
Get the validation contextvoid
handleAsync()
Indicate that this request is going to be handled asynchronouslyboolean
hasCookie(String cookieName)
Checks whether the context contains a given cookie.boolean
isAsync()
boolean
isMultipart()
Check if request is of type multipart.boolean
isRequestJson()
Check to see if the request content type is JSON.boolean
isRequestXml()
Check to see if the request content type is XML.<T> T
parseBody(Class<T> classOfT)
This will give you the request body nicely parsed.void
returnResultAsync(Result result)
Indicate that request processing of an async request is complete.void
setAttribute(String name, Object value)
Sets an attribute value.void
unsetCookie(Cookie cookie)
Removes a cookie from the response
-
-
-
Field Detail
-
CONTENT_TYPE
static final String CONTENT_TYPE
Content-Type: ... parameter for response.- See Also:
- Constant Field Values
-
X_FORWARD_HEADER
static final String X_FORWARD_HEADER
X Forwarded for header, used when behind fire walls and proxies.- See Also:
- Constant Field Values
-
NINJA_PROPERTIES_X_FORWARDED_FOR
static final String NINJA_PROPERTIES_X_FORWARDED_FOR
Used to enable or disable usage of X-Forwarded-For header in getRemoteAddr(). Can be set in application.conf to true or false. If not set it's assumed to be false;- See Also:
- Constant Field Values
-
-
Method Detail
-
getRequestContentType
String getRequestContentType()
The Content-Type header field indicates the media type of the request body sent to the recipient. E.g.Content-Type: text/html; charset=ISO-8859-4
- Returns:
- the content type of the incoming request.
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getRequestUri
@Deprecated String getRequestUri()
Deprecated.Returns the uri as seen by the server. http://example.com/index would return "/index". This is ambiguous. Please use the new method getRequestPath. It will also take care of any prefixes and contexts set by your servlet container- Returns:
- the uri as seen by the server
-
getRequestParameterString
String getRequestParameterString()
Returns the parameter string that is contained in the request URL after the path.
Example: For the URL "http://domain.local/?key=private&hash=%93%af%f2%3b", this method will return "key=private&hash=%93%af%f2%3b"- Returns:
- The parameter string
-
getHostname
String getHostname()
Returns the hostname as seen by the server. http://example.com/index would return "example.com".- Returns:
- the host name as seen by the server
-
getScheme
String getScheme()
For instance: http://example.com/index returns "http". https://example.com/index returns "https".- Returns:
- the scheme of the request as seen by the server (e.g. "http" or "https").
-
getRemoteAddr
String getRemoteAddr()
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variableREMOTE_ADDR
. To honour the X-Forwarded-For flag make sure you set "ninja.x_forwarded_for_enabled=true" in your application.conf. Default behavior is NOT to take X-Forwarded-For flag into account.- Returns:
- a
String
containing the IP address of the client that sent the request. Takes into account X-Forwarded-For header if configured to do so.
-
getRequestPath
String getRequestPath()
Returns the path that Ninja should act upon. For instance in servlets you could have soemthing like a context prefix. /myContext/app If your route only defines /app it will work as the requestpath will return only "/app". A context path is not returned. It does NOT decode any parts of the url. Interesting reads: - http://www.lunatech-research.com/archives/2009/02/03/ what-every-web-developer-must-know-about-url-encoding - http://stackoverflow .com/questions/966077/java-reading-undecoded-url-from-servlet- Returns:
- The the path as seen by the server. Does exclude any container set context prefixes. Not decoded.
-
getFlashScope
FlashScope getFlashScope()
Returns the flash cookie. Flash cookies only live for one request. Good uses are error messages to display. Almost everything else is bad use of Flash Cookies. A FlashScope is usually not signed. Don't trust the content.- Returns:
- the flash scope of that request.
-
getFlashCookie
@Deprecated FlashScope getFlashCookie()
Deprecated.Deprecated => please use getFlashScope()- Returns:
- FlashScope of this request.
-
getSession
Session getSession()
Returns the client side session. It is a cookie. Therefore you cannot store a lot of information inside the cookie. This is by intention. If you have the feeling that the session cookie is too small for what you want to achieve thing again. Most likely your design is wrong.- Returns:
- the Session of that request / response cycle.
-
getSessionCookie
@Deprecated Session getSessionCookie()
Deprecated.Deprecated => please use getSession();- Returns:
- the Session of that request / response cycle.
-
getCookie
Cookie getCookie(String cookieName)
Get cookie from context.- Parameters:
cookieName
- Name of the cookie to retrieve- Returns:
- the cookie with that name or null.
-
hasCookie
boolean hasCookie(String cookieName)
Checks whether the context contains a given cookie.- Parameters:
cookieName
- Name of the cookie to check for- Returns:
true
if the context has a cookie with that name.
-
getCookies
List<Cookie> getCookies()
Get all cookies from the context.- Returns:
- the cookie with that name or null.
-
getContextPath
String getContextPath()
Get the context path on which the application is running That means: - when running on root the context path is empty - when running on context there is NEVER a trailing slash We conform to the following rules: Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string. As outlined by: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getContextPath()- Returns:
- the context-path with a leading "/" or "" if running on root
-
getParameter
String getParameter(String name)
Get the parameter with the given key from the request. The parameter may either be a query parameter, or in the case of form submissions, may be a form parameter.When the parameter is multivalued, returns the first value.
The parameter is decoded by default.
- Parameters:
name
- The key of the parameter- Returns:
- The value, or null if no parameter was found.
- See Also:
getParameterValues(java.lang.String)
-
getParameterValues
List<String> getParameterValues(String name)
Get the parameter with the given key from the request. The parameter may either be a query parameter, or in the case of form submissions, may be a form parameter.The parameter is decoded by default.
- Parameters:
name
- The key of the parameter- Returns:
- The values, possibly an empty list.
-
getParameter
String getParameter(String name, String defaultValue)
Same likegetParameter(String)
, but returns given defaultValue instead of null in case parameter cannot be found. The parameter is decoded by default.- Parameters:
name
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.- Returns:
- The value of the parameter of the defaultValue if not found.
-
getParameterAsInteger
Integer getParameterAsInteger(String name)
Same likegetParameter(String)
, but converts the parameter to Integer if found. The parameter is decoded by default.- Parameters:
name
- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAsInteger
Integer getParameterAsInteger(String name, Integer defaultValue)
Same likegetParameter(String, String)
, but converts the parameter to Integer if found. The parameter is decoded by default.- Parameters:
name
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.- Returns:
- The value of the parameter of the defaultValue if not found.
-
getParameterAsFileItem
FileItem getParameterAsFileItem(String name)
Same likegetParameter(String, String)
, but converts the parameter to File if found. The parameter is read from the multipart stream, using the FileProvider declared on the route method, class.- Parameters:
name
- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAsFileItems
List<FileItem> getParameterAsFileItems(String name)
Get the files parameter with the given key from the request.- Parameters:
name
- The key of the parameter- Returns:
- The values, possibly an empty list.
-
getParameterFileItems
Map<String,List<FileItem>> getParameterFileItems()
Get all the file parameters from the request- Returns:
- The parameters, possibly a null map.
-
getParameterAs
<T> T getParameterAs(String name, Class<T> clazz)
Same likegetParameter(String)
, but converts the parameter to Class type if found. The parameter is decoded by default.- Parameters:
name
- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAs
<T> T getParameterAs(String name, Class<T> clazz, T defaultValue)
Same likegetParameter(String, String)
, but converts the parameter to Class type if found. The parameter is decoded by default.- Parameters:
name
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.- Returns:
- The value of the parameter of the defaultValue if not found.
-
getPathParameter
String getPathParameter(String name)
Get the path parameter for the given key. The parameter will be decoded based on the RFCs. Check out http://docs.oracle.com/javase/6/docs/api/java/net/URI.html for more information.- Parameters:
name
- The name of the path parameter in a route. Eg /{myName}/rest/of/url- Returns:
- The decoded path parameter, or null if no such path parameter was found.
-
getPathParameterEncoded
String getPathParameterEncoded(String name)
Get the path parameter for the given key. Returns the raw path part. That means you can get stuff like: blue%2Fred%3Fand+green- Parameters:
name
- The name of the path parameter in a route. Eg /{myName}/rest/of/url- Returns:
- The encoded (!) path parameter, or null if no such path parameter was found.
-
getPathParameterAsInteger
Integer getPathParameterAsInteger(String key)
Get the path parameter for the given key and convert it to Integer. The parameter will be decoded based on the RFCs. Check out http://docs.oracle.com/javase/6/docs/api/java/net/URI.html for more information.- Parameters:
key
- the key of the path parameter- Returns:
- the numeric path parameter, or null of no such path parameter is defined, or if it cannot be parsed to int
-
getParameters
Map<String,String[]> getParameters()
Get all the parameters from the request- Returns:
- The parameters
-
getHeader
String getHeader(String name)
Get the (first) request header with the given name- Returns:
- The header value or null if not found.
-
getHeaders
List<String> getHeaders(String name)
Get all the request headers with the given name.- Returns:
- the header values. Returns empty list if none are found.
-
getHeaders
Map<String,List<String>> getHeaders()
Get all the headers from the request- Returns:
- The headers
-
getCookieValue
String getCookieValue(String name)
Get the cookie value from the request, if defined- Parameters:
name
- The name of the cookie- Returns:
- The cookie value, or null if the cookie was not found
-
parseBody
<T> T parseBody(Class<T> classOfT)
This will give you the request body nicely parsed. You can register your own parsers depending on the request type. Have a look atBodyParserEngine
BodyParserEngineJson
BodyParserEngineXml
andBodyParserEngineManager
- Parameters:
classOfT
- The class of the result.- Returns:
- The parsed request or null if something went wrong.
-
isAsync
boolean isAsync()
-
handleAsync
void handleAsync()
Indicate that this request is going to be handled asynchronously
-
returnResultAsync
void returnResultAsync(Result result)
Indicate that request processing of an async request is complete.
-
asyncRequestComplete
void asyncRequestComplete()
Indicate that processing this request is complete.
-
controllerReturned
Result controllerReturned()
-
finalizeHeaders
ResponseStreams finalizeHeaders(Result result)
Finalizing the headers copies all stuff into the headers. It of course also handles Ninja session and Flash information. After finalizing the headers you can access the responseStreams.
-
finalizeHeadersWithoutFlashAndSessionCookie
ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result)
Finalizing the headers copies all stuff into the headers. After finalizing the headers you can access the responseStreams. This method does not set any Ninja session of flash information. Eg. When serving static assets this is the method you may want to use. Otherwise you'd get a race condition with a lot of requests setting scopes and deleting them immediately.
-
getInputStream
InputStream getInputStream() throws IOException
Get the input stream to read the request. Must not be used if getReader has been called.- Returns:
- The input stream
- Throws:
IOException
-
getReader
BufferedReader getReader() throws IOException
Get the reader to read the request. Must not be used if getInputStream has been called.- Returns:
- The reader
- Throws:
IOException
-
getRoute
Route getRoute()
Get the route for this context- Returns:
- The route
-
isMultipart
boolean isMultipart()
Check if request is of type multipart. Important when you want to process uploads for instance. Also check out: http://commons.apache.org/fileupload/streaming.html- Returns:
- true if request is of type multipart.
-
getFileItemIterator
@Deprecated org.apache.commons.fileupload.FileItemIterator getFileItemIterator()
Deprecated.This method is kept for backward compatibility, useFileProvider
to specify whichFileItemProvider
should be used to handle uploaded files, and access them usinggetParameterAsFileItem(String)
.Gets the FileItemIterator of the input. Can be used to process uploads in a streaming fashion. Check out: http://commons.apache.org/fileupload/streaming.html- Returns:
- the FileItemIterator of the request or null if there was an error.
-
getValidation
Validation getValidation()
Get the validation context- Returns:
- The validation context
-
getAcceptContentType
String getAcceptContentType()
Get the content type that is acceptable for the client. (in this order : {@see Result.TEXT_HTML} > {@see Result.APPLICATION_XML} > {@see Result.APPLICATION_JSON} > {@see Result.TEXT_PLAIN} > {@see Result.APPLICATION_OCTET_STREAM} level- or quality-parameter are ignored with this method.) E.g. Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,text/html;level=2;q=0.4 The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.- Returns:
- one of the {@see Result} mime types that is acceptable for the client or {@see Result.TEXT_HTML} if not set
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getAcceptEncoding
String getAcceptEncoding()
Get the encoding that is acceptable for the client. E.g. Accept-Encoding: compress, gzip The Accept-Encoding request-header field is similar to Accept, but restricts the content-codings that are acceptable in the response.- Returns:
- the encoding that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getAcceptLanguage
String getAcceptLanguage()
Get the language that is acceptable for the client. E.g. Accept-Language: da, en-gb;q=0.8, en;q=0.7 The Accept-Language request-header field is similar to Accept, but restricts the set of natural languages that are preferred as a response to the request.- Returns:
- the language that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getAcceptCharset
String getAcceptCharset()
Get the charset that is acceptable for the client. E.g. Accept-Charset: iso-8859-5, unicode-1-1;q=0.8 The Accept-Charset request-header field can be used to indicate what character sets are acceptable for the response. This field allows clients capable of understanding more comprehensive or special- purpose character sets to signal that capability to a server which is capable of representing documents in those character sets.- Returns:
- the charset that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getMethod
String getMethod()
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.- Returns:
- a
String
specifying the name of the method with which this request was made (eg GET, POST, PUT...)
-
getAttribute
Object getAttribute(String name)
Gets an attribute value previously set bysetAttribute(java.lang.String, java.lang.Object)
.Attributes are shared state for the duration of the request; useful to pass values between
filters
and controllers.- Returns:
- the attribute value, or
null
if the attribute does not exist
-
getAttribute
<T> T getAttribute(String name, Class<T> clazz)
Gets an attribute value previously set bysetAttribute(java.lang.String, java.lang.Object)
.This is a convenience method, equivalent to:
return clazz.cast(getAttribute(name));
Attributes are shared state for the duration of the request; useful to pass values between
filters
and controllers.- Returns:
- the attribute value, or
null
if the attribute does not exist
-
setAttribute
void setAttribute(String name, Object value)
Sets an attribute value.Attributes are shared state for the duration of the request; useful to pass values between
filters
and controllers.- See Also:
getAttribute(String)
,getAttribute(String, Class)
-
getAttributes
Map<String,Object> getAttributes()
Get all the attributes from the request- Returns:
- The attributes
-
isRequestJson
boolean isRequestJson()
Check to see if the request content type is JSON.Checks to see if the request content type has been set application/json
- Returns:
- true if the content type is to set
application/json
-
isRequestXml
boolean isRequestXml()
Check to see if the request content type is XML.Checks to see if the request content type has been set application/xml
- Returns:
- true if the content type is to set
application/xml
-
addCookie
void addCookie(Cookie cookie)
Adds a cookie to the response- Parameters:
cookie
- Ninja cookie
-
unsetCookie
void unsetCookie(Cookie cookie)
Removes a cookie from the response- Parameters:
cookie
- Ninja Cookie
-
cleanup
void cleanup()
Cleanup context
-
-