ninja
Interface Context

All Known Implementing Classes:
ContextImpl, WrappedContext

public interface Context


Nested Class Summary
static class Context.HTTP_STATUS
          Deprecated. 
 
Field Summary
static String CONTENT_TYPE
          Content-Type: ... parameter for response.
 
Method Summary
 Context addCookie(Cookie cookie)
          Add the given cookie to the response
 void asyncRequestComplete()
          Indicate that processing this request is complete.
 Result controllerReturned()
           
 ResponseStreams finalizeHeaders(Result result)
          Finalizing the headers copies all stuff into the headers.
 String getCookieValue(String name)
          Get the cookie value from the request, if defined
 org.apache.commons.fileupload.FileItemIterator getFileItemIterator()
          Gets the FileItemIterator of the input.
 FlashCookie getFlashCookie()
          Returns the flash cookie.
 String getHeader(String name)
          Get the request header with the given name
 Map<String,String> getHeaders()
          Get all the headers from the request
 javax.servlet.http.HttpServletRequest getHttpServletRequest()
          Deprecated. 
 javax.servlet.http.HttpServletResponse getHttpServletResponse()
          Deprecated. 
 InputStream getInputStream()
          Get the input stream to read the request.
 String getParameter(String name)
          Get the parameter with the given key from the request.
 String getParameter(String name, String defaultValue)
          Same like getParameter(String), but returns given defaultValue instead of null in case parameter cannot be found.
 Integer getParameterAsInteger(String name)
          Same like getParameter(String), but converts the parameter to Integer if found.
 Integer getParameterAsInteger(String name, Integer defaultValue)
          Same like getParameter(String, String), but converts the parameter to Integer if found.
 Map<String,String[]> getParameters()
          Get all the parameters 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 getRequestContentType()
          Content type of the request we got.
 String getRequestPath()
          Returns the path that Ninja should act upon.
 String getRequestUri()
          Deprecated. 
 Route getRoute()
          Get the route for this context
 SessionCookie getSessionCookie()
          Returns the client side session.
 Validation getValidation()
          Get the validation context
 void handleAsync()
          Indicate that this request is going to be handled asynchronously
 boolean isMultipart()
          Check if request is of type multipart.
<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.
 

Field Detail

CONTENT_TYPE

static final String CONTENT_TYPE
Content-Type: ... parameter for response.

See Also:
Constant Field Values
Method Detail

getRequestContentType

String getRequestContentType()
Content type of the request we got. Important for content negotiation...

Returns:
the content type of the incoming request.

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

getRequestPath

String getRequestPath()
Returns the path that Ninja should act upon. For instance in servlets you could have somthing 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.

getFlashCookie

FlashCookie getFlashCookie()
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 FlashCookie is usually not signed. Don't trust the content.

Returns:
the flash cookie of that request.

getSessionCookie

SessionCookie getSessionCookie()
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.

addCookie

Context addCookie(Cookie cookie)
Add the given cookie to the response

Parameters:
cookie - The cookie to add
Returns:
This context

getHttpServletRequest

@Deprecated
javax.servlet.http.HttpServletRequest getHttpServletRequest()
Deprecated. 

Get the underlying HTTP servlet request

Returns:
The HTTP servlet request

getHttpServletResponse

@Deprecated
javax.servlet.http.HttpServletResponse getHttpServletResponse()
Deprecated. 

Get the underlying HTTP servlet response

Returns:
The HTTP servlet response

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. The parameter is decoded by default.

Parameters:
name - The key of the parameter
Returns:
The value, or null if no parameter was found.

getParameter

String getParameter(String name,
                    String defaultValue)
Same like getParameter(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 parameter
defaultValue - 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 like getParameter(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 like getParameter(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 parameter
defaultValue - 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 request header with the given name

Returns:
The header value

getHeaders

Map<String,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 at BodyParserEngine BodyParserEngineJson and BodyParserEngineManager

Parameters:
classOfT - The class of the result.
Returns:
The parsed request or null if something went wrong.

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. After finalizing the headers you can access the responseStreams.

Parameters:
result -
Returns:

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

org.apache.commons.fileupload.FileItemIterator getFileItemIterator()
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


Copyright © 2012. All Rights Reserved.