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