KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > ExpressoRequestProcessor


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.controller;
65
66 import com.jcorporate.expresso.core.misc.ConfigManager;
67 import org.apache.commons.logging.Log;
68 import org.apache.commons.logging.LogFactory;
69 import org.apache.struts.action.Action;
70 import org.apache.struts.action.ActionForm;
71 import org.apache.struts.action.ActionMapping;
72 import org.apache.struts.action.ActionServlet;
73 import org.apache.struts.config.ActionConfig;
74 import org.apache.struts.config.ForwardConfig;
75 import org.apache.struts.config.ModuleConfig;
76 import org.apache.struts.tiles.TilesRequestProcessor;
77 import org.apache.struts.util.RequestUtils;
78
79 import javax.servlet.ServletException JavaDoc;
80 import javax.servlet.http.HttpServletRequest JavaDoc;
81 import javax.servlet.http.HttpServletResponse JavaDoc;
82 import java.io.IOException JavaDoc;
83
84
85 /**
86  * Title: Expresso Project Description: Copyright: Copyright (c)
87  * 2001, 2002, 2003 Company: Jcorporate
88  *
89  * @author Peter Pilgrim, Wed Feb 12 20:13:46 GMT 2003
90  * @version $Id: ExpressoRequestProcessor.java,v 1.10 2004/11/17 20:48:06 lhamel Exp $
91  */

92 public class ExpressoRequestProcessor extends TilesRequestProcessor {
93     /**
94      * Commons Logging instance.
95      */

96     protected static Log log = LogFactory.getLog(ExpressoRequestProcessor.class);
97
98     /**
99      * Default constructor
100      */

101     public ExpressoRequestProcessor() {
102         super();
103     }
104
105     /**
106      * Creates and return the <code>Action</code> controller. Request processor
107      * normally cache there action instances internally, therefore only an
108      * <code>Action</code> is normally created just once.
109      *
110      * @param className the class name of the action to create
111      * @return the action controller or <code>null</code> if the action cannot
112      * be created at all.
113      */

114     public Action createAction(String JavaDoc className) {
115         Action instance = null;
116
117         synchronized (actions) {
118             // Return any existing Action instance of this class
119
instance = (Action) actions.get(className);
120
121             if (instance != null) {
122                 if (log.isTraceEnabled()) {
123                     log.trace(" Returning existing Action instance");
124                 }
125
126                 return (instance);
127             }
128
129             // Create and return a new Action instance
130
if (log.isTraceEnabled()) {
131                 log.trace(" Creating new Action instance");
132             }
133
134             try {
135                 instance = (Action) RequestUtils.applicationInstance(className);
136                 instance.setServlet(this.servlet);
137                 actions.put(className, instance);
138             } catch (Throwable JavaDoc t) {
139                 log.error("Unable to create Action instance", t);
140
141                 return (null);
142             }
143         }
144
145         return (instance);
146     }
147
148     /**
149      * Initialize this request processor instance.
150      *
151      * @param servlet The ActionServlet we are associated with
152      * @param moduleConfig The ModuleConfig we are associated with.
153      * @throws ServletException If an error occur during initialization
154      */

155     public void init(ActionServlet servlet, ModuleConfig moduleConfig)
156             throws ServletException JavaDoc {
157         super.init(servlet, moduleConfig);
158     }
159
160     /**
161      * This method is call for each request. By default it does nothing with
162      * the requests, but Expresso Framework may use this call in the future to
163      * handle security and authentication.
164      *
165      * @param request The servlet request object
166      * @param response The servlet response object
167      * @return true if successful
168      */

169     public boolean processPrepocess(HttpServletRequest JavaDoc request,
170                                     HttpServletResponse JavaDoc response) {
171         return true;
172     }
173
174     /**
175      * This method was created specifically to allow dynamic URL forwarding since
176      * we cannot muck with the Struts 1.1 configuration due to the 'freeze()'
177      * method. It checks for
178      *
179      * @param request The servlet request
180      * @param response The servlet response
181      * @param forward the default forward config that Struts was handed by
182      * the controller
183      */

184     protected void processForwardConfig(HttpServletRequest JavaDoc request,
185                                         HttpServletResponse JavaDoc response,
186                                         ForwardConfig forward)
187             throws java.io.IOException JavaDoc, javax.servlet.ServletException JavaDoc {
188
189         //Grab any specified page styles from the session
190
String JavaDoc pageStyle = request.getParameter("pageStyle");
191         if (pageStyle != null && pageStyle.length() > 0) {
192             ForwardConfig forwardConfig = DynamicForwarder.redeemConfig(pageStyle, request);
193             if (forwardConfig != null) {
194                 if (log.isDebugEnabled()) {
195                     log.debug("Redeemed page style: " + forwardConfig.getPath());
196                 }
197                 forward = forwardConfig;
198             } else {
199                 if (log.isDebugEnabled()) {
200                     log.debug("Redeemed forward config, but forward config was null");
201                 }
202             }
203         }
204
205
206         super.processForwardConfig(request, response, forward);
207     }
208
209     /**
210      * subclass in order to allow ExpressoActionServlet to be a dispatcher
211      * for any finite-state-machine request which includes parameters for controller &
212      * state. if ExpressoActionServlet does not play this role, URLs must have
213      * a known controller embedded, which is not good for a JSP, for example,
214      * that isn't supposed to know about controllers. instead,
215      * ExpressoServletAction can be the "universal dispatcher"
216      * <p/>
217      * <p>Identify and return the path component (from the request URI) that
218      * we will use to select an <code>ActionMapping</code> with which to dispatch.
219      * If no such path can be identified, create an error response and return
220      * <code>null</code>.</p>
221      *
222      * @param httpServletRequest The servlet request we are processing
223      * @return String
224      */

225     protected String JavaDoc processPath(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse) throws IOException JavaDoc {
226         String JavaDoc path = super.processPath(httpServletRequest, httpServletResponse);
227
228         String JavaDoc controller = httpServletRequest.getParameter(Controller.CONTROLLER_PARAM_KEY);
229         if (controller != null) {
230             // do reverse-lookup from controller class to mapping
231
ActionConfig mapping = ConfigManager.getActionConfig(controller,
232                     httpServletRequest.getParameter(Controller.STATE_PARAM_KEY));
233             if (mapping != null) {
234                 path = mapping.getPath();
235             }
236         }
237
238         return path;
239     }
240
241     /**
242      * @todo add this method.. We don't want RequestProcessor to handle redirection if validation fails *RD* Mon Jul 27 2004
243      */

244     protected boolean processValidate(HttpServletRequest JavaDoc request,
245                                       HttpServletResponse JavaDoc response,
246                                       ActionForm form,
247                                       ActionMapping mapping) throws IOException JavaDoc, ServletException JavaDoc {
248         return true;
249     }
250
251 }
252
Popular Tags