View Javadoc
1   /**
2    * This file Copyright (c) 2003-2018 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.lang3.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   *
54   * @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}.
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       * Returns the value found in the form or the request.
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      * Returns the defaultValue if the parameter is not found in the request or form.
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      * @return decoded value
120      */
121     public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) {
122         return getURLParametersDecoded(request, charset).get(name);
123     }
124 
125     /**
126      * The url is not always properly decoded. This method does the job.
127      *
128      * @return decoded map of all values
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      * Extract and decodes parameters from a query string.
141      *
142      * @param queryString query string
143      * @param charset charset (e.g UTF-8)
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                 // nothing: return value as is
155             }
156             // @todo what about multi-valued parameters??
157             map.put(name, value);
158         }
159         return map;
160     }
161 
162     public MultipartForm getForm() {
163         return form;
164     }
165 
166     /*
167      * (non-Javadoc)
168      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocument(java.lang.String)
169      */
170     public Document getDocument(String name) {
171         return form.getDocument(name);
172     }
173 
174     /*
175      * (non-Javadoc)
176      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocuments()
177      */
178     public Map getDocuments() {
179         if (form == null) {
180             return new HashMap();
181         }
182 
183         return form.getDocuments();
184     }
185 
186     /*
187      * (non-Javadoc)
188      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameters()
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             // if get use UTF8 decoding
198             if (request.getMethod() == "GET") {
199                 return getURLParametersDecoded(request, "UTF8");
200             }
201             return request.getParameterMap();
202         }
203         return form.getParameters();
204     }
205 
206     /*
207      * (non-Javadoc)
208      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameterValues(java.lang.String)
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      * (non-Javadoc)
219      * @see info.magnolia.cms.beans.runtime.MultipartForm#removeParameter(java.lang.String)
220      */
221     public void removeParameter(String name) {
222         form.removeParameter(name);
223     }
224 
225 }