Package ninja.utils
Class AbstractContext
- java.lang.Object
-
- ninja.utils.AbstractContext
-
- All Implemented Interfaces:
Context
,Context.Impl
public abstract class AbstractContext extends Object implements Context.Impl
Abstract Context.Impl that implements features that are not reliant on the concrete Context implementation. For example, a concrete implementation may have to provide agetHeader()
method, but this class could supply a default implementation ofgetAcceptContentType()
since it only needs to fetch a value fromgetHeader()
. When adding features to aContext
please think about whether it should be fully or partially implemented here or in the concrete implementation.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface ninja.Context
Context.HTTP_STATUS, Context.Impl
-
-
Field Summary
Fields Modifier and Type Field Description protected BodyParserEngineManager
bodyParserEngineManager
protected FlashScope
flashScope
protected com.google.inject.Injector
injector
protected NinjaProperties
ninjaProperties
protected ParamParsers
paramParsers
protected Route
route
protected Session
session
protected Validation
validation
-
Fields inherited from interface ninja.Context
CONTENT_TYPE, NINJA_PROPERTIES_X_FORWARDED_FOR, X_FORWARD_HEADER
-
-
Constructor Summary
Constructors Constructor Description AbstractContext(BodyParserEngineManager bodyParserEngineManager, FlashScope flashScope, NinjaProperties ninjaProperties, Session session, Validation validation, com.google.inject.Injector injector, ParamParsers paramParsers)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
asyncRequestComplete()
Indicate that processing this request is complete.ResponseStreams
finalizeHeaders(Result result)
Finalizing the headers copies all stuff into the headers.protected ResponseStreams
finalizeHeaders(Result result, Boolean handleFlashAndSessionCookie)
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.<T> T
getAttribute(String name, Class<T> clazz)
Gets an attribute value previously set byContext.setAttribute(java.lang.String, java.lang.Object)
.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.String
getCookieValue(String name)
Get the cookie value from the request, if definedFlashScope
getFlashCookie()
Deprecated.FlashScope
getFlashScope()
Returns the flash cookie.String
getParameter(String key, String defaultValue)
Same likeContext.getParameter(String)
, but returns given defaultValue instead of null in case parameter cannot be found.<T> T
getParameterAs(String key, Class<T> clazz)
Same likeContext.getParameter(String)
, but converts the parameter to Class type if found.<T> T
getParameterAs(String key, Class<T> clazz, T defaultValue)
Same likeContext.getParameter(String, String)
, but converts the parameter to Class type if found.Integer
getParameterAsInteger(String key)
Same likeContext.getParameter(String)
, but converts the parameter to Integer if found.Integer
getParameterAsInteger(String key, Integer defaultValue)
Same likeContext.getParameter(String, String)
, but converts the parameter to Integer if found.String
getPathParameter(String key)
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 key)
Get the path parameter for the given key.protected abstract String
getRealRemoteAddr()
Get the "real" address of the client connection.String
getRemoteAddr()
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request.String
getRequestPath()
Returns the path that Ninja should act upon.Route
getRoute()
Get the route for this contextSession
getSession()
Returns the client side session.Session
getSessionCookie()
Deprecated.Validation
getValidation()
Get the validation contextprotected void
init(String contextPath, String requestPath)
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
setRoute(Route route)
void
unsetCookie(Cookie cookie)
Removes a cookie from the response-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ninja.Context
addCookie, cleanup, controllerReturned, getAttribute, getAttributes, getCookie, getCookies, getFileItemIterator, getHeader, getHeaders, getHeaders, getHostname, getInputStream, getMethod, getParameter, getParameterAsFileItem, getParameterAsFileItems, getParameterFileItems, getParameters, getParameterValues, getReader, getRequestContentType, getRequestParameterString, getRequestUri, getScheme, handleAsync, hasCookie, isAsync, isMultipart, returnResultAsync, setAttribute
-
-
-
-
Field Detail
-
bodyParserEngineManager
protected final BodyParserEngineManager bodyParserEngineManager
-
flashScope
protected final FlashScope flashScope
-
ninjaProperties
protected final NinjaProperties ninjaProperties
-
session
protected final Session session
-
validation
protected final Validation validation
-
injector
protected final com.google.inject.Injector injector
-
paramParsers
protected final ParamParsers paramParsers
-
route
protected Route route
-
-
Constructor Detail
-
AbstractContext
@Inject public AbstractContext(BodyParserEngineManager bodyParserEngineManager, FlashScope flashScope, NinjaProperties ninjaProperties, Session session, Validation validation, com.google.inject.Injector injector, ParamParsers paramParsers)
-
-
Method Detail
-
setRoute
public void setRoute(Route route)
- Specified by:
setRoute
in interfaceContext.Impl
-
getRoute
public Route getRoute()
Description copied from interface:Context
Get the route for this context
-
getValidation
public Validation getValidation()
Description copied from interface:Context
Get the validation context- Specified by:
getValidation
in interfaceContext
- Returns:
- The validation context
-
getFlashCookie
@Deprecated public FlashScope getFlashCookie()
Deprecated.Description copied from interface:Context
Deprecated => please use getFlashScope()- Specified by:
getFlashCookie
in interfaceContext
- Returns:
- FlashScope of this request.
-
getFlashScope
public FlashScope getFlashScope()
Description copied from interface:Context
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.- Specified by:
getFlashScope
in interfaceContext
- Returns:
- the flash scope of that request.
-
getSessionCookie
@Deprecated public Session getSessionCookie()
Deprecated.Description copied from interface:Context
Deprecated => please use getSession();- Specified by:
getSessionCookie
in interfaceContext
- Returns:
- the Session of that request / response cycle.
-
getSession
public Session getSession()
Description copied from interface:Context
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.- Specified by:
getSession
in interfaceContext
- Returns:
- the Session of that request / response cycle.
-
getContextPath
public String getContextPath()
Description copied from interface:Context
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()- Specified by:
getContextPath
in interfaceContext
- Returns:
- the context-path with a leading "/" or "" if running on root
-
getRequestPath
public String getRequestPath()
Description copied from interface:Context
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- Specified by:
getRequestPath
in interfaceContext
- Returns:
- The the path as seen by the server. Does exclude any container set context prefixes. Not decoded.
-
getRealRemoteAddr
protected abstract String getRealRemoteAddr()
Get the "real" address of the client connection. Does not take any header (e.g. X-Forwarded-For) into account.- Returns:
- The real address of the client connection
-
getRemoteAddr
public String getRemoteAddr()
Description copied from interface:Context
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.- Specified by:
getRemoteAddr
in interfaceContext
- 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.
-
getAttribute
public <T> T getAttribute(String name, Class<T> clazz)
Description copied from interface:Context
Gets 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
filters
and controllers.- Specified by:
getAttribute
in interfaceContext
- Returns:
- the attribute value, or
null
if the attribute does not exist
-
getPathParameter
public String getPathParameter(String key)
Description copied from interface:Context
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.- Specified by:
getPathParameter
in 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.
-
getPathParameterEncoded
public String getPathParameterEncoded(String key)
Description copied from interface:Context
Get 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:
getPathParameterEncoded
in 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.
-
getPathParameterAsInteger
public Integer getPathParameterAsInteger(String key)
Description copied from interface:Context
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.- Specified by:
getPathParameterAsInteger
in 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
-
getParameter
public String getParameter(String key, String defaultValue)
Description copied from interface:Context
Same likeContext.getParameter(String)
, but returns given defaultValue instead of null in case parameter cannot be found. The parameter is decoded by default.- Specified by:
getParameter
in 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:Context
Same likeContext.getParameter(String)
, but converts the parameter to Integer if found. The parameter is decoded by default.- Specified by:
getParameterAsInteger
in 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:Context
Same likeContext.getParameter(String, String)
, but converts the parameter to Integer if found. The parameter is decoded by default.- Specified by:
getParameterAsInteger
in 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.
-
getParameterAs
public <T> T getParameterAs(String key, Class<T> clazz)
Description copied from interface:Context
Same likeContext.getParameter(String)
, but converts the parameter to Class type if found. The parameter is decoded by default.- Specified by:
getParameterAs
in 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:Context
Same likeContext.getParameter(String, String)
, but converts the parameter to Class type if found. The parameter is decoded by default.- Specified by:
getParameterAs
in 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.
-
parseBody
public <T> T parseBody(Class<T> classOfT)
Description copied from interface:Context
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
-
getCookieValue
public String getCookieValue(String name)
Description copied from interface:Context
Get the cookie value from the request, if defined- Specified by:
getCookieValue
in interfaceContext
- Parameters:
name
- The name of the cookie- Returns:
- The cookie value, or null if the cookie was not found
-
unsetCookie
public void unsetCookie(Cookie cookie)
Description copied from interface:Context
Removes a cookie from the response- Specified by:
unsetCookie
in interfaceContext
- Parameters:
cookie
- Ninja Cookie
-
asyncRequestComplete
public void asyncRequestComplete()
Description copied from interface:Context
Indicate that processing this request is complete.- Specified by:
asyncRequestComplete
in interfaceContext
-
finalizeHeaders
protected ResponseStreams finalizeHeaders(Result result, Boolean handleFlashAndSessionCookie)
-
finalizeHeadersWithoutFlashAndSessionCookie
public ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result)
Description copied from interface:Context
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.- Specified by:
finalizeHeadersWithoutFlashAndSessionCookie
in interfaceContext
-
finalizeHeaders
public ResponseStreams finalizeHeaders(Result result)
Description copied from interface:Context
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.- Specified by:
finalizeHeaders
in interfaceContext
-
getAcceptContentType
public String getAcceptContentType()
Description copied from interface:Context
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.- Specified by:
getAcceptContentType
in 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:Context
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.- Specified by:
getAcceptEncoding
in 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:Context
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.- Specified by:
getAcceptLanguage
in 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:Context
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.- Specified by:
getAcceptCharset
in interfaceContext
- Returns:
- the charset that is acceptable for the client
- See Also:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
-
isRequestJson
public boolean isRequestJson()
Description copied from interface:Context
Check to see if the request content type is JSON.Checks to see if the request content type has been set application/json
- Specified by:
isRequestJson
in interfaceContext
- Returns:
- true if the content type is to set
application/json
-
isRequestXml
public boolean isRequestXml()
Description copied from interface:Context
Check to see if the request content type is XML.Checks to see if the request content type has been set application/xml
- Specified by:
isRequestXml
in interfaceContext
- Returns:
- true if the content type is to set
application/xml
-
-