Package ninja
Class WrappedContext
- java.lang.Object
-
- ninja.WrappedContext
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ninja.Context
Context.HTTP_STATUS, Context.Impl
-
-
Field Summary
-
Fields inherited from interface ninja.Context
CONTENT_TYPE, NINJA_PROPERTIES_X_FORWARDED_FOR, X_FORWARD_HEADER
-
-
Constructor Summary
Constructors Constructor Description WrappedContext(Context wrapped)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCookie(Cookie cookie)Adds a cookie to the responsevoidasyncRequestComplete()Indicate that processing this request is complete.voidcleanup()Cleanup contextResultcontrollerReturned()ResponseStreamsfinalizeHeaders(Result result)Finalizing the headers copies all stuff into the headers.ResponseStreamsfinalizeHeadersWithoutFlashAndSessionCookie(Result result)Finalizing the headers copies all stuff into the headers.StringgetAcceptCharset()Get the charset that is acceptable for the client.StringgetAcceptContentType()Get the content type that is acceptable for the client.StringgetAcceptEncoding()Get the encoding that is acceptable for the client.StringgetAcceptLanguage()Get the language that is acceptable for the client.ObjectgetAttribute(String name)Gets an attribute value previously set byContext.setAttribute(java.lang.String, java.lang.Object).<T> TgetAttribute(String name, Class<T> clazz)Gets an attribute value previously set byContext.setAttribute(java.lang.String, java.lang.Object).Map<String,Object>getAttributes()Get all the attributes from the requestStringgetContextPath()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.CookiegetCookie(String cookieName)Get cookie from context.List<Cookie>getCookies()Get all cookies from the context.StringgetCookieValue(String name)Get the cookie value from the request, if definedorg.apache.commons.fileupload.FileItemIteratorgetFileItemIterator()Gets the FileItemIterator of the input.FlashScopegetFlashCookie()Deprecated => please use getFlashScope()FlashScopegetFlashScope()Returns the flash cookie.StringgetHeader(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.StringgetHostname()Returns the hostname as seen by the server.InputStreamgetInputStream()Get the input stream to read the request.StringgetMethod()Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.StringgetParameter(String key)Get the parameter with the given key from the request.StringgetParameter(String key, String defaultValue)Same likeContext.getParameter(String), but returns given defaultValue instead of null in case parameter cannot be found.<T> TgetParameterAs(String key, Class<T> clazz)Same likeContext.getParameter(String), but converts the parameter to Class type if found.<T> TgetParameterAs(String key, Class<T> clazz, T defaultValue)Same likeContext.getParameter(String, String), but converts the parameter to Class type if found.FileItemgetParameterAsFileItem(String key)Same likeContext.getParameter(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.IntegergetParameterAsInteger(String key)Same likeContext.getParameter(String), but converts the parameter to Integer if found.IntegergetParameterAsInteger(String key, Integer defaultValue)Same likeContext.getParameter(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.StringgetPathParameter(String key)Get the path parameter for the given key.IntegergetPathParameterAsInteger(String key)Get the path parameter for the given key and convert it to Integer.StringgetPathParameterEncoded(String key)Get the path parameter for the given key.BufferedReadergetReader()Get the reader to read the request.StringgetRemoteAddr()Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.StringgetRequestContentType()The Content-Type header field indicates the media type of the request body sent to the recipient.StringgetRequestParameterString()Returns the parameter string that is contained in the request URL after the path.StringgetRequestPath()Returns the path that Ninja should act upon.StringgetRequestUri()Returns the uri as seen by the server.RoutegetRoute()Get the route for this contextStringgetScheme()For instance: http://example.com/index returns "http".SessiongetSession()Returns the client side session.SessiongetSessionCookie()Deprecated => please use getSession();ValidationgetValidation()Get the validation contextvoidhandleAsync()Indicate that this request is going to be handled asynchronouslybooleanhasCookie(String cookieName)Checks whether the context contains a given cookie.booleanisAsync()booleanisMultipart()Check if request is of type multipart.booleanisRequestJson()Check to see if the request content type is JSON.booleanisRequestXml()Check to see if the request content type is XML.<T> TparseBody(Class<T> classOfT)This will give you the request body nicely parsed.voidreturnResultAsync(Result result)Indicate that request processing of an async request is complete.voidsetAttribute(String name, Object value)Sets an attribute value.voidunsetCookie(Cookie cookie)Removes a cookie from the response
-
-
-
Constructor Detail
-
WrappedContext
public WrappedContext(Context wrapped)
-
-
Method Detail
-
getRequestUri
public String getRequestUri()
Description copied from interface:ContextReturns 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- Specified by:
getRequestUriin interfaceContext- Returns:
- the uri as seen by the server
-
getRequestParameterString
public String getRequestParameterString()
Description copied from interface:ContextReturns 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"- Specified by:
getRequestParameterStringin interfaceContext- Returns:
- The parameter string
-
getHostname
public String getHostname()
Description copied from interface:ContextReturns the hostname as seen by the server. http://example.com/index would return "example.com".- Specified by:
getHostnamein interfaceContext- Returns:
- the host name as seen by the server
-
getScheme
public String getScheme()
Description copied from interface:ContextFor instance: http://example.com/index returns "http". https://example.com/index returns "https".
-
getRemoteAddr
public String getRemoteAddr()
Description copied from interface:ContextReturns 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.- Specified by:
getRemoteAddrin interfaceContext- Returns:
- a
Stringcontaining the IP address of the client that sent the request. Takes into account X-Forwarded-For header if configured to do so.
-
getFlashCookie
public FlashScope getFlashCookie()
Description copied from interface:ContextDeprecated => please use getFlashScope()- Specified by:
getFlashCookiein interfaceContext- Returns:
- FlashScope of this request.
-
getSessionCookie
public Session getSessionCookie()
Description copied from interface:ContextDeprecated => please use getSession();- Specified by:
getSessionCookiein interfaceContext- Returns:
- the Session of that request / response cycle.
-
getFlashScope
public FlashScope getFlashScope()
Description copied from interface:ContextReturns 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.- Specified by:
getFlashScopein interfaceContext- Returns:
- the flash scope of that request.
-
getSession
public Session getSession()
Description copied from interface:ContextReturns 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.- Specified by:
getSessionin interfaceContext- Returns:
- the Session of that request / response cycle.
-
getParameter
public String getParameter(String key)
Description copied from interface:ContextGet 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.
- Specified by:
getParameterin interfaceContext- Parameters:
key- The key of the parameter- Returns:
- The value, or null if no parameter was found.
- See Also:
Context.getParameterValues(java.lang.String)
-
getParameterValues
public List<String> getParameterValues(String name)
Description copied from interface:ContextGet 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.
- Specified by:
getParameterValuesin interfaceContext- Parameters:
name- The key of the parameter- Returns:
- The values, possibly an empty list.
-
getParameter
public String getParameter(String key, String defaultValue)
Description copied from interface:ContextSame likeContext.getParameter(String), but returns given defaultValue instead of null in case parameter cannot be found. The parameter is decoded by default.- Specified by:
getParameterin interfaceContext- Parameters:
key- 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
public Integer getParameterAsInteger(String key)
Description copied from interface:ContextSame likeContext.getParameter(String), but converts the parameter to Integer if found. The parameter is decoded by default.- Specified by:
getParameterAsIntegerin interfaceContext- Parameters:
key- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAsInteger
public Integer getParameterAsInteger(String key, Integer defaultValue)
Description copied from interface:ContextSame likeContext.getParameter(String, String), but converts the parameter to Integer if found. The parameter is decoded by default.- Specified by:
getParameterAsIntegerin interfaceContext- Parameters:
key- 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
public FileItem getParameterAsFileItem(String key)
Description copied from interface:ContextSame likeContext.getParameter(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.- Specified by:
getParameterAsFileItemin interfaceContext- Parameters:
key- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAsFileItems
public List<FileItem> getParameterAsFileItems(String name)
Description copied from interface:ContextGet the files parameter with the given key from the request.- Specified by:
getParameterAsFileItemsin interfaceContext- Parameters:
name- The key of the parameter- Returns:
- The values, possibly an empty list.
-
getParameterFileItems
public Map<String,List<FileItem>> getParameterFileItems()
Description copied from interface:ContextGet all the file parameters from the request- Specified by:
getParameterFileItemsin interfaceContext- Returns:
- The parameters, possibly a null map.
-
getParameterAs
public <T> T getParameterAs(String key, Class<T> clazz)
Description copied from interface:ContextSame likeContext.getParameter(String), but converts the parameter to Class type if found. The parameter is decoded by default.- Specified by:
getParameterAsin interfaceContext- Parameters:
key- The name of the post or query parameter- Returns:
- The value of the parameter or null if not found.
-
getParameterAs
public <T> T getParameterAs(String key, Class<T> clazz, T defaultValue)
Description copied from interface:ContextSame likeContext.getParameter(String, String), but converts the parameter to Class type if found. The parameter is decoded by default.- Specified by:
getParameterAsin interfaceContext- Parameters:
key- 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
public String getPathParameter(String key)
Description copied from interface:ContextGet 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.- Specified by:
getPathParameterin interfaceContext- Parameters:
key- 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.
-
getPathParameterAsInteger
public Integer getPathParameterAsInteger(String key)
Description copied from interface:ContextGet 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.- Specified by:
getPathParameterAsIntegerin interfaceContext- 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
public Map<String,String[]> getParameters()
Description copied from interface:ContextGet all the parameters from the request- Specified by:
getParametersin interfaceContext- Returns:
- The parameters
-
getHeader
public String getHeader(String name)
Description copied from interface:ContextGet the (first) request header with the given name
-
getHeaders
public List<String> getHeaders(String name)
Description copied from interface:ContextGet all the request headers with the given name.- Specified by:
getHeadersin interfaceContext- Returns:
- the header values. Returns empty list if none are found.
-
getHeaders
public Map<String,List<String>> getHeaders()
Description copied from interface:ContextGet all the headers from the request- Specified by:
getHeadersin interfaceContext- Returns:
- The headers
-
getCookieValue
public String getCookieValue(String name)
Description copied from interface:ContextGet the cookie value from the request, if defined- Specified by:
getCookieValuein interfaceContext- Parameters:
name- The name of the cookie- Returns:
- The cookie value, or null if the cookie was not found
-
parseBody
public <T> T parseBody(Class<T> classOfT)
Description copied from interface:ContextThis will give you the request body nicely parsed. You can register your own parsers depending on the request type. Have a look atBodyParserEngineBodyParserEngineJsonBodyParserEngineXmlandBodyParserEngineManager
-
handleAsync
public void handleAsync()
Description copied from interface:ContextIndicate that this request is going to be handled asynchronously- Specified by:
handleAsyncin interfaceContext
-
returnResultAsync
public void returnResultAsync(Result result)
Description copied from interface:ContextIndicate that request processing of an async request is complete.- Specified by:
returnResultAsyncin interfaceContext
-
asyncRequestComplete
public void asyncRequestComplete()
Description copied from interface:ContextIndicate that processing this request is complete.- Specified by:
asyncRequestCompletein interfaceContext
-
controllerReturned
public Result controllerReturned()
- Specified by:
controllerReturnedin interfaceContext
-
finalizeHeaders
public ResponseStreams finalizeHeaders(Result result)
Description copied from interface:ContextFinalizing 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.- Specified by:
finalizeHeadersin interfaceContext
-
finalizeHeadersWithoutFlashAndSessionCookie
public ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result)
Description copied from interface:ContextFinalizing 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.- Specified by:
finalizeHeadersWithoutFlashAndSessionCookiein interfaceContext
-
getInputStream
public InputStream getInputStream() throws IOException
Description copied from interface:ContextGet the input stream to read the request. Must not be used if getReader has been called.- Specified by:
getInputStreamin interfaceContext- Returns:
- The input stream
- Throws:
IOException
-
getReader
public BufferedReader getReader() throws IOException
Description copied from interface:ContextGet the reader to read the request. Must not be used if getInputStream has been called.- Specified by:
getReaderin interfaceContext- Returns:
- The reader
- Throws:
IOException
-
getRequestContentType
public String getRequestContentType()
Description copied from interface:ContextThe 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- Specified by:
getRequestContentTypein interfaceContext- Returns:
- the content type of the incoming request.
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getRoute
public Route getRoute()
Description copied from interface:ContextGet the route for this context
-
isMultipart
public boolean isMultipart()
Description copied from interface:ContextCheck 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- Specified by:
isMultipartin interfaceContext- Returns:
- true if request is of type multipart.
-
getFileItemIterator
public org.apache.commons.fileupload.FileItemIterator getFileItemIterator()
Description copied from interface:ContextGets the FileItemIterator of the input. Can be used to process uploads in a streaming fashion. Check out: http://commons.apache.org/fileupload/streaming.html- Specified by:
getFileItemIteratorin interfaceContext- Returns:
- the FileItemIterator of the request or null if there was an error.
-
getRequestPath
public String getRequestPath()
Description copied from interface:ContextReturns 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- Specified by:
getRequestPathin interfaceContext- Returns:
- The the path as seen by the server. Does exclude any container set context prefixes. Not decoded.
-
getValidation
public Validation getValidation()
Description copied from interface:ContextGet the validation context- Specified by:
getValidationin interfaceContext- Returns:
- The validation context
-
getPathParameterEncoded
public String getPathParameterEncoded(String key)
Description copied from interface:ContextGet the path parameter for the given key. Returns the raw path part. That means you can get stuff like: blue%2Fred%3Fand+green- Specified by:
getPathParameterEncodedin interfaceContext- Parameters:
key- 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.
-
getAcceptContentType
public String getAcceptContentType()
Description copied from interface:ContextGet 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.- Specified by:
getAcceptContentTypein interfaceContext- 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
public String getAcceptEncoding()
Description copied from interface:ContextGet 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.- Specified by:
getAcceptEncodingin interfaceContext- Returns:
- the encoding that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getAcceptLanguage
public String getAcceptLanguage()
Description copied from interface:ContextGet 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.- Specified by:
getAcceptLanguagein interfaceContext- Returns:
- the language that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getAcceptCharset
public String getAcceptCharset()
Description copied from interface:ContextGet 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.- Specified by:
getAcceptCharsetin interfaceContext- Returns:
- the charset that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
getCookie
public Cookie getCookie(String cookieName)
Description copied from interface:ContextGet cookie from context.
-
hasCookie
public boolean hasCookie(String cookieName)
Description copied from interface:ContextChecks whether the context contains a given cookie.
-
getCookies
public List<Cookie> getCookies()
Description copied from interface:ContextGet all cookies from the context.- Specified by:
getCookiesin interfaceContext- Returns:
- the cookie with that name or null.
-
getMethod
public String getMethod()
Description copied from interface:ContextReturns 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.
-
getAttribute
public Object getAttribute(String name)
Description copied from interface:ContextGets an attribute value previously set byContext.setAttribute(java.lang.String, java.lang.Object).Attributes are shared state for the duration of the request; useful to pass values between
filtersand controllers.- Specified by:
getAttributein interfaceContext- Returns:
- the attribute value, or
nullif the attribute does not exist
-
getAttribute
public <T> T getAttribute(String name, Class<T> clazz)
Description copied from interface:ContextGets an attribute value previously set byContext.setAttribute(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
filtersand controllers.- Specified by:
getAttributein interfaceContext- Returns:
- the attribute value, or
nullif the attribute does not exist
-
setAttribute
public void setAttribute(String name, Object value)
Description copied from interface:ContextSets an attribute value.Attributes are shared state for the duration of the request; useful to pass values between
filtersand controllers.- Specified by:
setAttributein interfaceContext- See Also:
Context.getAttribute(String),Context.getAttribute(String, Class)
-
getAttributes
public Map<String,Object> getAttributes()
Description copied from interface:ContextGet all the attributes from the request- Specified by:
getAttributesin interfaceContext- Returns:
- The attributes
-
getContextPath
public String getContextPath()
Description copied from interface:ContextGet 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()- Specified by:
getContextPathin interfaceContext- Returns:
- the context-path with a leading "/" or "" if running on root
-
isRequestJson
public boolean isRequestJson()
Description copied from interface:ContextCheck to see if the request content type is JSON.Checks to see if the request content type has been set application/json
- Specified by:
isRequestJsonin interfaceContext- Returns:
- true if the content type is to set
application/json
-
isRequestXml
public boolean isRequestXml()
Description copied from interface:ContextCheck to see if the request content type is XML.Checks to see if the request content type has been set application/xml
- Specified by:
isRequestXmlin interfaceContext- Returns:
- true if the content type is to set
application/xml
-
addCookie
public void addCookie(Cookie cookie)
Description copied from interface:ContextAdds a cookie to the response
-
unsetCookie
public void unsetCookie(Cookie cookie)
Description copied from interface:ContextRemoves a cookie from the response- Specified by:
unsetCookiein interfaceContext- Parameters:
cookie- Ninja Cookie
-
-