1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37 public class MultipartRequestWrapper extends HttpServletRequestWrapper {
38
39
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
50
51
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
70
71
72
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
90
91
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
109
110
111
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
125
126
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 }