net.sf.easyweb4j.controller
Class Controller

java.lang.Object
  extended by net.sf.easyweb4j.controller.Controller
All Implemented Interfaces:
Serializable

public abstract class Controller
extends Object
implements Serializable

Parent Controller which should be extended by all application controllers. It provides several convenience methods for the sub-classes to use.

Author:
Chandra Sekar S
See Also:
Serialized Form

Field Summary
protected static String DELETE
           
protected static String GET
           
protected static String HEAD
           
protected static String POST
           
protected static String PUT
           
 
Constructor Summary
Controller()
           
 
Method Summary
 void filterRequest()
          This method is invoked at the beginning of each request.
protected
<T extends Annotation>
T
getActionAnnotation(Class<T> annotationClass)
          Returns action method's annotation for the specified type if such an annotation is present, else null.
protected  Annotation[] getActionAnnotations()
          Returns all the annotations on the action method used in the current request.
protected  String getActionInvoked()
          Returns the name of the action invoked by the current request.
 ActionResult getActionResult()
          Returns the ActionResult instance for the current request.
protected  String getControllerName()
          Returns the name of the controller handling the current request.
 org.apache.commons.fileupload.FileItem getFileItem(String paramName)
          Gets the FileItem with the given parameter name.
 Map<String,org.apache.commons.fileupload.FileItem[]> getFileItemMap()
          Returns a map of the FileItems in the current request.
 org.apache.commons.fileupload.FileItem[] getFileItems(String paramName)
          Gets all the FileItems with the given parameter name.
 String getMethod()
          Returns the HTTP method of the current request.
 String getParameter(String paramName)
          Gets the value of the specified request parameter.
 Map<String,String[]> getParameterMap()
          Returns a map of the parameters in the current request.
 String[] getParameterValues(String paramName)
          Gets the values of the specified multi-valued request parameter.
 javax.servlet.http.HttpServletRequest getRequest()
          Returns the current HttpServletRequest.
 Object getRequestAttribute(String attributeName)
          Retrieves the value of a request attribute.
 javax.servlet.http.HttpServletResponse getResponse()
          Returns the current HttpServletResponse.
 javax.servlet.ServletContext getServletContext()
          Returns the current ServletContext.
 Object getSessionAttribute(String attributeName)
          Retrieves the value of a session attribute.
 void handleActionException(Exception e)
          Callback to handle exceptions which occurred during execution of the action for the request, but were not handled within the action.
 void invalidateSession()
          Invalidates the current HTTP session.
 boolean isNavigationInitiated()
          Determines whether navigation has been initialized for the current request or not.
 void redirect(String action)
          Redirects to the specified action within the same controller.
 void redirect(String controller, String action)
          Redirects to the specified controller and action.
 void redirect(String controller, String action, String id)
          Redirects to the specified controller, action and id combination.
 void removeRequestAttribute(String attributeName)
          Removes the specified request attribute.
 void removeSessionAttribute(String attributeName)
          Removes the specified session attribute.
 void render(ResponseHandler responseHandler)
          Sets a ResponseHandler which deals with the view rendering.
 void render(String view)
          Changes the view to be rendered for the action to the given view.
 void renderWithoutLayout()
          Ensure that the default view of the action is rendered without any layout at the end of the action.
 void renderWithoutLayout(String view)
          Renders the specified view at the end of the action without any layout.
 void setFlash(String key, Serializable value)
          Sets a flash entry.
 void setRequestAttribute(String attributeName, Object attributeValue)
          Sets the value of a request attribute.
 void setSessionAttribute(String attributeName, Serializable attributeValue)
          Sets the value of a session attribute.
protected
<T extends Model>
T
updateModel(T model, String objectParameter)
          Same as updateModel(model, objectParameter, null);
protected
<T extends Model>
T
updateModel(T model, String objectParameter, AssociationsProvider associationsProvider)
          Updates the given model with the values specified by the object parameter and validates the model.
 void uploadProgress(long read, long length, int item)
          This callback is notified of progress in file uploads as the multi-part request is being parsed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET

protected static final String GET
See Also:
Constant Field Values

