public abstract class AbstractContext extends Object implements Context.Impl
getHeader()
method, but this class could
supply a default implementation of getAcceptContentType()
since
it only needs to fetch a value from getHeader()
.
When adding features to a Context
please think about whether
it should be fully or partially implemented here or in the concrete implementation.Context.HTTP_STATUS, Context.Impl
Modifier and Type | Field and 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 |
CONTENT_TYPE, NINJA_PROPERTIES_X_FORWARDED_FOR, X_FORWARD_HEADER
Constructor and Description |
---|
AbstractContext(BodyParserEngineManager bodyParserEngineManager,
FlashScope flashScope,
NinjaProperties ninjaProperties,
Session session,
Validation validation,
com.google.inject.Injector injector,
ParamParsers paramParsers) |
Modifier and Type | Method and 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 by
Context.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 defined
|
FlashScope |
getFlashCookie()
Deprecated.
|
FlashScope |
getFlashScope()
Returns the flash cookie.
|
String |
getParameter(String key,
String defaultValue)
Same like
Context.getParameter(String) , but returns given defaultValue
instead of null in case parameter cannot be found. |
<T> T |
getParameterAs(String key,
Class<T> clazz)
Same like
Context.getParameter(String) , but converts the parameter to
Class type if found. |
<T> T |
getParameterAs(String key,
Class<T> clazz,
T defaultValue)
Same like
Context.getParameter(String, String) , but converts the
parameter to Class type if found. |
Integer |
getParameterAsInteger(String key)
Same like
Context.getParameter(String) , but converts the parameter to
Integer if found. |
Integer |
getParameterAsInteger(String key,
Integer defaultValue)
Same like
Context.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 context
|
Session |
getSession()
Returns the client side session.
|
Session |
getSessionCookie()
Deprecated.
|
Validation |
getValidation()
Get the validation context
|
protected 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
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addCookie, cleanup, controllerReturned, getAttribute, getAttributes, getCookie, getCookies, getFileItemIterator, getHeader, getHeaders, getHeaders, getHostname, getInputStream, getMethod, getParameter, getParameterAsFileItem, getParameterAsFileItems, getParameterFileItems, getParameters, getParameterValues, getReader, getRequestContentType, getRequestUri, getScheme, handleAsync, hasCookie, isAsync, isMultipart, returnResultAsync, setAttribute
protected final BodyParserEngineManager bodyParserEngineManager
protected final FlashScope flashScope
protected final NinjaProperties ninjaProperties
protected final Session session
protected final Validation validation
protected final com.google.inject.Injector injector
protected final ParamParsers paramParsers
protected Route route
@Inject public AbstractContext(BodyParserEngineManager bodyParserEngineManager, FlashScope flashScope, NinjaProperties ninjaProperties, Session session, Validation validation, com.google.inject.Injector injector, ParamParsers paramParsers)
public void setRoute(Route route)
setRoute
in interface Context.Impl
public Route getRoute()
Context
public Validation getValidation()
Context
getValidation
in interface Context
@Deprecated public FlashScope getFlashCookie()
Context
getFlashCookie
in interface Context
public FlashScope getFlashScope()
Context
getFlashScope
in interface Context
@Deprecated public Session getSessionCookie()
Context
getSessionCookie
in interface Context
public Session getSession()
Context
getSession
in interface Context
public String getContextPath()
Context
getContextPath
in interface Context
public String getRequestPath()
Context
getRequestPath
in interface Context
protected abstract String getRealRemoteAddr()
public String getRemoteAddr()
Context
REMOTE_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.getRemoteAddr
in interface Context
String
containing the
IP address of the client that sent the request. Takes into
account X-Forwarded-For header if configured to do so.public <T> T getAttribute(String name, Class<T> clazz)
Context
Context.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.
getAttribute
in interface Context
null
if the attribute does
not existpublic String getPathParameter(String key)
Context
getPathParameter
in interface Context
key
- The name of the path parameter in a route. Eg
/{myName}/rest/of/urlpublic String getPathParameterEncoded(String key)
Context
getPathParameterEncoded
in interface Context
key
- The name of the path parameter in a route. Eg
/{myName}/rest/of/urlpublic Integer getPathParameterAsInteger(String key)
Context
getPathParameterAsInteger
in interface Context
key
- the key of the path parameterpublic String getParameter(String key, String defaultValue)
Context
Context.getParameter(String)
, but returns given defaultValue
instead of null in case parameter cannot be found.
The parameter is decoded by default.getParameter
in interface Context
key
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.public Integer getParameterAsInteger(String key)
Context
Context.getParameter(String)
, but converts the parameter to
Integer if found.
The parameter is decoded by default.getParameterAsInteger
in interface Context
key
- The name of the post or query parameterpublic Integer getParameterAsInteger(String key, Integer defaultValue)
Context
Context.getParameter(String, String)
, but converts the
parameter to Integer if found.
The parameter is decoded by default.getParameterAsInteger
in interface Context
key
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.public <T> T getParameterAs(String key, Class<T> clazz)
Context
Context.getParameter(String)
, but converts the parameter to
Class type if found.
The parameter is decoded by default.getParameterAs
in interface Context
key
- The name of the post or query parameterpublic <T> T getParameterAs(String key, Class<T> clazz, T defaultValue)
Context
Context.getParameter(String, String)
, but converts the
parameter to Class type if found.
The parameter is decoded by default.getParameterAs
in interface Context
key
- The name of the post or query parameterdefaultValue
- A default value if parameter not found.public <T> T parseBody(Class<T> classOfT)
Context
BodyParserEngine
BodyParserEngineJson
BodyParserEngineXml
and BodyParserEngineManager
public String getCookieValue(String name)
Context
getCookieValue
in interface Context
name
- The name of the cookiepublic void unsetCookie(Cookie cookie)
Context
unsetCookie
in interface Context
cookie
- Ninja Cookiepublic void asyncRequestComplete()
Context
asyncRequestComplete
in interface Context
protected ResponseStreams finalizeHeaders(Result result, Boolean handleFlashAndSessionCookie)
public ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result)
Context
finalizeHeadersWithoutFlashAndSessionCookie
in interface Context
public ResponseStreams finalizeHeaders(Result result)
Context
finalizeHeaders
in interface Context
public String getAcceptContentType()
Context
Result.TEXT_HTML
> Result.APPLICATION_XML
> Result.APPLICATION_JSON
> Result.TEXT_PLAIN
> 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.getAcceptContentType
in interface Context
Result
mime types that is acceptable for the
client or Result.TEXT_HTML
if not setpublic String getAcceptEncoding()
Context
getAcceptEncoding
in interface Context
public String getAcceptLanguage()
Context
getAcceptLanguage
in interface Context
public String getAcceptCharset()
Context
getAcceptCharset
in interface Context
public boolean isRequestJson()
Context
Checks to see if the request content type has been set application/json
isRequestJson
in interface Context
application/json
public boolean isRequestXml()
Context
Checks to see if the request content type has been set application/xml
isRequestXml
in interface Context
application/xml
Copyright © 2019 ninjaframework. All rights reserved.