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  @Deprecated
57  public class RequestFormUtil {
58  
59      private MultipartForm form;
60  
61      private HttpServletRequest request;
62  
63      public RequestFormUtil(HttpServletRequest request) {
64          this(request, MgnlContext.getPostedForm());
65      }
66  
67      public RequestFormUtil(HttpServletRequest request, MultipartForm form) {
68          super();
69          this.form = form;
70          this.request = request;
71      }
72  
73      public String getParameter(String name) {
74          return getParameter(this.request, this.form, name);
75      }
76  
77      public static String getParameter(HttpServletRequest request, String name) {
78          return getParameter(request, MgnlContext.getPostedForm(), name);
79      }
80  
81      /**
82       * Returns the value found in the form or the request.
83       */
84      public static String getParameter(HttpServletRequest request, MultipartForm from, String name) {
85          String param = null;
86          if (from != null) {
87              param = from.getParameter(name);
88          }
89          if (param == null) {
90              if (request.getMethod().equals("GET")) {
91                  param = getURLParameterDecoded(request, name, "UTF8");
92              } else {
93                  param = request.getParameter(name);
94              }
95          }
96          return param;
97  
98      }
99  
100     public String getParameter(String name, String defaultValue) {
101         return getParameter(this.request, this.form, name, defaultValue);
102     }
103 
104     public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
105         return getParameter(request, MgnlContext.getPostedForm(), name, defaultValue);
106     }
107 
108     /**
109      * Returns the defaultValue if the parameter is not found in the request or form.
110      */
111     public static String getParameter(HttpServletRequest request, MultipartForm from, String name, String defaultValue) {
112         String param = getParameter(request, from, name);
113         if (param == null) {
114             param = defaultValue;
115         }
116         return param;
117     }
118 
119     /**
120      * @return decoded value
121      */
122     public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) {
123         return getURLParametersDecoded(request, charset).get(name);
124     }
125 
126     /**
127      * The url is not always properly decoded. This method does the job.
128      *
129      * @return decoded map of all values
130      */
131     public static Map<String, String> getURLParametersDecoded(HttpServletRequest request, String charset) {
132 
133         String queryString = request.getQueryString();
134         if (queryString != null) {
135             return getURLParametersDecoded(queryString, charset);
136         }
137         return new HashMap<String, String>();
138     }
139 
140     /**
141      * Extract and decodes parameters from a query string.
142      *
143      * @param queryString query string
144      * @param charset charset (e.g UTF-8)
145      */
146     public static Map<String, String> getURLParametersDecoded(String queryString, String charset) {
147         Map<String, String> map = new HashMap<String, String>();
148         String[] params = queryString.split("&");
149         for (int i = 0; i < params.length; i++) {
150             String name = StringUtils.substringBefore(params[i], "=");
151             String value = StringUtils.substringAfter(params[i], "=");
152             try {
153                 value = URLDecoder.decode(value, charset);
154             } catch (UnsupportedEncodingException e) {
155                 // nothing: return value as is
156             }
157             // @todo what about multi-valued parameters??
158             map.put(name, value);
159         }
160         return map;
161     }
162 
163     public MultipartForm getForm() {
164         return form;
165     }
166 
167     /*
168      * (non-Javadoc)
169      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocument(java.lang.String)
170      */
171     public Document getDocument(String name) {
172         return form.getDocument(name);
173     }
174 
175     /*
176      * (non-Javadoc)
177      * @see info.magnolia.cms.beans.runtime.MultipartForm#getDocuments()
178      */
179     public Map getDocuments() {
180         if (form == null) {
181             return new HashMap();
182         }
183 
184         return form.getDocuments();
185     }
186 
187     /*
188      * (non-Javadoc)
189      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameters()
190      */
191     public Map getParameters() {
192         return getParameters(this.request);
193     }
194 
195     public static Map getParameters(HttpServletRequest request) {
196         MultipartForm form = MgnlContext.getPostedForm();
197         if (form == null) {
198             // if get use UTF8 decoding
199             if (request.getMethod() == "GET") {
200                 return getURLParametersDecoded(request, "UTF8");
201             }
202             return request.getParameterMap();
203         }
204         return form.getParameters();
205     }
206 
207     /*
208      * (non-Javadoc)
209      * @see info.magnolia.cms.beans.runtime.MultipartForm#getParameterValues(java.lang.String)
210      */
211     public String[] getParameterValues(String name) {
212         if (this.form != null) {
213             return this.form.getParameterValues(name);
214         }
215         return request.getParameterValues(name);
216     }
217 
218     /*
219      * (non-Javadoc)
220      * @see info.magnolia.cms.beans.runtime.MultipartForm#removeParameter(java.lang.String)
221      */
222     public void removeParameter(String name) {
223         form.removeParameter(name);
224     }
225 
226 }