001: /*
002: Mdarad-Toolobox is a collection of tools for Architected RAD
003: (Rapid Application Development) based on an MDA approach.
004: The toolbox contains frameworks and generators for many environments
005: (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
006: applications from a design Model
007: Copyright (C) 2004-2005 Elapse Technologies Inc.
008:
009: This library is free software; you can redistribute it and/or
010: modify it under the terms of the GNU General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: General Public License for more details.
018:
019: You should have received a copy of the GNU General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023:
024: package org.mdarad.framework.util.struts;
025:
026: import org.apache.commons.beanutils.ConvertUtils;
027: import org.apache.struts.Globals;
028: import org.apache.struts.action.Action;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionForward;
031: import org.apache.struts.action.ActionMapping;
032: import org.apache.struts.action.ActionMessages;
033: import org.apache.struts.action.ActionServlet;
034: import org.apache.struts.config.ForwardConfig;
035: import org.apache.struts.config.ModuleConfig;
036: import org.apache.struts.tiles.TilesRequestProcessor;
037: import org.mdarad.framework.exception.SystemException;
038:
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043: import java.io.UnsupportedEncodingException;
044: import java.net.URI;
045: import java.util.Currency;
046: import java.util.Locale;
047:
048: public abstract class AbstractRequestProcessor extends
049: org.apache.struts.action.SecureTilesRequestProcessor {
050: private final static String EXCEPTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.EXCEPTION_KEY";
051: private final static String ACTION_KEY = "org.mdarad.framework.util.struts.AbstractRequestProcessor.ACTION_KEY";
052:
053: /**
054: * @see TilesRequestProcessor#processActionPerform
055: */
056: protected ActionForward processActionPerform(
057: HttpServletRequest httpServletRequest,
058: HttpServletResponse httpServletResponse, Action action,
059: ActionForm actionForm, ActionMapping actionMapping)
060: throws IOException, ServletException {
061: StrutsChainedException exception = (StrutsChainedException) httpServletRequest
062: .getAttribute(EXCEPTION_KEY);
063: if (exception != null) {
064: return processException(httpServletRequest,
065: httpServletResponse, (Exception) exception
066: .getThrowable(0), actionForm, actionMapping);
067: }
068: return super .processActionPerform(httpServletRequest,
069: httpServletResponse, action, actionForm, actionMapping);
070: }
071:
072: protected ActionForward processException(
073: HttpServletRequest httpServletRequest,
074: HttpServletResponse httpServletResponse, Exception e,
075: ActionForm actionForm, ActionMapping actionMapping)
076: throws IOException, ServletException {
077: e.printStackTrace(System.err);
078: return super .processException(httpServletRequest,
079: httpServletResponse, e, actionForm, actionMapping); //To change body of overridden methods use File | Settings | File Templates.
080: }
081:
082: public void init(ActionServlet servlet, ModuleConfig moduleConfig)
083: throws ServletException {
084: StrutsConverter strutsConverter = new StrutsConverter();
085: ConvertUtils.register(strutsConverter, Locale.class);
086: ConvertUtils.register(strutsConverter, Currency.class);
087: ConvertUtils.register(strutsConverter, URI.class);
088: ConvertUtils.register(strutsConverter, Integer.class);
089: super .init(servlet, moduleConfig);
090: }
091:
092: static public void chainException(
093: HttpServletRequest httpServletRequest, Throwable throwable) {
094: StrutsChainedException exception = (StrutsChainedException) httpServletRequest
095: .getAttribute(EXCEPTION_KEY);
096: if (exception == null)
097: exception = new StrutsChainedException();
098: exception.chain(throwable);
099: httpServletRequest.setAttribute(EXCEPTION_KEY, exception);
100: }
101:
102: protected Action processActionCreate(
103: HttpServletRequest httpServletRequest,
104: HttpServletResponse httpServletResponse,
105: ActionMapping actionMapping) throws IOException {
106: Action action = (Action) httpServletRequest
107: .getAttribute(ACTION_KEY);
108: if (action == null)
109: action = super .processActionCreate(httpServletRequest,
110: httpServletResponse, actionMapping);
111: return action;
112: }
113:
114: protected void processLocale(HttpServletRequest httpServletRequest,
115: HttpServletResponse httpServletResponse) {
116: try {
117: httpServletRequest.setCharacterEncoding("UTF-8");
118: } catch (UnsupportedEncodingException e) {
119: throw new RuntimeException(e);
120: }
121: super .processLocale(httpServletRequest, httpServletResponse);
122: }
123:
124: protected boolean processRoles(
125: HttpServletRequest httpServletRequest,
126: HttpServletResponse httpServletResponse,
127: ActionMapping actionMapping) throws IOException,
128: ServletException {
129: return true;
130: }
131:
132: protected abstract boolean processAuthenticationRoles(
133: HttpServletRequest httpServletRequest,
134: HttpServletResponse httpServletResponse,
135: ActionForm actionForm, ActionMapping actionMapping)
136: throws IOException, ServletException;
137:
138: protected boolean processLocalization(
139: HttpServletRequest httpServletRequest,
140: HttpServletResponse httpServletResponse,
141: ActionForm actionForm, ActionMapping actionMapping)
142: throws IOException, ServletException {
143: try {
144: StrutsLocalizationUtils
145: .processLocale(httpServletRequest,
146: "org.mdarad.framework.util.struts.AbstractAction.webLocalizationContext");
147: } catch (Exception e) {
148: processForwardConfig(httpServletRequest,
149: httpServletResponse, processException(
150: httpServletRequest, httpServletResponse, e,
151: actionForm, actionMapping));
152: return false;
153: }
154: return true;
155: }
156:
157: protected boolean processInvalidForm(
158: HttpServletRequest httpServletRequest,
159: HttpServletResponse httpServletResponse,
160: ActionForm actionForm, ActionMapping actionMapping)
161: throws IOException, ServletException {
162: Action action = super .processActionCreate(httpServletRequest,
163: httpServletResponse, actionMapping);
164: httpServletRequest.setAttribute(ACTION_KEY, action);
165:
166: if (action instanceof InvalidFormRequestPopulator) {
167: InvalidFormRequestPopulator requestPopulatorAction = (InvalidFormRequestPopulator) action;
168: try {
169: requestPopulatorAction.populateRequestOnInvalidForm(
170: httpServletRequest, actionForm);
171: } catch (SystemException e) {
172: processForwardConfig(httpServletRequest,
173: httpServletResponse, processException(
174: httpServletRequest,
175: httpServletResponse, e, actionForm,
176: actionMapping));
177: return false;
178: }
179: }
180: return true;
181: }
182:
183: protected boolean processValidate(HttpServletRequest request,
184: HttpServletResponse response, ActionForm form,
185: ActionMapping mapping) throws IOException, ServletException {
186: if (!processLocalization(request, response, form, mapping)) {
187: return false;
188: }
189: if (!processAuthenticationRoles(request, response, form,
190: mapping)) {
191: return false;
192: }
193:
194: if (form == null) {
195: return (true);
196: }
197:
198: // Was this request cancelled?
199: if (request.getAttribute(Globals.CANCEL_KEY) != null) {
200: return (true);
201: }
202:
203: // Has validation been turned off for this mapping?
204: if (!mapping.getValidate()) {
205: return (true);
206: }
207:
208: // Call the form bean's validation method
209: ActionMessages errors = form.validate(mapping, request);
210: if ((errors == null) || errors.isEmpty()) {
211: return (true);
212: } else {
213: if (!processInvalidForm(request, response, form, mapping)) {
214: return false;
215: }
216: }
217:
218: // Special handling for multipart request
219: if (form.getMultipartRequestHandler() != null) {
220: form.getMultipartRequestHandler().rollback();
221: }
222:
223: // Was an input path (or forward) specified for this mapping?
224: String input = mapping.getInput();
225: if (input == null) {
226: response.sendError(
227: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
228: getInternal().getMessage("noInput",
229: mapping.getPath()));
230: return (false);
231: }
232:
233: // Save our error messages and return to the input form if possible
234: request.setAttribute(Globals.ERROR_KEY, errors);
235:
236: if (moduleConfig.getControllerConfig().getInputForward()) {
237: ForwardConfig forward = mapping.findForward(input);
238: processForwardConfig(request, response, forward);
239: } else {
240: internalModuleRelativeForward(input, request, response);
241: }
242:
243: return (false);
244: }
245: }
|