View Javadoc

1   /**
2    * 2007, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   package pt.digitalis.dif.controller;
6   
7   import pt.digitalis.dif.controller.interfaces.IDIFRequest;
8   import pt.digitalis.dif.controller.interfaces.IDIFResponse;
9   import pt.digitalis.dif.controller.interfaces.IDispatcherErrorHandler;
10  import pt.digitalis.dif.exception.controller.BusinessFlowException;
11  import pt.digitalis.dif.exception.controller.ControllerException;
12  
13  /**
14   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
15   * @created Dec 21, 2007
16   */
17  abstract public class AbstractControllerErrorHandler implements IDispatcherErrorHandler {
18  
19      /**
20       * Builds an error response from a stage id for error handling, with a defined handler stage (or the default one)
21       * 
22       * @param originalrequest
23       *            the original request
24       * @param errorStageID
25       *            the error stage to redirect to. If null will redirect to the default error stage
26       * @param exception
27       *            the exception launched
28       * @return the response
29       */
30      abstract protected IDIFResponse buildDynamicErrorResponse(IDIFRequest originalrequest, String errorStageID,
31              Exception exception);
32  
33      /**
34       * Builds an error response from a static resource for simple rendering for when the DIF has generated errors
35       * 
36       * @param originalrequest
37       *            the original request
38       * @param exception
39       *            the exception launched
40       * @return the response
41       */
42      abstract protected IDIFResponse buildStaticErrorResponse(IDIFRequest originalrequest, Exception exception);
43  
44      /**
45       * @see pt.digitalis.dif.controller.interfaces.IDispatcherErrorHandler#processException(pt.digitalis.dif.controller.interfaces.IDIFRequest,
46       *      pt.digitalis.dif.exception.controller.BusinessFlowException)
47       */
48      public IDIFResponse processException(IDIFRequest originalRequest, BusinessFlowException businessFlowException)
49      {
50  
51          return buildDynamicErrorResponse(originalRequest, ExceptionHandlers.getExceptionHandler(businessFlowException),
52                  businessFlowException);
53      }
54  
55      /**
56       * @see pt.digitalis.dif.controller.interfaces.IDispatcherErrorHandler#processException(pt.digitalis.dif.controller.interfaces.IDIFRequest,
57       *      pt.digitalis.dif.exception.controller.ControllerException)
58       */
59      public IDIFResponse processException(IDIFRequest originalRequest, ControllerException controllerException)
60      {
61          if (controllerException.isGeneratedFromException())
62              return buildStaticErrorResponse(originalRequest, controllerException);
63          else
64              return buildDynamicErrorResponse(originalRequest, ExceptionHandlers
65                      .getExceptionHandler(controllerException), controllerException);
66      }
67  
68      /**
69       * @see pt.digitalis.dif.controller.interfaces.IDispatcherErrorHandler#processException(pt.digitalis.dif.controller.interfaces.IDIFRequest,
70       *      java.lang.RuntimeException)
71       */
72      public IDIFResponse processException(IDIFRequest originalRequest, RuntimeException runtimeException)
73      {
74          return buildDynamicErrorResponse(originalRequest, ExceptionHandlers.getExceptionHandler(runtimeException),
75                  runtimeException);
76      }
77  
78      /**
79       * Creates a new error request base on the original request. Injects the original request in the error one for later
80       * reference in the error stage.
81       * 
82       * @param response
83       *            the response to add the context to
84       * @param originalRequest
85       *            the original request
86       * @param exception
87       *            the exception launched
88       * @return the request with the context added
89       */
90      public IDIFResponse responseWithContext(IDIFResponse response, IDIFRequest originalRequest, Exception exception)
91      {
92          response.addStageResult(IDispatcherErrorHandler.ORIGINAL_REQUEST, originalRequest);
93          response.addStageResult(IDispatcherErrorHandler.EXCEPTION, exception);
94  
95          if (response.getRequest() != null)
96              response.getRequest().addAttribute(ExceptionHandlers.RAISED_EXCEPTION_ATTRIBUTE, exception);
97  
98          return response;
99      }
100 }