play.mvc
Class Controller

java.lang.Object
  extended by play.mvc.Controller
All Implemented Interfaces:
ControllersEnhancer.ControllerSupport, LocalvariablesNamesEnhancer.LocalVariablesSupport

public abstract class Controller
extends Object
implements ControllersEnhancer.ControllerSupport, LocalvariablesNamesEnhancer.LocalVariablesSupport

Application controller support: The controller receives input and initiates a response by making calls on model objects. This is the class that your controllers should extend.


Field Summary
static ThreadLocal<Router.ActionDefinition> _currentReverse
          Don't use this directly if you don't know why
protected static Scope.Flash flash
          The current flash scope.
protected static Scope.Params params
          The current HTTP params.
protected static Scope.RenderArgs renderArgs
          The current renderArgs scope: This is a hash map that is accessible during the rendering phase.
protected static Http.Request request
          The current HTTP request: the message sent by the client to the server.
protected static Http.Response response
          The current HTTP response: The message sent back from the server after a request.
protected static Scope.Session session
          The current HTTP session.
protected static Validation validation
          The current Validation object.
 
Constructor Summary
Controller()
           
 
Method Summary
protected static void badRequest()
          Send a 400 Bad request
protected static void checkAuthenticity()
           
protected static void error()
          Send a 500 Error response
protected static void error(Exception reason)
          Send a 500 Error response
protected static void error(int status, String reason)
          Send a 5xx Error response
protected static void error(String reason)
          Send a 500 Error response
protected static void flash(String key, Object value)
          Add a value to the flash scope
protected static void forbidden()
          Send a 403 Forbidden response
protected static void forbidden(String reason)
          Send a 403 Forbidden response
protected static
<T extends Annotation>
T
getActionAnnotation(Class<T> clazz)
          Retrieve annotation for the action method
protected static
<T extends Annotation>
T
getControllerAnnotation(Class<T> clazz)
          Retrieve annotation for the controller class
protected static Class<?> getControllerClass()
          Retrieve annotation for the action method
protected static
<T extends Annotation>
T
getControllerInheritedAnnotation(Class<T> clazz)
          Retrieve annotation for the controller class
protected static void notFound()
          Send a 404 Not Found reponse
protected static void notFound(String what)
          Send a 404 Not Found response
protected static void notFoundIfNull(Object o)
          Send a 404 Not Found response if object is null
protected static void notModified()
          Send a 304 Not Modified response
protected static void ok()
          Send a 200 OK response
protected static void parent()
          Call the parent method
protected static void parent(Map<String,Object> map)
          Call the parent action adding this objects to the params scope
protected static void parent(Object... args)
          Call the parent action adding this objects to the params scope
protected static void redirect(String url)
          Send a 302 redirect response.
protected static void redirect(String url, boolean permanent)
          Send a Redirect response.
protected static void redirect(String action, boolean permanent, Object... args)
          Redirect to another action
protected static void redirect(String action, Object... args)
          302 Redirect to another action
protected static void redirectToStatic(String file)
          Send a 302 redirect response.
protected static void render(Object... args)
          Render the corresponding template
protected static void renderBinary(File file)
          Return a 200 OK application/binary response
protected static void renderBinary(File file, String name)
          Return a 200 OK application/binary response with content-disposition attachment
protected static void renderBinary(InputStream is)
          Return a 200 OK application/binary response
protected static void renderBinary(InputStream is, long length)
          Return a 200 OK application/binary response.
protected static void renderBinary(InputStream is, String name)
          Return a 200 OK application/binary response with content-disposition attachment
protected static void renderBinary(InputStream is, String name, long length)
          Return a 200 OK application/binary response with content-disposition attachment
protected static void renderJSON(Object o)
          Render a 200 OK application/json response
protected static void renderJSON(Object o, com.google.gson.JsonSerializer<?>... adapters)
          Render a 200 OK application/json response
protected static void renderJSON(String jsonString)
          Render a 200 OK application/json response
protected static void renderTemplate(String templateName, Object... args)
          Render a specific template
protected static void renderText(CharSequence pattern, Object... args)
          Return a 200 OK text/plain response
protected static void renderText(Object text)
          Return a 200 OK text/plain response
protected static void renderXml(Document xml)
          Return a 200 OK text/xml response
protected static void renderXml(String xml)
          Return a 200 OK text/xml response
protected static Router.ActionDefinition reverse()
           
protected static void suspend(int timeout)
           
protected static void suspend(String timeout)
          Suspend the current request for a specified amount of time
protected static void todo()
          Send a TODO response
