e.g. Calendar Search Help
You must enter a value before pressing Search
xplanner

Class: org.apache.struts.upload.MultipartRequestWrapper   ©

 OK to copy?
001 /*
002  * $Header: /cvsroot/xplanner/xplanner/src/org/apache/struts/upload/MultipartRequestWrapper.java,v 1.3 2004/05/23 18:41:18 sbate Exp $
003  * $Revision: 1.3 $
004  * $Date: 2004/05/23 18:41:18 $
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     public Enumeration getAttributeNames() {
186         return request.getAttributeNames();
187     }
188     public String getCharacterEncoding() {
189         return request.getCharacterEncoding();
190     }
191     public int getContentLength() {
192         return request.getContentLength();
193     }
194     public String getContentType() {
195         return request.getContentType();
196     }
197     public ServletInputStream getInputStream() throws IOException {
198         return request.getInputStream();
199     }
200     public String getProtocol() {
201         return request.getProtocol();
202     }
203     public String getScheme() {
204         return request.getScheme();
205     }
206     public String getServerName() {
207         return request.getServerName();
208     }
209     public int getServerPort() {
210         return request.getServerPort();
211     }
212     public BufferedReader getReader() throws IOException {
213         return request.getReader();
214     }
215     public String getRemoteAddr() {
216         return request.getRemoteAddr();
217     }
218     public String getRemoteHost() {
219         return request.getRemoteHost();
220     }
221     public void setAttribute(String name, Object o) {
222         request.setAttribute(name, o);
223     }
224     public void removeAttribute(String name) {
225         request.removeAttribute(name);
226     }
227     public Locale getLocale() {
228         return request.getLocale();
229     }
230     public Enumeration getLocales() {
231         return request.getLocales();
232     }
233     public boolean isSecure() {
234         return request.isSecure();
235     }
236     public RequestDispatcher getRequestDispatcher(String path) {
237         return request.getRequestDispatcher(path);
238     }
239     public String getRealPath(String path) {
Rate240         return request.getRealPath(path);
241     }
242 
243     public int getRemotePort() {
244         return 0;
245     }
246 
247     public String getLocalName() {
248         return null;
249     }
250 
251     public String getLocalAddr() {
252         return null;
253     }
254 
255     public int getLocalPort() {
256         return 0;
257     }
258 
259     //WRAPPER IMPLEMENTATIONS OF HTTPSERVLETREQUEST METHODS
260     public String getAuthType() {
261         return request.getAuthType();
262     }
263     public Cookie[] getCookies() {
264         return request.getCookies();
265     }
266     public long getDateHeader(String name) {
267         return request.getDateHeader(name);
268     }
269     public String getHeader(String name) {
270         return request.getHeader(name);
271     }
272     public Enumeration getHeaders(String name) {
273         return request.getHeaders(name);
274     }
275     public Enumeration getHeaderNames() {
276         return request.getHeaderNames();
277     }
278     public int getIntHeader(String name) {
279         return request.getIntHeader(name);
280     }
281     public String getMethod() {
282         return request.getMethod();
283     }
284     public String getPathInfo() {
285         return request.getPathInfo();
286     }
287     public String getPathTranslated() {
288         return request.getPathTranslated();
289     }
290     public String getContextPath() {
291         return request.getContextPath();
292     }
293     public String getQueryString() {
294         return request.getQueryString();
295     }
296     public String getRemoteUser() {
297         return request.getRemoteUser();
298     }
299     public boolean isUserInRole(String user) {
300         return request.isUserInRole(user);
301     }
302     public Principal getUserPrincipal() {
303         return request.getUserPrincipal();
304     }
305     public String getRequestedSessionId() {
306         return request.getRequestedSessionId();
307     }
308     public String getRequestURI() {
309         return request.getRequestURI();
310     }
311     public String getServletPath() {
312         return request.getServletPath();
313     }
314     public HttpSession getSession(boolean create) {
315         return request.getSession(create);
316     }
317     public HttpSession getSession() {
318         return request.getSession();
319     }
320     public boolean isRequestedSessionIdValid() {
321         return request.isRequestedSessionIdValid();
322     }
323     public boolean isRequestedSessionIdFromURL() {
324         return request.isRequestedSessionIdFromURL();
325     }
326     public boolean isRequestedSessionIdFromUrl() {
327         return request.isRequestedSessionIdFromUrl();
328     }
329 
330     //SERVLET 2.3 EMPTY METHODS
331     /**
332      * This method returns null.  To use any Servlet 2.3 methods,
333      * call on getRequest() and use that request object.  Once Servlet 2.3
334      * is required to build Struts, this will no longer be an issue.
335      */
336     public Map getParameterMap() {
337         return parameters;
338     }
339     /**
340      * This method does nothing.  To use any Servlet 2.3 methods,
341      * call on getRequest() and use that request object.  Once Servlet 2.3
342      * is required to build Struts, this will no longer be an issue.
343      */
344     public void setCharacterEncoding(String encoding) {
345         ;
346     }
347     /**
348      * This method returns null.  To use any Servlet 2.3 methods,
349      * call on getRequest() and use that request object.  Once Servlet 2.3
350      * is required to build Struts, this will no longer be an issue.
351      */
352     public StringBuffer getRequestURL() {
353         return null;
354     }
355     /**
356      * This method returns false.  To use any Servlet 2.3 methods,
357      * call on getRequest() and use that request object.  Once Servlet 2.3
358      * is required to build Struts, this will no longer be an issue.
359      */
360     public boolean isRequestedSessionIdFromCookie() {
361         return false;
362     }
363 
364 
365 
366 
367 
368 
369 
370 
371 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
240 0% of 0