KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > AbstractRequestProcessor


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23
24 package org.mdarad.framework.util.struts;
25
26
27 import org.apache.commons.beanutils.ConvertUtils;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessages;
34 import org.apache.struts.action.ActionServlet;
35 import org.apache.struts.config.ForwardConfig;
36 import org.apache.struts.config.ModuleConfig;
37 import org.apache.struts.tiles.TilesRequestProcessor;
38 import org.mdarad.framework.exception.SystemException;
39
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45 import java.net.URI JavaDoc;
46 import java.util.Currency JavaDoc;
47 import java.util.Locale JavaDoc;
48
49 public abstract class AbstractRequestProcessor extends org.apache.struts.action.SecureTilesRequestProcessor {
50     private final static String JavaDoc EXCEPTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.EXCEPTION_KEY";
51     private final static String JavaDoc ACTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.ACTION_KEY";
52
53     /**
54      * @see TilesRequestProcessor#processActionPerform
55      */

56     protected ActionForward processActionPerform(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Action action, ActionForm actionForm, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc {
57         StrutsChainedException exception = (StrutsChainedException) httpServletRequest.getAttribute(EXCEPTION_KEY);
58         if (exception != null) {
59             return processException(httpServletRequest, httpServletResponse, (Exception JavaDoc) exception.getThrowable(0), actionForm, actionMapping);
60         }
61         return super.processActionPerform(httpServletRequest, httpServletResponse, action, actionForm, actionMapping);
62     }
63
64     protected ActionForward processException(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Exception JavaDoc e, ActionForm actionForm, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc {
65         e.printStackTrace(System.err);
66         return super.processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping); //To change body of overridden methods use File | Settings | File Templates.
67
}
68
69
70     public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException JavaDoc {
71             StrutsConverter strutsConverter = new StrutsConverter();
72         ConvertUtils.register(strutsConverter, Locale JavaDoc.class);
73         ConvertUtils.register(strutsConverter, Currency JavaDoc.class);
74         ConvertUtils.register(strutsConverter, URI JavaDoc.class);
75         ConvertUtils.register(strutsConverter, Integer JavaDoc.class);
76         super.init(servlet, moduleConfig);
77     }
78
79     static public void chainException(HttpServletRequest JavaDoc httpServletRequest, Throwable JavaDoc throwable) {
80         StrutsChainedException exception = (StrutsChainedException) httpServletRequest.getAttribute(EXCEPTION_KEY);
81         if (exception == null) exception = new StrutsChainedException();
82         exception.chain(throwable);
83         httpServletRequest.setAttribute(EXCEPTION_KEY, exception);
84     }
85
86     protected Action processActionCreate(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, ActionMapping actionMapping) throws IOException JavaDoc {
87         Action action = (Action) httpServletRequest.getAttribute(ACTION_KEY);
88         if (action == null) action = super.processActionCreate(httpServletRequest, httpServletResponse, actionMapping);
89         return action;
90     }
91
92     protected void processLocale(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) {
93         try {
94             httpServletRequest.setCharacterEncoding("UTF-8");
95         } catch (UnsupportedEncodingException JavaDoc e) {
96             throw new RuntimeException JavaDoc(e);
97         }
98         super.processLocale(httpServletRequest, httpServletResponse);
99     }
100
101     protected boolean processRoles(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc {
102         return true;
103     }
104
105     protected abstract boolean processAuthenticationRoles(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc;
106
107     protected boolean processLocalization(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc {
108         try {
109             StrutsLocalizationUtils.processLocale(httpServletRequest, "org.mdarad.framework.util.struts.AbstractAction.webLocalizationContext");
110         } catch (Exception JavaDoc e) {
111             processForwardConfig(httpServletRequest, httpServletResponse, processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping));
112             return false;
113         }
114         return true;
115     }
116
117     protected boolean processInvalidForm(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, ActionForm actionForm, ActionMapping actionMapping) throws IOException JavaDoc, ServletException JavaDoc {
118         Action action = super.processActionCreate(httpServletRequest, httpServletResponse, actionMapping);
119         httpServletRequest.setAttribute(ACTION_KEY, action);
120
121         if (action instanceof InvalidFormRequestPopulator) {
122             InvalidFormRequestPopulator requestPopulatorAction = (InvalidFormRequestPopulator) action;
123             try {
124                 requestPopulatorAction.populateRequestOnInvalidForm(httpServletRequest, actionForm);
125             } catch (SystemException e) {
126                 processForwardConfig(httpServletRequest, httpServletResponse, processException(httpServletRequest, httpServletResponse, e, actionForm, actionMapping));
127                 return false;
128             }
129         }
130         return true;
131     }
132
133     protected boolean processValidate(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionForm form, ActionMapping mapping) throws IOException JavaDoc, ServletException JavaDoc {
134         if (!processLocalization(request, response, form, mapping)) {
135             return false;
136         }
137         if (!processAuthenticationRoles(request, response, form, mapping)) {
138             return false;
139         }
140
141         if (form == null) {
142             return (true);
143         }
144
145         // Was this request cancelled?
146
if (request.getAttribute(Globals.CANCEL_KEY) != null) {
147             return (true);
148         }
149
150         // Has validation been turned off for this mapping?
151
if (!mapping.getValidate()) {
152             return (true);
153         }
154
155         // Call the form bean's validation method
156
ActionMessages errors = form.validate(mapping, request);
157         if ((errors == null) || errors.isEmpty()) {
158             return (true);
159         } else {
160             if (!processInvalidForm(request, response, form, mapping)) {
161                 return false;
162             }
163         }
164
165         // Special handling for multipart request
166
if (form.getMultipartRequestHandler() != null) {
167             form.getMultipartRequestHandler().rollback();
168         }
169
170         // Was an input path (or forward) specified for this mapping?
171
String JavaDoc input = mapping.getInput();
172         if (input == null) {
173             response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
174                     getInternal().getMessage("noInput",
175                             mapping.getPath()));
176             return (false);
177         }
178
179         // Save our error messages and return to the input form if possible
180
request.setAttribute(Globals.ERROR_KEY, errors);
181
182         if (moduleConfig.getControllerConfig().getInputForward()) {
183             ForwardConfig forward = mapping.findForward(input);
184             processForwardConfig(request, response, forward);
185         } else {
186             internalModuleRelativeForward(input, request, response);
187         }
188
189         return (false);
190     }
191 }
Popular Tags