~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/action/ExceptionHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.26 $
 
4
 * $Date: 2004/05/18 20:00:10 $
 
5
 *
 
6
 * Copyright 2001-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.action;
 
22
 
 
23
import javax.servlet.ServletException;
 
24
import javax.servlet.http.HttpServletRequest;
 
25
import javax.servlet.http.HttpServletResponse;
 
26
 
 
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;
 
33
 
 
34
/**
 
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>
 
38
 * 
 
39
 * @since Struts 1.1
 
40
 */
 
41
public class ExceptionHandler {
 
42
    
 
43
 
 
44
    /**
 
45
     * <p>Commons logging instance.</p>
 
46
     */
 
47
    private static final Log log = LogFactory.getLog(ExceptionHandler.class);
 
48
    
 
49
 
 
50
    /**
 
51
     * <p>The message resources for this package.</p>
 
52
     */
 
53
    private static MessageResources messages =
 
54
        MessageResources.getMessageResources(
 
55
            "org.apache.struts.action.LocalStrings");
 
56
    
 
57
 
 
58
    /**
 
59
     * <p>Handle the <code>Exception</code>.
 
60
     * Return the <code>ActionForward</code> instance (if any) returned by
 
61
     * the called <code>ExceptionHandler</code>.
 
62
     *
 
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
 
69
     *
 
70
     * @exception ServletException if a servlet exception occurs
 
71
     *
 
72
     * @since Struts 1.1
 
73
     */
 
74
    public ActionForward execute(
 
75
        Exception ex,
 
76
        ExceptionConfig ae,
 
77
        ActionMapping mapping,
 
78
        ActionForm formInstance,
 
79
        HttpServletRequest request,
 
80
        HttpServletResponse response)
 
81
        throws ServletException {
 
82
 
 
83
        ActionForward forward = null;
 
84
        ActionMessage error = null;
 
85
        String property = null;
 
86
 
 
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());
 
91
        } else {
 
92
            forward = mapping.getInputForward();
 
93
        }
 
94
 
 
95
        // Figure out the error
 
96
        if (ex instanceof ModuleException) {
 
97
            error = ((ModuleException) ex).getActionMessage();
 
98
            property = ((ModuleException) ex).getProperty();
 
99
        } else {
 
100
            error = new ActionMessage(ae.getKey(), ex.getMessage());
 
101
            property = error.getKey();
 
102
        }
 
103
 
 
104
        this.logException(ex);
 
105
 
 
106
        // Store the exception
 
107
        request.setAttribute(Globals.EXCEPTION_KEY, ex);
 
108
        this.storeException(request, property, error, forward, ae.getScope());
 
109
 
 
110
        return forward;
 
111
 
 
112
    }
 
113
 
 
114
 
 
115
    /**
 
116
     * <p>Logs the <code>Exception</code> using commons-logging.</p>
 
117
     * @param e The Exception to log.
 
118
     * @since Struts 1.2
 
119
     */
 
120
    protected void logException(Exception e){
 
121
 
 
122
        log.debug(messages.getMessage("exception.log"), e);
 
123
 
 
124
    }
 
125
 
 
126
 
 
127
    /**
 
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>
 
134
     *
 
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.
 
142
     */
 
143
    protected void storeException(
 
144
        HttpServletRequest request,
 
145
        String property,
 
146
        ActionError error,
 
147
        ActionForward forward,
 
148
        String scope) {
 
149
 
 
150
        this.storeException(request, property, (ActionMessage) error, forward, scope);
 
151
        // :TODO: Remove after Struts 1.2
 
152
 
 
153
    }
 
154
 
 
155
 
 
156
    /**
 
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>
 
163
     *
 
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.
 
169
     * @since Struts 1.2
 
170
     */
 
171
    protected void storeException(
 
172
        HttpServletRequest request,
 
173
        String property,
 
174
        ActionMessage error,
 
175
        ActionForward forward,
 
176
        String scope) {
 
177
 
 
178
        ActionMessages errors = new ActionMessages();
 
179
        errors.add(property, error);
 
180
 
 
181
        if ("request".equals(scope)) {
 
182
            request.setAttribute(Globals.ERROR_KEY, errors);
 
183
        } else {
 
184
            request.getSession().setAttribute(Globals.ERROR_KEY, errors);
 
185
        }
 
186
    }
 
187
 
 
188
}
 
189