POST

protected static final String POST
See Also:
Constant Field Values

PUT

protected static final String PUT
See Also:
Constant Field Values

DELETE

protected static final String DELETE
See Also:
Constant Field Values

HEAD

protected static final String HEAD
See Also:
Constant Field Values
Constructor Detail

Controller

public Controller()
Method Detail

filterRequest

public void filterRequest()
                   throws Exception
This method is invoked at the beginning of each request. Sub-classes can override this method to implement behaviors which precede the invocation of actions in the controller. This can be used as a simpler form of request filter.

Throws:
Exception - If the filter doesn't want to handle exceptions.

handleActionException

public void handleActionException(Exception e)
                           throws Exception
Callback to handle exceptions which occurred during execution of the action for the request, but were not handled within the action. This callback is invoked after the action transaction for the request is rolled back.

Controllers must override this method if needed to handle exceptions.

Parameters:
e - The exception which occured within the action or during the action's transaction.
Throws:
Exception - May throw an exception back to the servlet container.

uploadProgress

public void uploadProgress(long read,
                           long length,
                           int item)
This callback is notified of progress in file uploads as the multi-part request is being parsed.

Note: The argument descriptions are taken from commons FileUpload's JavaDocs.

Parameters:
read - The total number of bytes, which have been read so far.
length - The total number of bytes, which are being read. May be -1, if this number is unknown.
item - The number of the field, which is currently being read. (0 = no item so far, 1 = first item is being read, ...)

getParameter

public String getParameter(String paramName)
Gets the value of the specified request parameter.

Parameters:
paramName - The name of the parameter, whose value is to be retrieved.
Returns:
The value of the request parameter.

getParameterValues

public String[] getParameterValues(String paramName)
Gets the values of the specified multi-valued request parameter.

Parameters:
paramName - The name of the parameter, whose values are to be retrieved.
Returns:
The values of the request parameter.

getParameterMap

public Map<String,String[]> getParameterMap()
Returns a map of the parameters in the current request.

Returns:
A map of request parameters.

getFileItemMap

public Map<String,org.apache.commons.fileupload.FileItem[]> getFileItemMap()
Returns a map of the FileItems in the current request.

Returns:
A map of FileItems.

getFileItem

public org.apache.commons.fileupload.FileItem getFileItem(String paramName)
Gets the FileItem with the given parameter name.

Parameters:
paramName - The name of the FileItem parameter.
Returns:
The FileItem.

getFileItems

public org.apache.commons.fileupload.FileItem[] getFileItems(String paramName)
Gets all the FileItems with the given parameter name.

Parameters:
paramName - The name of the FileItem parameter.
Returns:
The FileItems.

updateModel

protected <T extends Model> T updateModel(T model,
                                          String objectParameter)
Same as updateModel(model, objectParameter, null);

See Also:
updateModel(Model, String, AssociationsProvider)

updateModel

protected <T extends Model> T updateModel(T model,
                                          String objectParameter,
                                          AssociationsProvider associationsProvider)
Updates the given model with the values specified by the object parameter and validates the model. If the model is invalid, it returns a copy of the model with errors, else returns the original object.

Type Parameters:
T - The type of the model.
Parameters:
model - The model to be updated.
objectParameter - the object parameter's name.
associationsProvider - An implementation of AssociationsProvider.
Returns:
Original model or copy of the model if the input is invalid.

setRequestAttribute

public void setRequestAttribute(String attributeName,
                                Object attributeValue)
Sets the value of a request attribute.

Parameters:
attributeName - The name of the attribute to be set.
attributeValue - The value to set for the attribute.

getRequestAttribute

public Object getRequestAttribute(String attributeName)
Retrieves the value of a request attribute.

Parameters:
attributeName - The name of the attribute to be retrieved.
Returns:
The value of the request attribute.

removeRequestAttribute

public void removeRequestAttribute(String attributeName)
Removes the specified request attribute.

Parameters:
attributeName - The name of the attribute to be removed.

setSessionAttribute

public void setSessionAttribute(String attributeName,
                                Serializable attributeValue)
Sets the value of a session attribute.

