View Javadoc

1   /*
2    * $Id: MultipartRequestWrapper.java 127 2004-11-06 10:15:26Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters.upload;
24  
25  import java.util.Map;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Enumeration;
29  import java.util.Collection;
30  import java.util.Collections;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletRequestWrapper;
33  
34  /**
35   * Provides a convenient implementation of the <code>HttpServletRequest</code>
36   * interface that allows us transport all parameters from a multipart request like
37   * parameters in a normal request.
38   *
39   * @author  Jose M. Palomar
40   * @version $Revision: 127 $
41   */
42  public final class MultipartRequestWrapper extends HttpServletRequestWrapper {
43  
44      // Fields
45      /**
46       * Parameters map.
47       */
48      private Map parameters;
49  
50      // Constructors
51      /**
52       * Creates a new <code>MultipartRequestWrapper</code>.
53       *
54       * @param request HttpServletRequest client's original request.
55       * @param parameters Map parameters map.
56       * @param files Map files map.
57       */
58      public MultipartRequestWrapper(HttpServletRequest request, Map parameters, Map files) {
59          super(request);
60  
61          this.parameters = new HashMap();
62          Iterator parametersIterator = parameters.keySet().iterator();
63          while (parametersIterator.hasNext()) {
64  
65              String param = (String) parametersIterator.next();
66              Collection values = (Collection) parameters.get(param);
67              this.parameters.put(param, (String[]) values.toArray(new String[0]));
68  
69          }
70  
71          Iterator filesIterator = files.keySet().iterator();
72          while (filesIterator.hasNext()) {
73  
74              String fileParam = (String) filesIterator.next();
75              MultipartFile fileObject = (MultipartFile) files.get(fileParam);
76  
77              if (fileObject != null) {
78                  setAttribute(fileParam, fileObject);
79                  setParameter(fileParam, fileObject.getRemoteName());
80              }
81              else {
82                  setParameter(fileParam, "");
83              }
84  
85          }
86  
87      }
88  
89      /**
90       * Returns the value of a request parameter as a <code>String</code>, or
91       * <code>null</code> if the parameter does not exist. Request parameters are
92       * extra information sent with the request.
93       *
94       * @param name String a <code>String</code> specifying the name of the parameter.
95       * @return String a <code>String</code> representing the single value of the
96       * parameter.
97       * @see javax.servlet.ServletRequest#getParameter(String)
98       */
99      public String getParameter(String name) {
100         String value = super.getParameter(name);
101         if (value == null) {
102             String[] multiValue =  (String[]) parameters.get(name);
103             if ((multiValue != null) && (multiValue.length > 0)) {
104                 value = multiValue[0];
105             }
106         }
107 
108         return value;
109     }
110 
111     /**
112      * Returns an array of <code>String</code> objects containing all of the values
113      * the given request parameter has, or <code>null</code> if the parameter does
114      * not exist.
115      *
116      * @param name String a <code>String</code> specifying the name of the parameter.
117      * @return String[] an array of <code>String</code> objects containing the
118      * parameter's values.
119      * @see javax.servlet.ServletRequest#getParameterValues(String)
120      */
121     public String[] getParameterValues(String name) {
122         String[] multiValue = super.getParameterValues(name);
123         if (multiValue == null) {
124             multiValue = (String[]) parameters.get(name);
125         }
126 
127         return multiValue;
128     }
129 
130     /**
131      * Returns an <code>Enumeration</code> of <code>String</code> objects containing
132      * the names of the parameters contained in this request.
133      *
134      * @return Enumeration an <code>Enumeration</code> of <code>String</code> objects,
135      * each <code>String</code> containing the name of a request parameter; or an
136      * empty <code>Enumeration</code> if the request has no parameters.
137      * @see javax.servlet.ServletRequest#getParameterNames()
138      */
139     public Enumeration getParameterNames() {
140         return Collections.enumeration(getParameterMap().keySet());
141     }
142 
143     /**
144      * Returns a <code>Map</code> of the parameters of this request.
145      *
146      * @return Map an immutable <code>Map</code> containing parameter names as keys
147      * and parameter values as map values. The keys in the parameter map are of type
148      * <code>String</code>. The values in the parameter map are of type
149      * <code>String</code> array.
150      * @see javax.servlet.ServletRequest#getParameterMap()
151      */
152     public Map getParameterMap() {
153         HashMap parameterMap = new HashMap();
154         parameterMap.putAll(super.getParameterMap());
155         parameterMap.putAll(this.parameters);
156 
157         return Collections.unmodifiableMap(parameterMap);
158     }
159 
160     /**
161      * Sets a parameter value.
162      *
163      * @param name String parameter name.
164      * @param value String parameter value.
165      */
166     private void setParameter(String name, String value) {
167         String[] multiValue = new String[1];
168         multiValue[0] = value;
169 
170         parameters.put(name, multiValue);
171     }
172 
173 }