KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > legacy > ComposableRequestProcessor


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain.legacy;
18
19
20 import java.io.IOException JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import org.apache.commons.chain.Catalog;
25 import org.apache.commons.chain.CatalogFactory;
26 import org.apache.commons.chain.Command;
27 import org.apache.commons.chain.web.servlet.ServletWebContext;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.action.ActionServlet;
31 import org.apache.struts.action.RequestProcessor;
32 import org.apache.struts.chain.Constants;
33 import org.apache.struts.config.ModuleConfig;
34 import org.apache.struts.upload.MultipartRequestWrapper;
35
36
37 /**
38  * <p><strong>ComposableRequestProcessor</strong> uses the <em>Chain Of
39  * Resposibility</em> design pattern (as implemented by the commons-chain
40  * package in Jakarta Commons) to support external configuration of command
41  * chains to be used. It is configured via the following context initialization
42  * parameters:</p>
43  * <ul>
44  * <li><strong>org.apache.struts.chain.CATALOG_NAME</strong> - Name of the
45  * <code>Catalog</code> in which we will look up the <code>Command</code>
46  * to be executed for each request. If not specified, the default value
47  * is <code>struts</code>.</li>
48  * <li><strong>org.apache.struts.chain.COMMAND_NAME</strong> - Name of the
49  * <code>Command</code> which we will execute for each request, to be
50  * looked up in the specified <code>Catalog</code>. If not specified,
51  * the default value is <code>servlet-standard</code>.</li>
52  * </ul>
53  *
54  * @version $Rev: 55324 $ $Date: 2004-10-22 19:55:27 +0100 (Fri, 22 Oct 2004) $
55  * @since Struts 1.1
56  */

57
58 public class ComposableRequestProcessor extends RequestProcessor {
59
60
61     // ------------------------------------------------------ Manifest Constants
62

63
64     /**
65      * <p>Name of the context initialization parameter containing the
66      * name of the <code>Catalog</code> we will use.</p>
67      */

68     private static final String JavaDoc CATALOG_NAME =
69         "org.apache.struts.chain.CATALOG_NAME";
70
71
72     /**
73      * <p>Name of the <code>Command</code> to be executed for each request.</p>
74      */

75     private static final String JavaDoc COMMAND_NAME =
76         "org.apache.struts.chain.COMMAND_NAME";
77
78
79     // ------------------------------------------------------ Instance Variables
80

81
82     /**
83      * <p>The {@link Catalog} containing all of the available command chains
84      * for this module.
85      */

86     protected Catalog catalog = null;
87
88
89     /**
90      * <p>The {@link Command} to be executed for each request.</p>
91      */

92     protected Command command = null;
93
94
95     /**
96      * <p>The <code>Log</code> instance for this class.</p>
97      */

98     protected static final Log log =
99         LogFactory.getLog(ComposableRequestProcessor.class);
100
101
102     // ---------------------------------------------------------- Public Methods
103

104
105     /**
106      * Clean up in preparation for a shutdown of this application.
107      */

108     public void destroy() {
109
110         super.destroy();
111         catalog = null;
112         command = null;
113
114     }
115
116
117     /**
118      * <p>Initialize this request processor instance.</p>
119      *
120      * @param servlet The ActionServlet we are associated with
121      * @param moduleConfig The ModuleConfig we are associated with.
122      *
123      * @throws ServletException If an error occurs during initialization
124      */

125     public void init(ActionServlet servlet,
126                      ModuleConfig moduleConfig)
127            throws ServletException JavaDoc {
128
129         log.info("Initializing composable request processor for module prefix '"
130                  + moduleConfig.getPrefix() + "'");
131         super.init(servlet, moduleConfig);
132
133         String JavaDoc catalogName =
134             servlet.getServletContext().getInitParameter(CATALOG_NAME);
135         if (catalogName == null) {
136             catalogName = "struts";
137         }
138         catalog = CatalogFactory.getInstance().getCatalog(catalogName);
139         if (catalog == null) {
140             throw new ServletException JavaDoc("Cannot find catalog '" +
141                                        catalogName + "'");
142         }
143
144         String JavaDoc commandName =
145             servlet.getServletContext().getInitParameter(COMMAND_NAME);
146         if (commandName == null) {
147             commandName = "servlet-standard";
148         }
149         command = catalog.getCommand(commandName);
150         if (command == null) {
151             throw new ServletException JavaDoc("Cannot find command '" +
152                                        commandName + "'");
153         }
154
155     }
156
157
158     /**
159      * <p>Process an <code>HttpServletRequest</code> and create the
160      * corresponding <code>HttpServletResponse</code>.</p>
161      *
162      * @param request The servlet request we are processing
163      * @param response The servlet response we are creating
164      *
165      * @exception IOException if an input/output error occurs
166      * @exception ServletException if a processing exception occurs
167      */

168     public void process(HttpServletRequest JavaDoc request,
169                         HttpServletResponse JavaDoc response)
170         throws IOException JavaDoc, ServletException JavaDoc {
171
172         // Wrap the request in the case of a multipart request
173
request = processMultipart(request);
174         
175         // Create and populate a Context for this request
176
ServletWebContext context = new ServletWebContext();
177         context.initialize(getServletContext(), request, response);
178         context.put(Constants.ACTION_SERVLET_KEY,
179                     this.servlet);
180         context.put(Constants.MODULE_CONFIG_KEY,
181                     this.moduleConfig);
182
183         // Create and execute the command.
184
try {
185             if (log.isDebugEnabled()) {
186                 log.debug("Using processing chain for this request");
187             }
188             command.execute(context);
189         } catch (Exception JavaDoc e) {
190             // Execute the exception processing chain??
191
throw new ServletException JavaDoc(e);
192         }
193
194         // Release the context.
195
context.release();
196     }
197     
198
199     /**
200      * If this is a multipart request, wrap it with a special wrapper.
201      * Otherwise, return the request unchanged.
202      *
203      * @param request The HttpServletRequest we are processing
204      */

205     protected HttpServletRequest JavaDoc processMultipart(HttpServletRequest JavaDoc request) {
206
207         if (!"POST".equalsIgnoreCase(request.getMethod())) {
208             return (request);
209         }
210         
211         String JavaDoc contentType = request.getContentType();
212         if ((contentType != null) &&
213             contentType.startsWith("multipart/form-data")) {
214             return (new MultipartRequestWrapper(request));
215         } else {
216             return (request);
217         }
218
219     }
220
221
222 }
223
224
225
226
Popular Tags