KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > upload > MultipartRequestWrapper


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

18
19 package org.apache.struts.upload;
20
21 import java.util.Map JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.BufferedReader JavaDoc;
31 import java.security.Principal JavaDoc;
32 import javax.servlet.ServletInputStream JavaDoc;
33 import javax.servlet.RequestDispatcher JavaDoc;
34 import javax.servlet.http.Cookie JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.commons.beanutils.MethodUtils;
41
42 /**
43  * This class functions as a wrapper around HttpServletRequest to
44  * provide working getParameter methods for multipart requests. Once
45  * Struts requires Servlet 2.3, this class will definately be changed to
46  * extend javax.servlet.http.HttpServletRequestWrapper instead of
47  * implementing HttpServletRequest. Servlet 2.3 methods are implemented
48  * to return <code>null</code> or do nothing if called on. Use
49  * {@link #getRequest() getRequest} to retrieve the underlying HttpServletRequest
50  * object and call on the 2.3 method there, the empty methods are here only
51  * so that this will compile with the Servlet 2.3 jar. This class exists temporarily
52  * in the process() method of ActionServlet, just before the ActionForward is processed
53  * and just after the Action is performed, the request is set back to the original
54  * HttpServletRequest object.
55  */

56 public class MultipartRequestWrapper implements HttpServletRequest JavaDoc {
57
58     /** Logging instance */
59     private static final Log log = LogFactory.getLog(MultipartRequestWrapper.class);
60     
61     /**
62      * The parameters for this multipart request
63      */

64     protected Map JavaDoc parameters;
65     
66     /**
67      * The underlying HttpServletRequest
68      */

69     protected HttpServletRequest JavaDoc request;
70     
71     public MultipartRequestWrapper(HttpServletRequest JavaDoc request) {
72         this.request = request;
73         this.parameters = new HashMap JavaDoc();
74     }
75     
76     /**
77      * Sets a parameter for this request. The parameter is actually
78      * separate from the request parameters, but calling on the
79      * getParameter() methods of this class will work as if they weren't.
80      */

81     public void setParameter(String JavaDoc name, String JavaDoc value) {
82         String JavaDoc[] mValue = (String JavaDoc[]) parameters.get(name);
83         if (mValue == null) {
84             mValue = new String JavaDoc[0];
85         }
86         String JavaDoc[] newValue = new String JavaDoc[mValue.length + 1];
87         System.arraycopy(mValue, 0, newValue, 0, mValue.length);
88         newValue[mValue.length] = value;
89         
90         parameters.put(name, newValue);
91     }
92     
93     /**
94      * Attempts to get a parameter for this request. It first looks in the
95      * underlying HttpServletRequest object for the parameter, and if that
96      * doesn't exist it looks for the parameters retrieved from the multipart
97      * request
98      */

99     public String JavaDoc getParameter(String JavaDoc name) {
100         String JavaDoc value = request.getParameter(name);
101         if (value == null) {
102             String JavaDoc[] mValue = (String JavaDoc[]) parameters.get(name);
103             if ((mValue != null) && (mValue.length > 0)) {
104                 value = mValue[0];
105             }
106         }
107         return value;
108     }
109     
110     /**
111      * Returns the names of the parameters for this request.
112      * The enumeration consists of the normal request parameter
113      * names plus the parameters read from the multipart request
114      */

115     public Enumeration JavaDoc getParameterNames() {
116         Enumeration JavaDoc baseParams = request.getParameterNames();
117         Vector JavaDoc list = new Vector JavaDoc();
118         while (baseParams.hasMoreElements()) {
119             list.add(baseParams.nextElement());
120         }
121         Collection JavaDoc multipartParams = parameters.keySet();
122         Iterator JavaDoc iterator = multipartParams.iterator();
123         while (iterator.hasNext()) {
124             list.add(iterator.next());
125         }
126         return Collections.enumeration(list);
127     }
128     
129     public String JavaDoc[] getParameterValues(String JavaDoc name) {
130         String JavaDoc[] value = request.getParameterValues(name);
131         if (value == null) {
132             value = (String JavaDoc[]) parameters.get(name);
133         }
134         return value;
135     }
136     
137     /**
138      * Returns the underlying HttpServletRequest for this wrapper
139      */

140     public HttpServletRequest JavaDoc getRequest() {
141         return request;
142     }
143     
144     //WRAPPER IMPLEMENTATIONS OF SERVLET REQUEST METHODS
145
public Object JavaDoc getAttribute(String JavaDoc name) {
146         return request.getAttribute(name);
147     }
148     public Enumeration JavaDoc getAttributeNames() {
149         return request.getAttributeNames();
150     }
151     public String JavaDoc getCharacterEncoding() {
152         return request.getCharacterEncoding();
153     }
154     public int getContentLength() {
155         return request.getContentLength();
156     }
157     public String JavaDoc getContentType() {
158         return request.getContentType();
159     }
160     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
161         return request.getInputStream();
162     }
163     public String JavaDoc getProtocol() {
164         return request.getProtocol();
165     }
166     public String JavaDoc getScheme() {
167         return request.getScheme();
168     }
169     public String JavaDoc getServerName() {
170         return request.getServerName();
171     }
172     public int getServerPort() {
173         return request.getServerPort();
174     }
175     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
176         return request.getReader();
177     }
178     public String JavaDoc getRemoteAddr() {
179         return request.getRemoteAddr();
180     }
181     public String JavaDoc getRemoteHost() {
182         return request.getRemoteHost();
183     }
184     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
185         request.setAttribute(name, o);
186     }
187     public void removeAttribute(String JavaDoc name) {
188         request.removeAttribute(name);
189     }
190     public Locale JavaDoc getLocale() {
191         return request.getLocale();
192     }
193     public Enumeration JavaDoc getLocales() {
194         return request.getLocales();
195     }
196     public boolean isSecure() {
197         return request.isSecure();
198     }
199     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
200         return request.getRequestDispatcher(path);
201     }
202     public String JavaDoc getRealPath(String JavaDoc path) {
203         return request.getRealPath(path);
204     }
205     
206     //WRAPPER IMPLEMENTATIONS OF HTTPSERVLETREQUEST METHODS
207
public String JavaDoc getAuthType() {
208         return request.getAuthType();
209     }
210     public Cookie JavaDoc[] getCookies() {
211         return request.getCookies();
212     }
213     public long getDateHeader(String JavaDoc name) {
214         return request.getDateHeader(name);
215     }
216     public String JavaDoc getHeader(String JavaDoc name) {
217         return request.getHeader(name);
218     }
219     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
220         return request.getHeaders(name);
221     }
222     public Enumeration JavaDoc getHeaderNames() {
223         return request.getHeaderNames();
224     }
225     public int getIntHeader(String JavaDoc name) {
226         return request.getIntHeader(name);
227     }
228     public String JavaDoc getMethod() {
229         return request.getMethod();
230     }
231     public String JavaDoc getPathInfo() {
232         return request.getPathInfo();
233     }
234     public String JavaDoc getPathTranslated() {
235         return request.getPathTranslated();
236     }
237     public String JavaDoc getContextPath() {
238         return request.getContextPath();
239     }
240     public String JavaDoc getQueryString() {
241         return request.getQueryString();
242     }
243     public String JavaDoc getRemoteUser() {
244         return request.getRemoteUser();
245     }
246     public boolean isUserInRole(String JavaDoc user) {
247         return request.isUserInRole(user);
248     }
249     public Principal JavaDoc getUserPrincipal() {
250         return request.getUserPrincipal();
251     }
252     public String JavaDoc getRequestedSessionId() {
253         return request.getRequestedSessionId();
254     }
255     public String JavaDoc getRequestURI() {
256         return request.getRequestURI();
257     }
258     public String JavaDoc getServletPath() {
259         return request.getServletPath();
260     }
261     public HttpSession JavaDoc getSession(boolean create) {
262         return request.getSession(create);
263     }
264     public HttpSession JavaDoc getSession() {
265         return request.getSession();
266     }
267     public boolean isRequestedSessionIdValid() {
268         return request.isRequestedSessionIdValid();
269     }
270     public boolean isRequestedSessionIdFromURL() {
271         return request.isRequestedSessionIdFromURL();
272     }
273     public boolean isRequestedSessionIdFromUrl() {
274         return request.isRequestedSessionIdFromUrl();
275     }
276     
277     //SERVLET 2.3 METHODS
278