protected static void unauthorized(String realm)
          Send a 401 Unauthorized response
protected static void waitFor(Future<?> task)
          Suspend this request and wait for the task completion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

protected static Http.Request request
The current HTTP request: the message sent by the client to the server. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.request - controller.request.current()


response

protected static Http.Response response
The current HTTP response: The message sent back from the server after a request. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.response - controller.response.current()


session

protected static Scope.Session session
The current HTTP session. The Play! session is not living on the server side but on the client side. In fact, it is stored in a signed cookie. This session is therefore limited to 4kb. From Wikipedia: Client-side sessions use cookies and cryptographic techniques to maintain state without storing as much data on the server. When presenting a dynamic web page, the server sends the current state data to the client (web browser) in the form of a cookie. The client saves the cookie in memory or on disk. With each successive request, the client sends the cookie back to the server, and the server uses the data to "remember" the state of the application for that specific client and generate an appropriate response. This mechanism may work well in some contexts; however, data stored on the client is vulnerable to tampering by the user or by software that has access to the client computer. To use client-side sessions where confidentiality and integrity are required, the following must be guaranteed: Confidentiality: Nothing apart from the server should be able to interpret session data. Data integrity: Nothing apart from the server should manipulate session data (accidentally or maliciously). Authenticity: Nothing apart from the server should be able to initiate valid sessions. To accomplish this, the server needs to encrypt the session data before sending it to the client, and modification of such information by any other party should be prevented via cryptographic means. Transmitting state back and forth with every request is only practical when the size of the cookie is small. In essence, client-side sessions trade server disk space for the extra bandwidth that each web request will require. Moreover, web browsers limit the number and size of cookies that may be stored by a web site. To improve efficiency and allow for more session data, the server may compress the data before creating the cookie, decompressing it later when the cookie is returned by the client. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.session - controller.session.current()


flash

protected static Scope.Flash flash
The current flash scope. The flash is a temporary storage mechanism that is a hash map You can store values associated with keys and later retrieve them. It has one special property: by default, values stored into the flash during the processing of a request will be available during the processing of the immediately following request. Once that second request has been processed, those values are removed automatically from the storage This scope is very useful to display messages after issuing a Redirect. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.flash - controller.flash.current()


params

protected static Scope.Params params
The current HTTP params. This scope allows you to access the HTTP parameters supplied with the request. This is useful for example to know which submit button a user pressed on a form. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.params - controller.params.current()


renderArgs

protected static Scope.RenderArgs renderArgs
The current renderArgs scope: This is a hash map that is accessible during the rendering phase. It means you can access variables stored in this scope during the rendering phase (the template phase). Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.renderArgs - controller.renderArgs.current()


validation

protected static Validation validation
The current Validation object. It allows you to validate objects and to retrieve potential validations errors for those objects. Note: The ControllersEnhancer makes sure that an appropriate thread local version is applied. ie : controller.validation - controller.validation.current()


_currentReverse

public static ThreadLocal<Router.ActionDefinition> _currentReverse
Don't use this directly if you don't know why

Constructor Detail

Controller

public Controller()
Method Detail

renderText

protected static void renderText(Object text)
Return a 200 OK text/plain response

Parameters:
text - The response content

renderText

protected static void renderText(CharSequence pattern,
                                 Object... args)
Return a 200 OK text/plain response

Parameters:
pattern - The response content to be formatted (with String.format)
args - Args for String.format

renderXml

protected static void renderXml(String xml)
Return a 200 OK text/xml response

Parameters:
xml - The XML string

renderXml

protected static void renderXml(Document xml)
Return a 200 OK text/xml response

Parameters:
xml - The DOM document object

renderBinary

protected static void renderBinary(InputStream is)
Return a 200 OK application/binary response

Parameters:
is - The stream to copy

renderBinary

protected static void renderBinary(InputStream is,
                                   long length)
Return a 200 OK application/binary response. Content is streamed

Parameters:
is - The stream to copy

renderBinary

protected static void renderBinary(InputStream is,
                                   String name)
Return a 200 OK application/binary response with content-disposition attachment

Parameters:
is - The stream to copy
name - The attachment name

renderBinary

protected static void renderBinary(InputStream is,
                                   String name,
                                   long length)
Return a 200 OK application/binary response with content-disposition attachment

Parameters:
is - The stream to copy. COntent is streamed
name - The attachment name

renderBinary

protected static void renderBinary(File file)
Return a 200 OK application/binary response

Parameters:
file - The file to copy

renderBinary

