001: /*
002: * $Header$
003: * $Revision: 70 $
004: * $Date: 2004-09-27 13:26:11 -0500 (Mon, 27 Sep 2004) $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Struts", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: */
061:
062: package org.apache.struts.upload;
063:
064: import javax.servlet.RequestDispatcher;
065: import javax.servlet.ServletInputStream;
066: import javax.servlet.http.Cookie;
067: import javax.servlet.http.HttpServletRequest;
068: import javax.servlet.http.HttpServletRequestWrapper;
069: import javax.servlet.http.HttpSession;
070: import java.io.BufferedReader;
071: import java.io.IOException;
072: import java.security.Principal;
073: import java.util.Collection;
074: import java.util.Collections;
075: import java.util.Enumeration;
076: import java.util.HashMap;
077: import java.util.Iterator;
078: import java.util.Locale;
079: import java.util.Map;
080: import java.util.Vector;
081:
082: /**
083: * This class functions as a wrapper around HttpServletRequest to
084: * provide working getParameter methods for multipart requests. Once
085: * Struts requires Servlet 2.3, this class will definately be changed to
086: * extend javax.servlet.http.HttpServletRequestWrapper instead of
087: * implementing HttpServletRequest. Servlet 2.3 methods are implemented
088: * to return <code>null</code> or do nothing if called on. Use
089: * {@link #getRequest() getRequest} to retrieve the underlying HttpServletRequest
090: * object and call on the 2.3 method there, the empty methods are here only
091: * so that this will compile with the Servlet 2.3 jar. This class exists temporarily
092: * in the process() method of ActionServlet, just before the ActionForward is processed
093: * and just after the Action is performed, the request is set back to the original
094: * HttpServletRequest object.
095: */
096: public class MultipartRequestWrapper implements HttpServletRequest {
097:
098: /**
099: * The parameters for this multipart request
100: */
101: protected Map parameters;
102:
103: /**
104: * The underlying HttpServletRequest
105: */
106: protected HttpServletRequest request;
107:
108: public MultipartRequestWrapper(HttpServletRequest request) {
109: this .request = request;
110: this .parameters = new HashMap();
111: }
112:
113: /**
114: * Sets a parameter for this request. The parameter is actually
115: * separate from the request parameters, but calling on the
116: * getParameter() methods of this class will work as if they weren't.
117: */
118: public void setParameter(String name, String value) {
119: String[] mValue = (String[]) parameters.get(name);
120: if (mValue == null) {
121: mValue = new String[0];
122: }
123: String[] newValue = new String[mValue.length + 1];
124: System.arraycopy(mValue, 0, newValue, 0, mValue.length);
125: newValue[mValue.length] = value;
126:
127: parameters.put(name, newValue);
128: }
129:
130: /**
131: * Attempts to get a parameter for this request. It first looks in the
132: * underlying HttpServletRequest object for the parameter, and if that
133: * doesn't exist it looks for the parameters retrieved from the multipart
134: * request
135: */
136: public String getParameter(String name) {
137: String value = request.getParameter(name);
138: if (value == null) {
139: String[] mValue = (String[]) parameters.get(name);
140: if ((mValue != null) && (mValue.length > 0)) {
141: value = mValue[0];
142: }
143: }
144: return value;
145: }
146:
147: /**
148: * Returns the names of the parameters for this request.
149: * The enumeration consists of the normal request parameter
150: * names plus the parameters read from the multipart request
151: */
152: public Enumeration getParameterNames() {
153: Enumeration baseParams = request.getParameterNames();
154: Vector list = new Vector();
155: while (baseParams.hasMoreElements()) {
156: list.add(baseParams.nextElement());
157: }
158: Collection multipartParams = parameters.keySet();
159: Iterator iterator = multipartParams.iterator();
160: while (iterator.hasNext()) {
161: list.add(iterator.next());
162: }
163: return Collections.enumeration(list);
164: }
165:
166: public String[] getParameterValues(String name) {
167: String[] value = request.getParameterValues(name);
168: if (value == null) {
169: value = (String[]) parameters.get(name);
170: }
171: return value;
172: }
173:
174: /**
175: * Returns the underlying HttpServletRequest for this wrapper
176: */
177: public HttpServletRequest getRequest() {
178: return new HttpServletRequestWrapper(this );
179: }
180:
181: //WRAPPER IMPLEMENTATIONS OF SERVLET REQUEST METHODS
182: public Object getAttribute(String name) {
183: return request.getAttribute(name);
184: }
185:
186: public Enumeration getAttributeNames() {
187: return request.getAttributeNames();
188: }
189:
190: public String getCharacterEncoding() {
191: return request.getCharacterEncoding();
192: }
193:
194: public int getContentLength() {
195: return request.getContentLength();
196: }
197:
198: public String getContentType() {
199: return request.getContentType();
200: }
201:
202: public ServletInputStream getInputStream() throws IOException {
203: return request.getInputStream();
204: }
205:
206: public String getProtocol() {
207: return request.getProtocol();
208: }
209:
210: public String getScheme() {
211: return request.getScheme();
212: }
213:
214: public String getServerName() {
215: return request.getServerName();
216: }
217:
218: public int getServerPort() {
219: return request.getServerPort();
220: }
221:
222: public BufferedReader getReader() throws IOException {
223: return request.getReader();
224: }
225:
226: public String getRemoteAddr() {
227: return request.getRemoteAddr();
228: }
229:
230: public String getRemoteHost() {
231: return request.getRemoteHost();
232: }
233:
234: public void setAttribute(String name, Object o) {
235: request.setAttribute(name, o);
236: }
237:
238: public void removeAttribute(String name) {
239: request.removeAttribute(name);
240: }
241:
242: public Locale getLocale() {
243: return request.getLocale();
244: }
245:
246: public Enumeration getLocales() {
247: return request.getLocales();
248: }
249:
250: public boolean isSecure() {
251: return request.isSecure();
252: }
253:
254: public RequestDispatcher getRequestDispatcher(String path) {
255: return request.getRequestDispatcher(path);
256: }
257:
258: public String getRealPath(String path) {
259: return request.getRealPath(path);
260: }
261:
262: public int getRemotePort() {
263: return 0;
264: }
265:
266: public String getLocalName() {
267: return null;
268: }
269:
270: public String getLocalAddr() {
271: return null;
272: }
273:
274: public int getLocalPort() {
275: return 0;
276: }
277:
278: //WRAPPER IMPLEMENTATIONS OF HTTPSERVLETREQUEST METHODS
279: public String getAuthType() {
280: return request.getAuthType();
281: }
282:
283: public Cookie[] getCookies() {
284: return request.getCookies();
285: }
286:
287: public long getDateHeader(String name) {
288: return request.getDateHeader(name);
289: }
290:
291: public String getHeader(String name) {
292: return request.getHeader(name);
293: }
294:
295: public Enumeration getHeaders(String name) {
296: return request.getHeaders(name);
297: }
298:
299: public Enumeration getHeaderNames() {
300: return request.getHeaderNames();
301: }
302:
303: public int getIntHeader(String name) {
304: return request.getIntHeader(name);
305: }
306:
307: public String getMethod() {
308: return request.getMethod();
309: }
310:
311: public String getPathInfo() {
312: return request.getPathInfo();
313: }
314:
315: public String getPathTranslated() {
316: return request.getPathTranslated();
317: }
318:
319: public String getContextPath() {
320: return request.getContextPath();
321: }
322:
323: public String getQueryString() {
324: return request.getQueryString();
325: }
326:
327: public String getRemoteUser() {
328: return request.getRemoteUser();
329: }
330:
331: public boolean isUserInRole(String user) {
332: return request.isUserInRole(user);
333: }
334:
335: public Principal getUserPrincipal() {
336: return request.getUserPrincipal();
337: }
338:
339: public String getRequestedSessionId() {
340: return request.getRequestedSessionId();
341: }
342:
343: public String getRequestURI() {
344: return request.getRequestURI();
345: }
346:
347: public String getServletPath() {
348: return request.getServletPath();
349: }
350:
351: public HttpSession getSession(boolean create) {
352: return request.getSession(create);
353: }
354:
355: public HttpSession getSession() {
356: return request.getSession();
357: }
358:
359: public boolean isRequestedSessionIdValid() {
360: return request.isRequestedSessionIdValid();
361: }
362:
363: public boolean isRequestedSessionIdFromURL() {
364: return request.isRequestedSessionIdFromURL();
365: }
366:
367: public boolean isRequestedSessionIdFromUrl() {
368: return request.isRequestedSessionIdFromUrl();
369: }
370:
371: //SERVLET 2.3 EMPTY METHODS
372: /**
373: * This method returns null. To use any Servlet 2.3 methods,
374: * call on getRequest() and use that request object. Once Servlet 2.3
375: * is required to build Struts, this will no longer be an issue.
376: */
377: public Map getParameterMap() {
378: return parameters;
379: }
380:
381: /**
382: * This method does nothing. To use any Servlet 2.3 methods,
383: * call on getRequest() and use that request object. Once Servlet 2.3
384: * is required to build Struts, this will no longer be an issue.
385: */
386: public void setCharacterEncoding(String encoding) {
387: ;
388: }
389:
390: /**
391: * This method returns null. To use any Servlet 2.3 methods,
392: * call on getRequest() and use that request object. Once Servlet 2.3
393: * is required to build Struts, this will no longer be an issue.
394: */
395: public StringBuffer getRequestURL() {
396: return null;
397: }
398:
399: /**
400: * This method returns false. To use any Servlet 2.3 methods,
401: * call on getRequest() and use that request object. Once Servlet 2.3
402: * is required to build Struts, this will no longer be an issue.
403: */
404: public boolean isRequestedSessionIdFromCookie() {
405: return false;
406: }
407:
408: }
|