279     /**
280      * Implements the Servlet 2.3 <i>getParameterMap</i> method.
281      */

282     public Map JavaDoc getParameterMap() {
283         Map JavaDoc map = new HashMap JavaDoc(parameters);
284         Enumeration JavaDoc names = request.getParameterNames();
285         while (names.hasMoreElements()) {
286             String JavaDoc name = (String JavaDoc)names.nextElement();
287             map.put(name, request.getParameterValues(name));
288         }
289         return map;
290     }
291
292     /**
293      * Use Reflection to invoke Servlet 2.3 <i>setCharacterEncoding</i>
294      * method on the wrapped Request.
295      */

296     public void setCharacterEncoding(String JavaDoc encoding) {
297         invokeRequestMethod("setCharacterEncoding", new Object JavaDoc[] {encoding});
298     }
299
300     /**
301      * Use Reflection to invoke Servlet 2.3 <i>getRequestURL</i>
302      * method on the wrapped Request.
303      */

304     public StringBuffer JavaDoc getRequestURL() {
305         return (StringBuffer JavaDoc)invokeRequestMethod("getRequestURL", null);
306     }
307
308     /**
309      * Use Reflection to invoke Servlet 2.3 <i>isRequestedSessionIdFromCookie</i>
310      * method on the wrapped Request.
311      */

