View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts.upload;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import jakarta.servlet.http.HttpServletRequest;
31  import jakarta.servlet.http.HttpServletRequestWrapper;
32  
33  /**
34   * <p> This class functions as a wrapper around HttpServletRequest to provide
35   * working getParameter methods for multipart requests. </p>
36   */
37  public class MultipartRequestWrapper extends HttpServletRequestWrapper {
38      /**
39       * <p> The parameters for this multipart request </p>
40       */
41      protected Map<String, String[]> parameters;
42  
43      public MultipartRequestWrapper(HttpServletRequest request) {
44          super(request);
45          this.parameters = new HashMap<>();
46      }
47  
48      /**
49       * <p> Sets a parameter for this request.  The parameter is actually
50       * separate from the request parameters, but calling on the getParameter()
51       * methods of this class will work as if they weren't. </p>
52       */
53      public void setParameter(String name, String value) {
54          String[] mValue = parameters.get(name);
55  
56          if (mValue == null) {
57              mValue = new String[0];
58          }
59  
60          String[] newValue = new String[mValue.length + 1];
61  
62          System.arraycopy(mValue, 0, newValue, 0, mValue.length);
63          newValue[mValue.length] = value;
64  
65          parameters.put(name, newValue);
66      }
67  
68      /**
69       * <p> Attempts to get a parameter for this request.  It first looks in
70       * the underlying HttpServletRequest object for the parameter, and if that
71       * doesn't exist it looks for the parameters retrieved from the multipart
72       * request </p>
73       */
74      public String getParameter(String name) {
75          String value = getRequest().getParameter(name);
76  
77          if (value == null) {
78              String[] mValue = parameters.get(name);
79  
80              if ((mValue != null) && (mValue.length > 0)) {
81                  value = mValue[0];
82              }
83          }
84  
85          return value;
86      }
87  
88      /**
89       * <p> Returns the names of the parameters for this request. The
90       * enumeration consists of the normal request parameter names plus the
91       * parameters read from the multipart request </p>
92       */
93      public Enumeration<String> getParameterNames() {
94          Enumeration<String> baseParams = getRequest().getParameterNames();
95          ArrayList<String> list = new ArrayList<>();
96  
97          while (baseParams.hasMoreElements()) {
98              list.add(baseParams.nextElement());
99          }
100 
101         Collection<String> multipartParams = parameters.keySet();
102         list.addAll(multipartParams);
103 
104         return Collections.enumeration(list);
105     }
106 
107     /**
108      * <p> Returns the values of a parameter in this request. It first looks
109      * in the underlying HttpServletRequest object for the parameter, and if
110      * that doesn't exist it looks for the parameter retrieved from the
111      * multipart request. </p>
112      */
113     public String[] getParameterValues(String name) {
114         String[] value = getRequest().getParameterValues(name);
115 
116         if (value == null) {
117             value = parameters.get(name);
118         }
119 
120         return value;
121     }
122 
123     /**
124      * <p> Combines the parameters stored here with those in the underlying
125      * request. If paramater values in the underlying request take precedence
126      * over those stored here. </p>
127      */
128     public Map<String, String[]> getParameterMap() {
129         Map<String, String[]> map = new HashMap<>(parameters);
130 
131         map.putAll(getRequest().getParameterMap());
132 
133         return map;
134     }
135 }