KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > RequestFormUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.cms.beans.runtime.Document;
16 import info.magnolia.cms.beans.runtime.MultipartForm;
17
18 import java.io.UnsupportedEncodingException JavaDoc;
19 import java.net.URLDecoder JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26
27
28 /**
29  * Sometimes one get the parameters via form (multipart-post) and via request (get, normal post). Using this Util you
30  * have not to care.
31  * @author philipp
32  */

33 public class RequestFormUtil {
34
35     private MultipartForm form;
36
37     private HttpServletRequest JavaDoc request;
38
39     public RequestFormUtil(HttpServletRequest JavaDoc request) {
40         this(request, Resource.getPostedForm(request));
41     }
42
43     public RequestFormUtil(HttpServletRequest JavaDoc request, MultipartForm form) {
44         super();
45         this.form = form;
46         this.request = request;
47     }
48
49     /**
50      * @param name
51      * @return
52      */

53
54     public String JavaDoc getParameter(String JavaDoc name) {
55         return RequestFormUtil.getParameter(this.request, this.form, name);
56     }
57
58     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc name) {
59         return RequestFormUtil.getParameter(request, Resource.getPostedForm(request), name);
60     }
61
62     /**
63      * returns the value found in the form or the request
64      * @param request
65      * @param form
66      * @param name
67      * @return
68      */

69     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, MultipartForm from, String JavaDoc name) {
70         String JavaDoc param = null;
71         if (from != null) {
72             param = from.getParameter(name);
73         }
74         if (param == null) {
75             if (request.getMethod().equals("GET")) {
76                 param = getURLParameterDecoded(request, name, "UTF8");
77             }
78             else {
79                 param = request.getParameter(name);
80             }
81         }
82         return param;
83
84     }
85
86     public String JavaDoc getParameter(String JavaDoc name, String JavaDoc defaultValue) {
87         return RequestFormUtil.getParameter(this.request, this.form, name, defaultValue);
88     }
89
90     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc name, String JavaDoc defaultValue) {
91         return RequestFormUtil.getParameter(request, Resource.getPostedForm(request), name, defaultValue);
92     }
93
94     /**
95      * returns the defaultValue if the parameter is not found in the request or form
96      * @param string
97      * @param repository
98      * @return
99      */

100     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, MultipartForm from, String JavaDoc name, String JavaDoc defaultValue) {
101         String JavaDoc param = getParameter(request, from, name);
102         if (param == null) {
103             param = defaultValue;
104         }
105         return param;
106     }
107
108     /**
109      * @param request
110      * @param charset
111      * @return decoded value
112      */

113     public static String JavaDoc getURLParameterDecoded(HttpServletRequest JavaDoc request, String JavaDoc name, String JavaDoc charset) {
114         return (String JavaDoc) getURLParametersDecoded(request, charset).get(name);
115     }
116
117     /**
118      * The url is not always properly decoded. This method does the job.
119      * @param request
120      * @param charset
121      * @return decoded map of all values
122      */

123     public static Map JavaDoc getURLParametersDecoded(HttpServletRequest JavaDoc request, String JavaDoc charset) {
124         Map JavaDoc map = new HashMap JavaDoc();
125         String JavaDoc queryString = request.getQueryString();
126         if (queryString != null) {
127             String JavaDoc[] params = request.getQueryString().split("&");
128             for (int i = 0; i < params.length; i++) {
129                 String JavaDoc name = StringUtils.substringBefore(params[i], "=");
130                 String JavaDoc value = StringUtils.substringAfter(params[i], "=");
131                 try {
132                     value = URLDecoder.decode(value, charset);
133                 }
134                 catch (UnsupportedEncodingException JavaDoc e) {
135                     // nothing: return value as is
136
}
137                 map.put(name, value);
138             }
139         }
140         return map;
141     }
142
143     public MultipartForm getForm() {
144         return form;
145     }
146
147     /*
148      * (non-Javadoc)
149      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocument(java.lang.String)
150      */

151     public Document getDocument(String JavaDoc name) {
152         return form.getDocument(name);
153     }
154
155     /*
156      * (non-Javadoc)
157      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocuments()
158      */

159     public Map JavaDoc getDocuments() {
160         if (form == null) {
161             return new HashMap JavaDoc();
162         }
163
164         return form.getDocuments();
165     }
166
167     /*
168      * (non-Javadoc)
169      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameters()
170      */

171     public Map JavaDoc getParameters() {
172         return getParameters(this.request);
173     }
174
175     public static Map JavaDoc getParameters(HttpServletRequest JavaDoc request) {
176         MultipartForm form = Resource.getPostedForm(request);
177         if (form == null) {
178             // if get use UTF8 decoding
179
if (request.getMethod() == "GET") {
180                 return getURLParametersDecoded(request, "UTF8");
181             }
182             return request.getParameterMap();
183         }
184         return form.getParameters();
185     }
186
187     /*
188      * (non-Javadoc)
189      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameterValues(java.lang.String)
190      */

191     public String JavaDoc[] getParameterValues(String JavaDoc name) {
192         if (this.form != null) {
193             return this.form.getParameterValues(name);
194         }
195         return request.getParameterValues(name);
196     }
197
198     /*
199      * (non-Javadoc)
200      * @see info.magnolia.cms.beans.runtime.MultipartForm#removeParameter(java.lang.String)
201      */

202     public void removeParameter(String JavaDoc name) {
203         form.removeParameter(name);
204     }
205
206 }
Popular Tags