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