001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.text.DateFormat;
024:
025: import java.util.Date;
026: import java.util.Enumeration;
027:
028: import javax.portlet.PortletRequest;
029:
030: import javax.servlet.ServletRequest;
031:
032: /**
033: * <a href="ParamUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: * @author Raymond Augé
037: *
038: */
039: public class ParamUtil {
040:
041: // Servlet Request
042:
043: public static boolean getBoolean(ServletRequest req, String param) {
044: return GetterUtil.getBoolean(req.getParameter(param));
045: }
046:
047: public static boolean getBoolean(ServletRequest req, String param,
048: boolean defaultValue) {
049:
050: return get(req, param, defaultValue);
051: }
052:
053: public static boolean[] getBooleanValues(ServletRequest req,
054: String param) {
055:
056: return getBooleanValues(req, param, new boolean[0]);
057: }
058:
059: public static boolean[] getBooleanValues(ServletRequest req,
060: String param, boolean[] defaultValue) {
061:
062: return GetterUtil.getBooleanValues(req
063: .getParameterValues(param), defaultValue);
064: }
065:
066: public static Date getDate(ServletRequest req, String param,
067: DateFormat df) {
068:
069: return GetterUtil.getDate(req.getParameter(param), df);
070: }
071:
072: public static Date getDate(ServletRequest req, String param,
073: DateFormat df, Date defaultValue) {
074:
075: return get(req, param, df, defaultValue);
076: }
077:
078: public static double getDouble(ServletRequest req, String param) {
079: return GetterUtil.getDouble(req.getParameter(param));
080: }
081:
082: public static double getDouble(ServletRequest req, String param,
083: double defaultValue) {
084:
085: return get(req, param, defaultValue);
086: }
087:
088: public static double[] getDoubleValues(ServletRequest req,
089: String param) {
090: return getDoubleValues(req, param, new double[0]);
091: }
092:
093: public static double[] getDoubleValues(ServletRequest req,
094: String param, double[] defaultValue) {
095:
096: return GetterUtil.getDoubleValues(
097: req.getParameterValues(param), defaultValue);
098: }
099:
100: public static float getFloat(ServletRequest req, String param) {
101: return GetterUtil.getFloat(req.getParameter(param));
102: }
103:
104: public static float getFloat(ServletRequest req, String param,
105: float defaultValue) {
106:
107: return get(req, param, defaultValue);
108: }
109:
110: public static float[] getFloatValues(ServletRequest req,
111: String param) {
112: return getFloatValues(req, param, new float[0]);
113: }
114:
115: public static float[] getFloatValues(ServletRequest req,
116: String param, float[] defaultValue) {
117:
118: return GetterUtil.getFloatValues(req.getParameterValues(param),
119: 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(ServletRequest req, String param,
127: int defaultValue) {
128:
129: return get(req, param, defaultValue);
130: }
131:
132: public static int[] getIntegerValues(ServletRequest req,
133: String param) {
134: return getIntegerValues(req, param, new int[0]);
135: }
136:
137: public static int[] getIntegerValues(ServletRequest req,
138: String param, int[] defaultValue) {
139:
140: return GetterUtil.getIntegerValues(req
141: .getParameterValues(param), defaultValue);
142: }
143:
144: public static long getLong(ServletRequest req, String param) {
145: return GetterUtil.getLong(req.getParameter(param));
146: }
147:
148: public static long getLong(ServletRequest req, String param,
149: long defaultValue) {
150:
151: return get(req, param, defaultValue);
152: }
153:
154: public static long[] getLongValues(ServletRequest req, String param) {
155: return getLongValues(req, param, new long[0]);
156: }
157:
158: public static long[] getLongValues(ServletRequest req,
159: String param, long[] defaultValue) {
160:
161: return GetterUtil.getLongValues(req.getParameterValues(param),
162: defaultValue);
163: }
164:
165: public static short getShort(ServletRequest req, String param) {
166: return GetterUtil.getShort(req.getParameter(param));
167: }
168:
169: public static short getShort(ServletRequest req, String param,
170: short defaultValue) {
171:
172: return get(req, param, defaultValue);
173: }
174:
175: public static short[] getShortValues(ServletRequest req,
176: String param) {
177: return getShortValues(req, param, new short[0]);
178: }
179:
180: public static short[] getShortValues(ServletRequest req,
181: String param, short[] defaultValue) {
182:
183: return GetterUtil.getShortValues(req.getParameterValues(param),
184: defaultValue);
185: }
186:
187: public static String getString(ServletRequest req, String param) {
188: return GetterUtil.getString(req.getParameter(param));
189: }
190:
191: public static String getString(ServletRequest req, String param,
192: String defaultValue) {
193:
194: return get(req, param, defaultValue);
195: }
196:
197: public static boolean get(ServletRequest req, String param,
198: boolean defaultValue) {
199:
200: return GetterUtil.get(req.getParameter(param), defaultValue);
201: }
202:
203: public static Date get(ServletRequest req, String param,
204: DateFormat df, Date defaultValue) {
205:
206: return GetterUtil
207: .get(req.getParameter(param), df, defaultValue);
208: }
209:
210: public static double get(ServletRequest req, String param,
211: double defaultValue) {
212:
213: return GetterUtil.get(req.getParameter(param), defaultValue);
214: }
215:
216: public static float get(ServletRequest req, String param,
217: float defaultValue) {
218:
219: return GetterUtil.get(req.getParameter(param), defaultValue);
220: }
221:
222: public static int get(ServletRequest req, String param,
223: int defaultValue) {
224: return GetterUtil.get(req.getParameter(param), defaultValue);
225: }
226:
227: public static long get(ServletRequest req, String param,
228: long defaultValue) {
229:
230: return GetterUtil.get(req.getParameter(param), defaultValue);
231: }
232:
233: public static short get(ServletRequest req, String param,
234: short defaultValue) {
235:
236: return GetterUtil.get(req.getParameter(param), defaultValue);
237: }
238:
239: public static String get(ServletRequest req, String param,
240: String defaultValue) {
241:
242: String returnValue = GetterUtil.get(req.getParameter(param),
243: defaultValue);
244:
245: if (returnValue != null) {
246: return returnValue.trim();
247: }
248:
249: return null;
250: }
251:
252: public static void print(ServletRequest req) {
253: Enumeration e = req.getParameterNames();
254:
255: while (e.hasMoreElements()) {
256: String param = (String) e.nextElement();
257:
258: String[] values = req.getParameterValues(param);
259:
260: for (int i = 0; i < values.length; i++) {
261: System.out
262: .println(param + "[" + i + "] = " + values[i]);
263: }
264: }
265: }
266:
267: // Portlet Request
268:
269: public static boolean getBoolean(PortletRequest req, String param) {
270: return GetterUtil.getBoolean(req.getParameter(param));
271: }
272:
273: public static boolean getBoolean(PortletRequest req, String param,
274: boolean defaultValue) {
275:
276: return get(req, param, defaultValue);
277: }
278:
279: public static boolean[] getBooleanValues(PortletRequest req,
280: String param) {
281:
282: return getBooleanValues(req, param, new boolean[0]);
283: }
284:
285: public static boolean[] getBooleanValues(PortletRequest req,
286: String param, boolean[] defaultValue) {
287:
288: return GetterUtil.getBooleanValues(req
289: .getParameterValues(param), defaultValue);
290: }
291:
292: public static Date getDate(PortletRequest req, String param,
293: DateFormat df) {
294:
295: return GetterUtil.getDate(req.getParameter(param), df);
296: }
297:
298: public static Date getDate(PortletRequest req, String param,
299: DateFormat df, Date defaultValue) {
300:
301: return get(req, param, df, defaultValue);
302: }
303:
304: public static double getDouble(PortletRequest req, String param) {
305: return GetterUtil.getDouble(req.getParameter(param));
306: }
307:
308: public static double getDouble(PortletRequest req, String param,
309: double defaultValue) {
310:
311: return get(req, param, defaultValue);
312: }
313:
314: public static double[] getDoubleValues(PortletRequest req,
315: String param) {
316: return getDoubleValues(req, param, new double[0]);
317: }
318:
319: public static double[] getDoubleValues(PortletRequest req,
320: String param, double[] defaultValue) {
321:
322: return GetterUtil.getDoubleValues(
323: req.getParameterValues(param), defaultValue);
324: }
325:
326: public static float getFloat(PortletRequest req, String param) {
327: return GetterUtil.getFloat(req.getParameter(param));
328: }
329:
330: public static float getFloat(PortletRequest req, String param,
331: float defaultValue) {
332:
333: return get(req, param, defaultValue);
334: }
335:
336: public static float[] getFloatValues(PortletRequest req,
337: String param) {
338: return getFloatValues(req, param, new float[0]);
339: }
340:
341: public static float[] getFloatValues(PortletRequest req,
342: String param, float[] defaultValue) {
343:
344: return GetterUtil.getFloatValues(req.getParameterValues(param),
345: defaultValue);
346: }
347:
348: public static int getInteger(PortletRequest req, String param) {
349: return GetterUtil.getInteger(req.getParameter(param));
350: }
351:
352: public static int getInteger(PortletRequest req, String param,
353: int defaultValue) {
354:
355: return get(req, param, defaultValue);
356: }
357:
358: public static int[] getIntegerValues(PortletRequest req,
359: String param) {
360: return getIntegerValues(req, param, new int[0]);
361: }
362:
363: public static int[] getIntegerValues(PortletRequest req,
364: String param, int[] defaultValue) {
365:
366: return GetterUtil.getIntegerValues(req
367: .getParameterValues(param), defaultValue);
368: }
369:
370: public static long getLong(PortletRequest req, String param) {
371: return GetterUtil.getLong(req.getParameter(param));
372: }
373:
374: public static long getLong(PortletRequest req, String param,
375: long defaultValue) {
376:
377: return get(req, param, defaultValue);
378: }
379:
380: public static long[] getLongValues(PortletRequest req, String param) {
381: return getLongValues(req, param, new long[0]);
382: }
383:
384: public static long[] getLongValues(PortletRequest req,
385: String param, long[] defaultValue) {
386:
387: return GetterUtil.getLongValues(req.getParameterValues(param),
388: defaultValue);
389: }
390:
391: public static short getShort(PortletRequest req, String param) {
392: return GetterUtil.getShort(req.getParameter(param));
393: }
394:
395: public static short getShort(PortletRequest req, String param,
396: short defaultValue) {
397:
398: return get(req, param, defaultValue);
399: }
400:
401: public static short[] getShortValues(PortletRequest req,
402: String param) {
403: return getShortValues(req, param, new short[0]);
404: }
405:
406: public static short[] getShortValues(PortletRequest req,
407: String param, short[] defaultValue) {
408:
409: return GetterUtil.getShortValues(req.getParameterValues(param),
410: defaultValue);
411: }
412:
413: public static String getString(PortletRequest req, String param) {
414: return GetterUtil.getString(req.getParameter(param));
415: }
416:
417: public static String getString(PortletRequest req, String param,
418: String defaultValue) {
419:
420: return get(req, param, defaultValue);
421: }
422:
423: public static boolean get(PortletRequest req, String param,
424: boolean defaultValue) {
425:
426: return GetterUtil.get(req.getParameter(param), defaultValue);
427: }
428:
429: public static Date get(PortletRequest req, String param,
430: DateFormat df, Date defaultValue) {
431:
432: return GetterUtil
433: .get(req.getParameter(param), df, defaultValue);
434: }
435:
436: public static double get(PortletRequest req, String param,
437: double defaultValue) {
438:
439: return GetterUtil.get(req.getParameter(param), defaultValue);
440: }
441:
442: public static float get(PortletRequest req, String param,
443: float defaultValue) {
444:
445: return GetterUtil.get(req.getParameter(param), defaultValue);
446: }
447:
448: public static int get(PortletRequest req, String param,
449: int defaultValue) {
450: return GetterUtil.get(req.getParameter(param), defaultValue);
451: }
452:
453: public static long get(PortletRequest req, String param,
454: long defaultValue) {
455:
456: return GetterUtil.get(req.getParameter(param), defaultValue);
457: }
458:
459: public static short get(PortletRequest req, String param,
460: short defaultValue) {
461:
462: return GetterUtil.get(req.getParameter(param), defaultValue);
463: }
464:
465: public static String get(PortletRequest req, String param,
466: String defaultValue) {
467:
468: String returnValue = GetterUtil.get(req.getParameter(param),
469: defaultValue);
470:
471: if (returnValue != null) {
472: return returnValue.trim();
473: }
474:
475: return null;
476: }
477:
478: public static void print(PortletRequest req) {
479: Enumeration e = req.getParameterNames();
480:
481: while (e.hasMoreElements()) {
482: String param = (String) e.nextElement();
483:
484: String[] values = req.getParameterValues(param);
485:
486: for (int i = 0; i < values.length; i++) {
487: System.out
488: .println(param + "[" + i + "] = " + values[i]);
489: }
490: }
491: }
492:
493: }
|