1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.text.DateFormat;
26  
27  import java.util.Date;
28  import java.util.Enumeration;
29  
30  import javax.portlet.PortletRequest;
31  
32  import javax.servlet.ServletRequest;
33  
34  /**
35   * <a href="ParamUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Raymond Augé
39   *
40   */
41  public class ParamUtil {
42  
43      // Servlet Request
44  
45      public static boolean getBoolean(ServletRequest req, String param) {
46          return GetterUtil.getBoolean(req.getParameter(param));
47      }
48  
49      public static boolean getBoolean(
50          ServletRequest req, String param, boolean defaultValue) {
51  
52          return get(req, param, defaultValue);
53      }
54  
55      public static boolean[] getBooleanValues(
56          ServletRequest req, String param) {
57  
58          return getBooleanValues(req, param, new boolean[0]);
59      }
60  
61      public static boolean[] getBooleanValues(
62          ServletRequest req, String param, boolean[] defaultValue) {
63  
64          return GetterUtil.getBooleanValues(
65              req.getParameterValues(param), defaultValue);
66      }
67  
68      public static Date getDate(
69          ServletRequest req, String param, DateFormat df) {
70  
71          return GetterUtil.getDate(req.getParameter(param), df);
72      }
73  
74      public static Date getDate(
75          ServletRequest req, String param, DateFormat df, Date defaultValue) {
76  
77          return get(req, param, df, defaultValue);
78      }
79  
80      public static double getDouble(ServletRequest req, String param) {
81          return GetterUtil.getDouble(req.getParameter(param));
82      }
83  
84      public static double getDouble(
85          ServletRequest req, String param, double defaultValue) {
86  
87          return get(req, param, defaultValue);
88      }
89  
90      public static double[] getDoubleValues(ServletRequest req, String param) {
91          return getDoubleValues(req, param, new double[0]);
92      }
93  
94      public static double[] getDoubleValues(
95          ServletRequest req, String param, double[] defaultValue) {
96  
97          return GetterUtil.getDoubleValues(
98              req.getParameterValues(param), defaultValue);
99      }
100 
101     public static float getFloat(ServletRequest req, String param) {
102         return GetterUtil.getFloat(req.getParameter(param));
103     }
104 
105     public static float getFloat(
106         ServletRequest req, String param, float defaultValue) {
107 
108         return get(req, param, defaultValue);
109     }
110 
111     public static float[] getFloatValues(ServletRequest req, String param) {
112         return getFloatValues(req, param, new float[0]);
113     }
114 
115     public static float[] getFloatValues(
116         ServletRequest req, String param, float[] defaultValue) {
117 
118         return GetterUtil.getFloatValues(
119             req.getParameterValues(param), defaultValue);
120     }
121 
122     public static int getInteger(ServletRequest req, String param) {
123         return GetterUtil.getInteger(req.getParameter(param));
124     }
125 
126     public static int getInteger(
127         ServletRequest req, String param, int defaultValue) {
128 
129         return get(req, param, defaultValue);
130     }
131 
132     public static int[] getIntegerValues(ServletRequest req, String param) {
133         return getIntegerValues(req, param, new int[0]);
134     }
135 
136     public static int[] getIntegerValues(
137         ServletRequest req, String param, int[] defaultValue) {
138 
139         return GetterUtil.getIntegerValues(
140             req.getParameterValues(param), defaultValue);
141     }
142 
143     public static long getLong(ServletRequest req, String param) {
144         return GetterUtil.getLong(req.getParameter(param));
145     }
146 
147     public static long getLong(
148         ServletRequest req, String param, long defaultValue) {
149 
150         return get(req, param, defaultValue);
151     }
152 
153     public static long[] getLongValues(ServletRequest req, String param) {
154         return getLongValues(req, param, new long[0]);
155     }
156 
157     public static long[] getLongValues(
158         ServletRequest req, String param, long[] defaultValue) {
159 
160         return GetterUtil.getLongValues(
161             req.getParameterValues(param), defaultValue);
162     }
163 
164     public static short getShort(ServletRequest req, String param) {
165         return GetterUtil.getShort(req.getParameter(param));
166     }
167 
168     public static short getShort(
169         ServletRequest req, String param, short defaultValue) {
170 
171         return get(req, param, defaultValue);
172     }
173 
174     public static short[] getShortValues(ServletRequest req, String param) {
175         return getShortValues(req, param, new short[0]);
176     }
177 
178     public static short[] getShortValues(
179         ServletRequest req, String param, short[] defaultValue) {
180 
181         return GetterUtil.getShortValues(
182             req.getParameterValues(param), defaultValue);
183     }
184 
185     public static String getString(ServletRequest req, String param) {
186         return GetterUtil.getString(req.getParameter(param));
187     }
188 
189     public static String getString(
190         ServletRequest req, String param, String defaultValue) {
191 
192         return get(req, param, defaultValue);
193     }
194 
195     public static boolean get(
196         ServletRequest req, String param, boolean defaultValue) {
197 
198         return GetterUtil.get(req.getParameter(param), defaultValue);
199     }
200 
201     public static Date get(
202         ServletRequest req, String param, DateFormat df, Date defaultValue) {
203 
204         return GetterUtil.get(req.getParameter(param), df, defaultValue);
205     }
206 
207     public static double get(
208         ServletRequest req, String param, double defaultValue) {
209 
210         return GetterUtil.get(req.getParameter(param), defaultValue);
211     }
212 
213     public static float get(
214         ServletRequest req, String param, float defaultValue) {
215 
216         return GetterUtil.get(req.getParameter(param), defaultValue);
217     }
218 
219     public static int get(ServletRequest req, String param, int defaultValue) {
220         return GetterUtil.get(req.getParameter(param), defaultValue);
221     }
222 
223     public static long get(
224         ServletRequest req, String param, long defaultValue) {
225 
226         return GetterUtil.get(req.getParameter(param), defaultValue);
227     }
228 
229     public static short get(
230         ServletRequest req, String param, short defaultValue) {
231 
232         return GetterUtil.get(req.getParameter(param), defaultValue);
233     }
234 
235     public static String get(
236         ServletRequest req, String param, String defaultValue) {
237 
238         String returnValue =
239             GetterUtil.get(req.getParameter(param), defaultValue);
240 
241         if (returnValue != null) {
242             return returnValue.trim();
243         }
244 
245         return null;
246     }
247 
248     public static void print(ServletRequest req) {
249         Enumeration<String> enu = req.getParameterNames();
250 
251         while (enu.hasMoreElements()) {
252             String param = enu.nextElement();
253 
254             String[] values = req.getParameterValues(param);
255 
256             for (int i = 0; i < values.length; i++) {
257                 System.out.println(param + "[" + i + "] = " + values[i]);
258             }
259         }
260     }
261 
262     // Portlet Request
263 
264     public static boolean getBoolean(PortletRequest req, String param) {
265         return GetterUtil.getBoolean(req.getParameter(param));
266     }
267 
268     public static boolean getBoolean(
269         PortletRequest req, String param, boolean defaultValue) {
270 
271         return get(req, param, defaultValue);
272     }
273 
274     public static boolean[] getBooleanValues(
275         PortletRequest req, String param) {
276 
277         return getBooleanValues(req, param, new boolean[0]);
278     }
279 
280     public static boolean[] getBooleanValues(
281         PortletRequest req, String param, boolean[] defaultValue) {
282 
283         return GetterUtil.getBooleanValues(
284             req.getParameterValues(param), defaultValue);
285     }
286 
287     public static Date getDate(
288         PortletRequest req, String param, DateFormat df) {
289 
290         return GetterUtil.getDate(req.getParameter(param), df);
291     }
292 
293     public static Date getDate(
294         PortletRequest req, String param, DateFormat df, Date defaultValue) {
295 
296         return get(req, param, df, defaultValue);
297     }
298 
299     public static double getDouble(PortletRequest req, String param) {
300         return GetterUtil.getDouble(req.getParameter(param));
301     }
302 
303     public static double getDouble(
304         PortletRequest req, String param, double defaultValue) {
305 
306         return get(req, param, defaultValue);
307     }
308 
309     public static double[] getDoubleValues(PortletRequest req, String param) {
310         return getDoubleValues(req, param, new double[0]);
311     }
312 
313     public static double[] getDoubleValues(
314         PortletRequest req, String param, double[] defaultValue) {
315 
316         return GetterUtil.getDoubleValues(
317             req.getParameterValues(param), defaultValue);
318     }
319 
320     public static float getFloat(PortletRequest req, String param) {
321         return GetterUtil.getFloat(req.getParameter(param));
322     }
323 
324     public static float getFloat(
325         PortletRequest req, String param, float defaultValue) {
326 
327         return get(req, param, defaultValue);
328     }
329 
330     public static float[] getFloatValues(PortletRequest req, String param) {
331         return getFloatValues(req, param, new float[0]);
332     }
333 
334     public static float[] getFloatValues(
335         PortletRequest req, String param, float[] defaultValue) {
336 
337         return GetterUtil.getFloatValues(
338             req.getParameterValues(param), defaultValue);
339     }
340 
341     public static int getInteger(PortletRequest req, String param) {
342         return GetterUtil.getInteger(req.getParameter(param));
343     }
344 
345     public static int getInteger(
346         PortletRequest req, String param, int defaultValue) {
347 
348         return get(req, param, defaultValue);
349     }
350 
351     public static int[] getIntegerValues(PortletRequest req, String param) {
352         return getIntegerValues(req, param, new int[0]);
353     }
354 
355     public static int[] getIntegerValues(
356         PortletRequest req, String param, int[] defaultValue) {
357 
358         return GetterUtil.getIntegerValues(
359             req.getParameterValues(param), defaultValue);
360     }
361 
362     public static long getLong(PortletRequest req, String param) {
363         return GetterUtil.getLong(req.getParameter(param));
364     }
365 
366     public static long getLong(
367         PortletRequest req, String param, long defaultValue) {
368 
369         return get(req, param, defaultValue);
370     }
371 
372     public static long[] getLongValues(PortletRequest req, String param) {
373         return getLongValues(req, param, new long[0]);
374     }
375 
376     public static long[] getLongValues(
377         PortletRequest req, String param, long[] defaultValue) {
378 
379         return GetterUtil.getLongValues(
380             req.getParameterValues(param), defaultValue);
381     }
382 
383     public static short getShort(PortletRequest req, String param) {
384         return GetterUtil.getShort(req.getParameter(param));
385     }
386 
387     public static short getShort(
388         PortletRequest req, String param, short defaultValue) {
389 
390         return get(req, param, defaultValue);
391     }
392 
393     public static short[] getShortValues(PortletRequest req, String param) {
394         return getShortValues(req, param, new short[0]);
395     }
396 
397     public static short[] getShortValues(
398         PortletRequest req, String param, short[] defaultValue) {
399 
400         return GetterUtil.getShortValues(
401             req.getParameterValues(param), defaultValue);
402     }
403 
404     public static String getString(PortletRequest req, String param) {
405         return GetterUtil.getString(req.getParameter(param));
406     }
407 
408     public static String getString(
409         PortletRequest req, String param, String defaultValue) {
410 
411         return get(req, param, defaultValue);
412     }
413 
414     public static boolean get(
415         PortletRequest req, String param, boolean defaultValue) {
416 
417         return GetterUtil.get(req.getParameter(param), defaultValue);
418     }
419 
420     public static Date get(
421         PortletRequest req, String param, DateFormat df, Date defaultValue) {
422 
423         return GetterUtil.get(req.getParameter(param), df, defaultValue);
424     }
425 
426     public static double get(
427         PortletRequest req, String param, double defaultValue) {
428 
429         return GetterUtil.get(req.getParameter(param), defaultValue);
430     }
431 
432     public static float get(
433         PortletRequest req, String param, float defaultValue) {
434 
435         return GetterUtil.get(req.getParameter(param), defaultValue);
436     }
437 
438     public static int get(PortletRequest req, String param, int defaultValue) {
439         return GetterUtil.get(req.getParameter(param), defaultValue);
440     }
441 
442     public static long get(
443         PortletRequest req, String param, long defaultValue) {
444 
445         return GetterUtil.get(req.getParameter(param), defaultValue);
446     }
447 
448     public static short get(
449         PortletRequest req, String param, short defaultValue) {
450 
451         return GetterUtil.get(req.getParameter(param), defaultValue);
452     }
453 
454     public static String get(
455         PortletRequest req, String param, String defaultValue) {
456 
457         String returnValue =
458             GetterUtil.get(req.getParameter(param), defaultValue);
459 
460         if (returnValue != null) {
461             return returnValue.trim();
462         }
463 
464         return null;
465     }
466 
467     public static void print(PortletRequest req) {
468         Enumeration<String> enu = req.getParameterNames();
469 
470         while (enu.hasMoreElements()) {
471             String param = enu.nextElement();
472 
473             String[] values = req.getParameterValues(param);
474 
475             for (int i = 0; i < values.length; i++) {
476                 System.out.println(param + "[" + i + "] = " + values[i]);
477             }
478         }
479     }
480 
481 }