View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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  
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   * Sometimes one get the parameters via form (multipart-post) and via request (get, normal post). Using this Util you
52   * have not to care.
53   * @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}.
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       * @param name
73       * @return
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       * Returns the value found in the form or the request.
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      * Returns the defaultValue if the parameter is not found in the request or form.
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      * @param request
125      * @param charset
126      * @return decoded value
127      */
128     public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) {
129         return getURLParametersDecoded(request, charset).get(name);
130     }
131 
132     /**
133      * The url is not always properly decoded. This method does the job.
134      * @param request
135      * @param charset
136      * @return decoded map of all values
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      * Extract and decodes parameters from a query string.
149      * @param queryString query string
150      * @param charset charset (e.g UTF-8)
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                 // nothing: return value as is
163             }
164             // @todo what about multi-valued parameters??
165             map.put(name, value);
166         }
167         return map;
168     }
169 
170     public MultipartForm getForm() {
171         return form;
172     }
173 
174     /*
175      * (non-Javadoc)
176      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocument(java.lang.String)
177      */
178     public Document getDocument(String name) {
179         return form.getDocument(name);
180     }
181 
182     /*
183      * (non-Javadoc)
184      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocuments()
185      */
186     public Map getDocuments() {
187         if (form == null) {
188             return new HashMap();
189         }
190 
191         return form.getDocuments();
192     }
193 
194     /*
195      * (non-Javadoc)
196      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameters()
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             // if get use UTF8 decoding
206             if (request.getMethod() == "GET") {
207                 return getURLParametersDecoded(request, "UTF8");
208             }
209             return request.getParameterMap();
210         }
211         return form.getParameters();
212     }
213 
214     /*
215      * (non-Javadoc)
216      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameterValues(java.lang.String)
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      * (non-Javadoc)
227      * @see info.magnolia.cms.beans.runtime.MultipartForm#removeParameter(java.lang.String)
228      */
229     public void removeParameter(String name) {
230         form.removeParameter(name);
231     }
232 
233 }