Parameters:
attributeName - The name of the attribute to be set.
attributeValue - The value to set for the attribute.

getSessionAttribute

public Object getSessionAttribute(String attributeName)
Retrieves the value of a session attribute.

Parameters:
attributeName - The name of the attribute to be retrieved.
Returns:
The value of the session attribute.

removeSessionAttribute

public void removeSessionAttribute(String attributeName)
Removes the specified session attribute.

Parameters:
attributeName - The name of the attribute to be removed.

invalidateSession

public void invalidateSession()
Invalidates the current HTTP session.


render

public void render(String view)
Changes the view to be rendered for the action to the given view.

Note: This method does not commit the servlet's response

Parameters:
view - The view to be rendered at the end of the action.

render

public void render(ResponseHandler responseHandler)
Sets a ResponseHandler which deals with the view rendering.

Note: This method does not commit the servlet's response

Parameters:
responseHandler - The ResponseHandler to be used for rendering the view.

renderWithoutLayout

public void renderWithoutLayout()
Ensure that the default view of the action is rendered without any layout at the end of the action.

Note: This method does not commit the servlet's response


renderWithoutLayout

public void renderWithoutLayout(String view)
Renders the specified view at the end of the action without any layout.

Note: This method does not commit the servlet's response

Parameters:
view - The view to be rendered at the end of the action.

redirect

public void redirect(String action)
Redirects to the specified action within the same controller. If the action's name starts with an upper-case character, it is considered to be a name of a controller and the default action (currently "index"), is invoked on that controller.

Note: This method does not commit the servlet's response

Parameters:
action - The action/controller to redirect to.

redirect

public void redirect(String controller,
                     String action)
Redirects to the specified controller and action.

Note: This method does not commit the servlet's response

Parameters:
controller - The controller to whose action response should be redirected.
action - The action in the controller to which the response should be redirected.

redirect

public void redirect(String controller,
                     String action,
                     String id)
Redirects to the specified controller, action and id combination.

Note: This method does not commit the servlet's response

Parameters:
controller - The controller to whose action response should be redirected.
action - The action in the controller to which the response should be redirected.
id - The object id to be appended to the redirection URL.

getMethod

public String getMethod()
Returns the HTTP method of the current request.

Returns:
The HTTP method.

setFlash

public void setFlash(String key,
                     Serializable value)
Sets a flash entry. Flash entries are retained for exactly one request after the request in which they were set.

Parameters:
key - The key to identify the entry.
value - The value for the entry.

getRequest

public javax.servlet.http.HttpServletRequest getRequest()
Returns the current HttpServletRequest.

Returns:
The current HttpServletRequest.

getResponse

public javax.servlet.http.HttpServletResponse getResponse()
Returns the current HttpServletResponse.

Returns:
The current HttpServletResponse.

getServletContext

public javax.servlet.ServletContext getServletContext()
Returns the current ServletContext.

Returns:
The current ServletContext.

getActionResult

public ActionResult getActionResult()
Returns the ActionResult instance for the current request.

Returns:
The ActionResult of current request.

isNavigationInitiated

public boolean isNavigationInitiated()
Determines whether navigation has been initialized for the current request or not. A navigation may initiated either using one of the navigation methods or by directly writing to servlet's response.

Returns:
true if navigation has been initiated false otherwise.

getActionInvoked

protected String getActionInvoked()
Returns the name of the action invoked by the current request.

Returns:
The name of the action.

getActionAnnotations

protected Annotation[] getActionAnnotations()
Returns all the annotations on the action method used in the current request.

Returns:
An array of all annotations.
See Also:
AccessibleObject.getAnnotations()

getActionAnnotation

protected <T extends Annotation> T getActionAnnotation(Class<T> annotationClass)
Returns action method's annotation for the specified type if such an annotation is present, else null.

Parameters:
annotationClass - the Class object corresponding to the annotation type
Returns:
Action method's annotation of specified type.

getControllerName

protected String getControllerName()
Returns the name of the controller handling the current request.

Returns:
The name of the controller.


Copyright © 2009-2010 Chandra Sekar S. All Rights Reserved.