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