1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.beans.runtime.Document;
37 import info.magnolia.cms.beans.runtime.MultipartForm;
38 import info.magnolia.context.MgnlContext;
39 import org.apache.commons.lang.StringUtils;
40
41 import javax.servlet.http.HttpServletRequest;
42 import java.io.UnsupportedEncodingException;
43 import java.net.URLDecoder;
44 import java.util.HashMap;
45 import java.util.Map;
46
47
48
49
50
51
52
53
54 public class RequestFormUtil {
55
56 private MultipartForm form;
57
58 private HttpServletRequest request;
59
60 public RequestFormUtil(HttpServletRequest request) {
61 this(request, MgnlContext.getPostedForm());
62 }
63
64 public RequestFormUtil(HttpServletRequest request, MultipartForm form) {
65 super();
66 this.form = form;
67 this.request = request;
68 }
69
70
71
72
73
74
75 public String getParameter(String name) {
76 return getParameter(this.request, this.form, name);
77 }
78
79 public static String getParameter(HttpServletRequest request, String name) {
80 return getParameter(request, MgnlContext.getPostedForm(), name);
81 }
82
83
84
85
86
87
88
89
90 public static String getParameter(HttpServletRequest request, MultipartForm from, String name) {
91 String param = null;
92 if (from != null) {
93 param = from.getParameter(name);
94 }
95 if (param == null) {
96 if (request.getMethod().equals("GET")) {
97 param = getURLParameterDecoded(request, name, "UTF8");
98 }
99 else {
100 param = request.getParameter(name);
101 }
102 }
103 return param;
104
105 }
106
107 public String getParameter(String name, String defaultValue) {
108 return getParameter(this.request, this.form, name, defaultValue);
109 }
110
111 public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
112 return getParameter(request, MgnlContext.getPostedForm(), name, defaultValue);
113 }
114
115
116
117
118
119
120
121 public static String getParameter(HttpServletRequest request, MultipartForm from, String name, String defaultValue) {
122 String param = getParameter(request, from, name);
123 if (param == null) {
124 param = defaultValue;
125 }
126 return param;
127 }
128
129
130
131
132
133
134 public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) {
135 return (String) getURLParametersDecoded(request, charset).get(name);
136 }
137
138
139
140
141
142
143
144 public static Map getURLParametersDecoded(HttpServletRequest request, String charset) {
145
146 String queryString = request.getQueryString();
147 if (queryString != null) {
148 return getURLParametersDecoded(queryString, charset);
149 }
150 return new HashMap();
151 }
152
153
154
155
156
157
158 public static Map getURLParametersDecoded(String queryString, String charset) {
159 Map map = new HashMap();
160 String[] params = queryString.split("&");
161 for (int i = 0; i < params.length; i++) {
162 String name = StringUtils.substringBefore(params[i], "=");
163 String value = StringUtils.substringAfter(params[i], "=");
164 try {
165 value = URLDecoder.decode(value, charset);
166 }
167 catch (UnsupportedEncodingException e) {
168
169 }
170
171 map.put(name, value);
172 }
173 return map;
174 }
175
176 public MultipartForm getForm() {
177 return form;
178 }
179
180
181
182
183
184 public Document getDocument(String name) {
185 return form.getDocument(name);
186 }
187
188
189
190
191
192 public Map getDocuments() {
193 if (form == null) {
194 return new HashMap();
195 }
196
197 return form.getDocuments();
198 }
199
200
201
202
203
204 public Map getParameters() {
205 return getParameters(this.request);
206 }
207
208 public static Map getParameters(HttpServletRequest request) {
209 MultipartForm form = MgnlContext.getPostedForm();
210 if (form == null) {
211
212 if (request.getMethod() == "GET") {
213 return getURLParametersDecoded(request, "UTF8");
214 }
215 return request.getParameterMap();
216 }
217 return form.getParameters();
218 }
219
220
221
222
223
224 public String[] getParameterValues(String name) {
225 if (this.form != null) {
226 return this.form.getParameterValues(name);
227 }
228 return request.getParameterValues(name);
229 }
230
231
232
233
234
235 public void removeParameter(String name) {
236 form.removeParameter(name);
237 }
238
239 }