312     public boolean isRequestedSessionIdFromCookie() {
313         Object JavaDoc result = invokeRequestMethod("isRequestedSessionIdFromCookie", null);
314         return (result == null) ? false : ((Boolean JavaDoc)result).booleanValue();
315     }
316
317     //SERVLET 2.4 METHODS
318

319     /**
320      * Use Reflection to invoke Servlet 2.4 <i>getLocalAddr</i>
321      * method on the wrapped Request.
322      */

323     public String JavaDoc getLocalAddr() {
324         return (String JavaDoc)invokeRequestMethod("getLocalAddr", null);
325     }
326
327     /**
328      * Use Reflection to invoke Servlet 2.4 <i>getLocalName</i>
329      * method on the wrapped Request.
330      */

331     public String JavaDoc getLocalName() {
332         return (String JavaDoc)invokeRequestMethod("getLocalName", null);
333     }
334
335     /**
336      * Use Reflection to invoke Servlet 2.4 <i>getLocalPort</i>
337      * method on the wrapped Request.
338      */

339     public int getLocalPort() {
340         Object JavaDoc result = invokeRequestMethod("getLocalPort", null);
341         return (result == null) ? 0 : ((Integer JavaDoc)result).intValue();
342     }
343
344     /**
345      * Use Reflection to invoke Servlet 2.4 <i>getRemotePort</i>
346      * method on the wrapped Request.
347      */

348     public int getRemotePort() {
349         Object JavaDoc result = invokeRequestMethod("getRemotePort", null);
350         return (result == null) ? 0 : ((Integer JavaDoc)result).intValue();
351     }
352
353     /**
354      * Convenience method which uses reflection to invoke a method
355      * on the Request.
356      */

357     private Object JavaDoc invokeRequestMethod(String JavaDoc name, Object JavaDoc[] args) {
358         try {
359             return MethodUtils.invokeExactMethod(request, name, args);
360         } catch (NoSuchMethodException JavaDoc e) {
361             if (log.isDebugEnabled()) {
362                 log.debug("Method '" +name + "' not defined for javax.servlet.http.HttpServletRequest");
363             }
364         } catch (InvocationTargetException JavaDoc e) {
365             log.error("Error invoking method '" +name + "' ", e.getTargetException());
366         } catch (IllegalAccessException JavaDoc e) {
367             log.error("Error invoking method '" +name + "' ", e);
368         }
369         return null;
370     }
371     
372 }
373
Popular Tags