KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > ActionExceptionHandler


1 package org.appfuse.webapp.action;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.io.StringWriter JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.acegisecurity.AccessDeniedException;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.struts.Globals;
16 import org.apache.struts.action.ActionErrors;
17 import org.apache.struts.action.ActionForm;
18 import org.apache.struts.action.ActionForward;
19 import org.apache.struts.action.ActionMapping;
20 import org.apache.struts.action.ActionMessage;
21 import org.apache.struts.action.ActionMessages;
22 import org.apache.struts.action.ExceptionHandler;
23 import org.apache.struts.config.ExceptionConfig;
24
25
26 /**
27  * Implementation of <strong>ExceptionHandler</strong> that
28  * handles any Exceptions that are bubbled up to the Action
29  * layer. This allows us to remove generic try/catch statements
30  * from our Action Classes.
31  *
32  * <p>
33  * <a HREF="ActionExceptionHandler.java.htm"><i>View Source</i></a>
34  * </p>
35  *
36  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
37  */

38 public final class ActionExceptionHandler extends ExceptionHandler {
39     //~ Instance fields ========================================================
40

41     private transient final Log log = LogFactory.getLog(ActionExceptionHandler.class);
42
43     //~ Methods ================================================================
44

45     /**
46      * This method handles any java.lang.Exceptions that are not
47      * caught in previous classes. It will loop through and get
48      * all the causes (exception chain), create ActionErrors,
49      * add them to the request and then forward to the input.
50      *
51      * @see org.apache.struts.action.ExceptionHandler#execute
52      * (
53      * java.lang.Exception,
54      * org.apache.struts.config.ExceptionConfig,
55      * org.apache.struts.action.ActionMapping,
56      * org.apache.struts.action.ActionForm,
57      * javax.servlet.http.HttpServletRequest,
58      * javax.servlet.http.HttpServletResponse
59      * )
60      */

61     public ActionForward execute(Exception JavaDoc ex, ExceptionConfig ae,
62                                  ActionMapping mapping,
63                                  ActionForm formInstance,
64                                  HttpServletRequest JavaDoc request,
65                                  HttpServletResponse JavaDoc response)
66     throws ServletException JavaDoc {
67         // if there's already errors in the request, don't process
68
ActionErrors errors =
69             (ActionErrors) request.getAttribute(Globals.ERROR_KEY);
70
71         if (errors != null) {
72             return null;
73         }
74
75         ActionForward forward =
76             super.execute(ex, ae, mapping, formInstance, request, response);
77
78         ActionMessage error = null;
79         String JavaDoc property = null;
80
81         if (ex instanceof AccessDeniedException && forward == null) {
82             storeException(request, "", new ActionMessage("errors.detail", ex.getMessage()), forward);
83             try {
84                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
85                 return null;
86             } catch (IOException JavaDoc io) {
87                 io.printStackTrace();
88                 log.error(io.getMessage());
89             }
90         }
91         
92         // Get the chained exceptions (causes) and add them to the
93
// list of errors as well
94
while (ex != null) {
95             String JavaDoc msg = ex.getMessage();
96             error = new ActionMessage("errors.detail", msg);
97             property = error.getKey();
98             ex = (Exception JavaDoc) ex.getCause();
99
100             if ((ex != null) && (ex.getMessage() != null)) {
101                 // check to see if the child message is the same
102
// if so, don't store it
103
if (msg.indexOf(ex.getMessage()) == -1) {
104                     storeException(request, property, error, forward);
105                 }
106             } else {
107                 storeException(request, property, error, forward);
108             }
109         }
110
111         return forward;
112     }
113
114     /**
115      * This method overrides the the ExceptionHandler's storeException
116      * method in order to create more than one error message.
117      *
118      * @param request - The request we are handling
119      * @param property - The property name to use for this error
120      * @param error - The error generated from the exception mapping
121      * @param forward - The forward generated from the input path
122      * (from the form or exception mapping)
123      */

124     protected void storeException(HttpServletRequest JavaDoc request, String JavaDoc property,
125                                   ActionMessage error, ActionForward forward) {
126         ActionMessages errors =
127             (ActionMessages) request.getAttribute(Globals.ERROR_KEY);
128
129         if (errors == null) {
130             errors = new ActionMessages();
131         }
132
133         errors.add(property, error);
134
135         request.setAttribute(Globals.ERROR_KEY, errors);
136     }
137
138     /**
139      * Overrides logException method in ExceptionHandler to print the stackTrace
140      * @see org.apache.struts.action.ExceptionHandler#logException(java.lang.Exception)
141      */

142     protected void logException(Exception JavaDoc ex) {
143         StringWriter JavaDoc sw = new StringWriter JavaDoc();
144         ex.printStackTrace(new PrintWriter JavaDoc(sw));
145         log.error(sw.toString());
146     }
147 }
148
Popular Tags