protected static void renderBinary(File file,
                                   String name)
Return a 200 OK application/binary response with content-disposition attachment

Parameters:
file - The file to copy
name - The attachment name

renderJSON

protected static void renderJSON(String jsonString)
Render a 200 OK application/json response

Parameters:
jsonString - The JSON string

renderJSON

protected static void renderJSON(Object o)
Render a 200 OK application/json response

Parameters:
o - The Java object to serialize

renderJSON

protected static void renderJSON(Object o,
                                 com.google.gson.JsonSerializer<?>... adapters)
Render a 200 OK application/json response

Parameters:
o - The Java object to serialize
adapters - A set of GSON serializers/deserializers/instance creator to use

notModified

protected static void notModified()
Send a 304 Not Modified response


badRequest

protected static void badRequest()
Send a 400 Bad request


unauthorized

protected static void unauthorized(String realm)
Send a 401 Unauthorized response

Parameters:
realm - The realm name

notFound

protected static void notFound(String what)
Send a 404 Not Found response

Parameters:
what - The Not Found resource name

ok

protected static void ok()
Send a 200 OK response


todo

protected static void todo()
Send a TODO response


notFoundIfNull

protected static void notFoundIfNull(Object o)
Send a 404 Not Found response if object is null

Parameters:
o - The object to check

notFound

protected static void notFound()
Send a 404 Not Found reponse


checkAuthenticity

protected static void checkAuthenticity()

forbidden

protected static void forbidden(String reason)
Send a 403 Forbidden response

Parameters:
reason - The reason

forbidden

protected static void forbidden()
Send a 403 Forbidden response


error

protected static void error(int status,
                            String reason)
Send a 5xx Error response

Parameters:
status - The exact status code
reason - The reason

error

protected static void error(String reason)
Send a 500 Error response

Parameters:
reason - The reason

error

protected static void error(Exception reason)
Send a 500 Error response

Parameters:
reason - The reason

error

protected static void error()
Send a 500 Error response


flash

protected static void flash(String key,
                            Object value)
Add a value to the flash scope

Parameters:
key - The key
value - The value

redirect

protected static void redirect(String url)
Send a 302 redirect response.

Parameters:
url - The Location to redirect

redirectToStatic

protected static void redirectToStatic(String file)
Send a 302 redirect response.

Parameters:
file - The Location to redirect

redirect

protected static void redirect(String url,
                               boolean permanent)
Send a Redirect response.

Parameters:
url - The Location to redirect
permanent - true -> 301, false -> 302

redirect

protected static void redirect(String action,
                               Object... args)
302 Redirect to another action

Parameters:
action - The fully qualified action name (ex: Application.index)
args - Method arguments

redirect

protected static void redirect(String action,
                               boolean permanent,
                               Object... args)
Redirect to another action

Parameters:
action - The fully qualified action name (ex: Application.index)
permanent - true -> 301, false -> 302
args - Method arguments

renderTemplate

protected static void renderTemplate(String templateName,
                                     Object... args)
Render a specific template

Parameters:
templateName - The template name
args - The template data

render

protected static void render(Object... args)
Render the corresponding template

Parameters:
args - The template data

getActionAnnotation

protected static <T extends Annotation> T getActionAnnotation(Class<T> clazz)
Retrieve annotation for the action method

Parameters:
clazz - The annotation class
Returns:
Annotation object or null if not found

getControllerAnnotation

protected static <T extends Annotation> T getControllerAnnotation(Class<T> clazz)
Retrieve annotation for the controller class

Parameters:
clazz - The annotation class
Returns:
Annotation object or null if not found

getControllerInheritedAnnotation

protected static <T extends Annotation> T getControllerInheritedAnnotation(Class<T> clazz)
Retrieve annotation for the controller class

Parameters:
clazz - The annotation class
Returns:
Annotation object or null if not found

getControllerClass

protected static Class<?> getControllerClass()
Retrieve annotation for the action method

Returns:
Annotation object or null if not found

parent

protected static void parent(Object... args)
Call the parent action adding this objects to the params scope


parent

protected static void parent()
Call the parent method


parent

protected static void parent(Map<String,Object> map)
Call the parent action adding this objects to the params scope


suspend

protected static void suspend(String timeout)
Suspend the current request for a specified amount of time


suspend

protected static void suspend(int timeout)

waitFor

protected static void waitFor(Future<?> task)
Suspend this request and wait for the task completion


reverse

protected static Router.ActionDefinition reverse()


Guillaume Bort & zenexity - Distributed under Apache 2 licence, without any warrantly