View Javadoc

1   /*
2    * $Id: MultipartRequestWrapper.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2005 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  package org.apache.struts.upload;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletRequestWrapper;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.Vector;
30  
31  /***
32   * <p> This class functions as a wrapper around HttpServletRequest to provide
33   * working getParameter methods for multipart requests. </p>
34   */
35  public class MultipartRequestWrapper extends HttpServletRequestWrapper {
36      /***
37       * <p> The parameters for this multipart request </p>
38       */
39      protected Map parameters;
40  
41      public MultipartRequestWrapper(HttpServletRequest request) {
42          super(request);
43          this.parameters = new HashMap();
44      }
45  
46      /***
47       * <p> Sets a parameter for this request.  The parameter is actually
48       * separate from the request parameters, but calling on the getParameter()
49       * methods of this class will work as if they weren't. </p>
50       */
51      public void setParameter(String name, String value) {
52          String[] mValue = (String[]) parameters.get(name);
53  
54          if (mValue == null) {
55              mValue = new String[0];
56          }
57  
58          String[] newValue = new String[mValue.length + 1];
59  
60          System.arraycopy(mValue, 0, newValue, 0, mValue.length);
61          newValue[mValue.length] = value;
62  
63          parameters.put(name, newValue);
64      }
65  
66      /***
67       * <p> Attempts to get a parameter for this request.  It first looks in
68       * the underlying HttpServletRequest object for the parameter, and if that
69       * doesn't exist it looks for the parameters retrieved from the multipart
70       * request </p>
71       */
72      public String getParameter(String name) {
73          String value = getRequest().getParameter(name);
74  
75          if (value == null) {
76              String[] mValue = (String[]) parameters.get(name);
77  
78              if ((mValue != null) && (mValue.length > 0)) {
79                  value = mValue[0];
80              }
81          }
82  
83          return value;
84      }
85  
86      /***
87       * <p> Returns the names of the parameters for this request. The
88       * enumeration consists of the normal request parameter names plus the
89       * parameters read from the multipart request </p>
90       */
91      public Enumeration getParameterNames() {
92          Enumeration baseParams = getRequest().getParameterNames();
93          Vector list = new Vector();
94  
95          while (baseParams.hasMoreElements()) {
96              list.add(baseParams.nextElement());
97          }
98  
99          Collection multipartParams = parameters.keySet();
100         Iterator iterator = multipartParams.iterator();
101 
102         while (iterator.hasNext()) {
103             list.add(iterator.next());
104         }
105 
106         return Collections.enumeration(list);
107     }
108 
109     /***
110      * <p> Returns the values of a parameter in this request. It first looks
111      * in the underlying HttpServletRequest object for the parameter, and if
112      * that doesn't exist it looks for the parameter retrieved from the
113      * multipart request. </p>
114      */
115     public String[] getParameterValues(String name) {
116         String[] value = getRequest().getParameterValues(name);
117 
118         if (value == null) {
119             value = (String[]) parameters.get(name);
120         }
121 
122         return value;
123     }
124 
125     /***
126      * <p> Combines the parameters stored here with those in the underlying
127      * request. If paramater values in the underlying request take precedence
128      * over those stored here. </p>
129      */
130     public Map getParameterMap() {
131         Map map = new HashMap(parameters);
132 
133         map.putAll(getRequest().getParameterMap());
134 
135         return map;
136     }
137 }