2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/action/ExceptionHandler.java,v 1.26 2004/05/18 20:00:10 germuska Exp $
4
* $Date: 2004/05/18 20:00:10 $
6
* Copyright 2001-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.action;
23
import javax.servlet.ServletException;
24
import javax.servlet.http.HttpServletRequest;
25
import javax.servlet.http.HttpServletResponse;
27
import org.apache.commons.logging.Log;
28
import org.apache.commons.logging.LogFactory;
29
import org.apache.struts.Globals;
30
import org.apache.struts.config.ExceptionConfig;
31
import org.apache.struts.util.MessageResources;
32
import org.apache.struts.util.ModuleException;
35
* <p>An <strong>ExceptionHandler</strong> is configured in the Struts
36
* configuration file to handle a specific type of exception thrown
37
* by an <code>Action.execute</code> method.</p>
41
public class ExceptionHandler {
45
* <p>Commons logging instance.</p>
47
private static final Log log = LogFactory.getLog(ExceptionHandler.class);
51
* <p>The message resources for this package.</p>
53
private static MessageResources messages =
54
MessageResources.getMessageResources(
55
"org.apache.struts.action.LocalStrings");
59
* <p>Handle the <code>Exception</code>.
60
* Return the <code>ActionForward</code> instance (if any) returned by
61
* the called <code>ExceptionHandler</code>.
63
* @param ex The exception to handle
64
* @param ae The ExceptionConfig corresponding to the exception
65
* @param mapping The ActionMapping we are processing
66
* @param formInstance The ActionForm we are processing
67
* @param request The servlet request we are processing
68
* @param response The servlet response we are creating
70
* @exception ServletException if a servlet exception occurs
74
public ActionForward execute(
77
ActionMapping mapping,
78
ActionForm formInstance,
79
HttpServletRequest request,
80
HttpServletResponse response)
81
throws ServletException {
83
ActionForward forward = null;
84
ActionMessage error = null;
85
String property = null;
87
// Build the forward from the exception mapping if it exists
88
// or from the form input
89
if (ae.getPath() != null) {
90
forward = new ActionForward(ae.getPath());
92
forward = mapping.getInputForward();
95
// Figure out the error
96
if (ex instanceof ModuleException) {
97
error = ((ModuleException) ex).getActionMessage();
98
property = ((ModuleException) ex).getProperty();
100
error = new ActionMessage(ae.getKey(), ex.getMessage());
101
property = error.getKey();
104
this.logException(ex);
106
// Store the exception
107
request.setAttribute(Globals.EXCEPTION_KEY, ex);
108
this.storeException(request, property, error, forward, ae.getScope());
116
* <p>Logs the <code>Exception</code> using commons-logging.</p>
117
* @param e The Exception to log.
120
protected void logException(Exception e){
122
log.debug(messages.getMessage("exception.log"), e);
128
* <p>Default implementation for handling an <code>ActionError</code> generated
129
* from an <code>Exception</code> during <code>Action</code> delegation. The default
130
* implementation is to set an attribute of the request or session, as
131
* defined by the scope provided (the scope from the exception mapping). An
132
* <code>ActionErrors</code> instance is created, the error is added to the collection
133
* and the collection is set under the <code>Globals.ERROR_KEY</code>.</p>
135
* @param request The request we are handling
136
* @param property The property name to use for this error
137
* @param error The error generated from the exception mapping
138
* @param forward The forward generated from the input path (from the form or exception mapping)
139
* @param scope The scope of the exception mapping.
140
* @deprecated Use storeException(HttpServletRequest, String, ActionMessage, ActionForward, String)
141
* instead. This will be removed after Struts 1.2.
143
protected void storeException(
144
HttpServletRequest request,
147
ActionForward forward,
150
this.storeException(request, property, (ActionMessage) error, forward, scope);
151
// :TODO: Remove after Struts 1.2
157
* <p>Default implementation for handling an <code>ActionMessage</code> generated
158
* from an <code>Exception</code> during <code>Action</code> delegation. The default
159
* implementation is to set an attribute of the request or session, as
160
* defined by the scope provided (the scope from the exception mapping). An
161
* <code>ActionMessages</code> instance is created, the error is added to the
162
* collection and the collection is set under the <code>Globals.ERROR_KEY</code>.</p>
164
* @param request The request we are handling
165
* @param property The property name to use for this error
166
* @param error The error generated from the exception mapping
167
* @param forward The forward generated from the input path (from the form or exception mapping)
168
* @param scope The scope of the exception mapping.
171
protected void storeException(
172
HttpServletRequest request,
175
ActionForward forward,
178
ActionMessages errors = new ActionMessages();
179
errors.add(property, error);
181
if ("request".equals(scope)) {
182
request.setAttribute(Globals.ERROR_KEY, errors);
184
request.getSession().setAttribute(Globals.ERROR_KEY, errors);