View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Sometimes one get the parameters via form (multipart-post) and via request (get, normal post). Using this Util you
50   * have not to care.
51   * @deprecated since 4.0 - should not be needed anymore since MAGNOLIA-2449 - request parameters should be correctly wrapped. To access uploaded documents, use {@link info.magnolia.cms.beans.runtime.MultipartForm}.
52   */
53  public class RequestFormUtil {
54  
55      private MultipartForm form;
56  
57      private HttpServletRequest request;
58  
59      public RequestFormUtil(HttpServletRequest request) {
60          this(request, MgnlContext.getPostedForm());
61      }
62  
63      public RequestFormUtil(HttpServletRequest request, MultipartForm form) {
64          super();
65          this.form = form;
66          this.request = request;
67      }
68  
69      /**
70       * @param name
71       * @return
72       */
73  
74      public String getParameter(String name) {
75          return getParameter(this.request, this.form, name);
76      }
77  
78      public static String getParameter(HttpServletRequest request, String name) {
79          return getParameter(request, MgnlContext.getPostedForm(), name);
80      }
81  
82      /**
83       * Returns the value found in the form or the request.
84       */
85      public static String getParameter(HttpServletRequest request, MultipartForm from, String name) {
86          String param = null;
87          if (from != null) {
88              param = from.getParameter(name);
89          }
90          if (param == null) {
91              if (request.getMethod().equals("GET")) {
92                  param = getURLParameterDecoded(request, name, "UTF8");
93              }
94              else {
95                  param = request.getParameter(name);
96              }
97          }
98          return param;
99  
100     }
101 
102     public String getParameter(String name, String defaultValue) {
103         return getParameter(this.request, this.form, name, defaultValue);
104     }
105 
106     public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
107         return getParameter(request, MgnlContext.getPostedForm(), name, defaultValue);
108     }
109 
110     /**
111      * Returns the defaultValue if the parameter is not found in the request or form.
112      */
113     public static String getParameter(HttpServletRequest request, MultipartForm from, String name, String defaultValue) {
114         String param = getParameter(request, from, name);
115         if (param == null) {
116             param = defaultValue;
117         }
118         return param;
119     }
120 
121     /**
122      * @param request
123      * @param charset
124      * @return decoded value
125      */
126     public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) {
127         return (String) getURLParametersDecoded(request, charset).get(name);
128     }
129 
130     /**
131      * The url is not always properly decoded. This method does the job.
132      * @param request
133      * @param charset
134      * @return decoded map of all values
135      */
136     public static Map getURLParametersDecoded(HttpServletRequest request, String charset) {
137 
138         String queryString = request.getQueryString();
139         if (queryString != null) {
140             return getURLParametersDecoded(queryString, charset);
141         }
142         return new HashMap();
143     }
144 
145     /**
146      * Extract and decodes parameters from a query string.
147      * @param queryString query string
148      * @param charset charset (e.g UTF-8)
149      */
150     public static Map getURLParametersDecoded(String queryString, String charset) {
151         Map map = new HashMap();
152         String[] params = queryString.split("&");
153         for (int i = 0; i < params.length; i++) {
154             String name = StringUtils.substringBefore(params[i], "=");
155             String value = StringUtils.substringAfter(params[i], "=");
156             try {
157                 value = URLDecoder.decode(value, charset);
158             }
159             catch (UnsupportedEncodingException e) {
160                 // nothing: return value as is
161             }
162             // @todo what about multi-valued parameters??
163             map.put(name, value);
164         }
165         return map;
166     }
167 
168     public MultipartForm getForm() {
169         return form;
170     }
171 
172     /*
173      * (non-Javadoc)
174      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocument(java.lang.String)
175      */
176     public Document getDocument(String name) {
177         return form.getDocument(name);
178     }
179 
180     /*
181      * (non-Javadoc)
182      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocuments()
183      */
184     public Map getDocuments() {
185         if (form == null) {
186             return new HashMap();
187         }
188 
189         return form.getDocuments();
190     }
191 
192     /*
193      * (non-Javadoc)
194      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameters()
195      */
196     public Map getParameters() {
197         return getParameters(this.request);
198     }
199 
200     public static Map getParameters(HttpServletRequest request) {
201         MultipartForm form = MgnlContext.getPostedForm();
202         if (form == null) {
203             // if get use UTF8 decoding
204             if (request.getMethod() == "GET") {
205                 return getURLParametersDecoded(request, "UTF8");
206             }
207             return request.getParameterMap();
208         }
209         return form.getParameters();
210     }
211 
212     /*
213      * (non-Javadoc)
214      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameterValues(java.lang.String)
215      */
216     public String[] getParameterValues(String name) {
217         if (this.form != null) {
218             return this.form.getParameterValues(name);
219         }
220         return request.getParameterValues(name);
221     }
222 
223     /*
224      * (non-Javadoc)
225      * @see info.magnolia.cms.beans.runtime.MultipartForm#removeParameter(java.lang.String)
226      */
227     public void removeParameter(String name) {
228         form.removeParameter(name);
229     }
230 
231 }