1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.Properties;
18  
19  import javax.portlet.PortletRequest;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  /**
24   * <a href="PropertiesParamUtil.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class PropertiesParamUtil {
29  
30      public static boolean getBoolean(
31          Properties properties, HttpServletRequest request, String param) {
32  
33          return getBoolean(
34              properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
35      }
36  
37      public static boolean getBoolean(
38          Properties properties, HttpServletRequest request, String param,
39          boolean defaultValue) {
40  
41          String propertiesValue = properties.getProperty(param, null);
42  
43          boolean getterUtilValue = GetterUtil.getBoolean(
44              propertiesValue, defaultValue);
45  
46          return ParamUtil.get(request, param, getterUtilValue);
47      }
48  
49      public static boolean getBoolean(
50          Properties properties, PortletRequest portletRequest, String param) {
51  
52          return getBoolean(
53              properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
54      }
55  
56      public static boolean getBoolean(
57          Properties properties, PortletRequest portletRequest, String param,
58          boolean defaultValue) {
59  
60          String propertiesValue = properties.getProperty(param, null);
61  
62          boolean getterUtilValue = GetterUtil.getBoolean(
63              propertiesValue, defaultValue);
64  
65          return ParamUtil.get(portletRequest, param, getterUtilValue);
66      }
67  
68      public static boolean getBoolean(
69          UnicodeProperties properties, HttpServletRequest request,
70          String param) {
71  
72          return getBoolean(
73              properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
74      }
75  
76      public static boolean getBoolean(
77          UnicodeProperties properties, HttpServletRequest request, String param,
78          boolean defaultValue) {
79  
80          String propertiesValue = properties.getProperty(param, null);
81  
82          boolean getterUtilValue = GetterUtil.getBoolean(
83              propertiesValue, defaultValue);
84  
85          return ParamUtil.get(request, param, getterUtilValue);
86      }
87  
88      public static boolean getBoolean(
89          UnicodeProperties properties, PortletRequest portletRequest,
90          String param) {
91  
92          return getBoolean(
93              properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
94      }
95  
96      public static boolean getBoolean(
97          UnicodeProperties properties, PortletRequest portletRequest,
98          String param, boolean defaultValue) {
99  
100         String propertiesValue = properties.getProperty(param, null);
101 
102         boolean getterUtilValue = GetterUtil.getBoolean(
103             propertiesValue, defaultValue);
104 
105         return ParamUtil.get(portletRequest, param, getterUtilValue);
106     }
107 
108     public static double getDouble(
109         Properties properties, HttpServletRequest request, String param) {
110 
111         return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
112     }
113 
114     public static double getDouble(
115         Properties properties, HttpServletRequest request, String param,
116         double defaultValue) {
117 
118         String propertiesValue = properties.getProperty(param, null);
119 
120         double getterUtilValue = GetterUtil.getDouble(
121             propertiesValue, defaultValue);
122 
123         return ParamUtil.get(request, param, getterUtilValue);
124     }
125 
126     public static double getDouble(
127         Properties properties, PortletRequest portletRequest, String param) {
128 
129         return getDouble(
130             properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
131     }
132 
133     public static double getDouble(
134         Properties properties, PortletRequest portletRequest, String param,
135         double defaultValue) {
136 
137         String propertiesValue = properties.getProperty(param, null);
138 
139         double getterUtilValue = GetterUtil.getDouble(
140             propertiesValue, defaultValue);
141 
142         return ParamUtil.get(portletRequest, param, getterUtilValue);
143     }
144 
145     public static double getDouble(
146         UnicodeProperties properties, HttpServletRequest request,
147         String param) {
148 
149         return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
150     }
151 
152     public static double getDouble(
153         UnicodeProperties properties, HttpServletRequest request, String param,
154         double defaultValue) {
155 
156         String propertiesValue = properties.getProperty(param, null);
157 
158         double getterUtilValue = GetterUtil.getDouble(
159             propertiesValue, defaultValue);
160 
161         return ParamUtil.get(request, param, getterUtilValue);
162     }
163 
164     public static double getDouble(
165         UnicodeProperties properties, PortletRequest portletRequest,
166         String param) {
167 
168         return getDouble(
169             properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
170     }
171 
172     public static double getDouble(
173         UnicodeProperties properties, PortletRequest portletRequest,
174         String param, double defaultValue) {
175 
176         String propertiesValue = properties.getProperty(param, null);
177 
178         double getterUtilValue = GetterUtil.getDouble(
179             propertiesValue, defaultValue);
180 
181         return ParamUtil.get(portletRequest, param, getterUtilValue);
182     }
183 
184     public static int getInteger(
185         Properties properties, HttpServletRequest request, String param) {
186 
187         return getInteger(
188             properties, request, param, GetterUtil.DEFAULT_INTEGER);
189     }
190 
191     public static int getInteger(
192         Properties properties, HttpServletRequest request, String param,
193         int defaultValue) {
194 
195         String propertiesValue = properties.getProperty(param, null);
196 
197         int getterUtilValue = GetterUtil.getInteger(
198             propertiesValue, defaultValue);
199 
200         return ParamUtil.get(request, param, getterUtilValue);
201     }
202 
203     public static int getInteger(
204         Properties properties, PortletRequest portletRequest, String param) {
205 
206         return getInteger(
207             properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
208     }
209 
210     public static int getInteger(
211         Properties properties, PortletRequest portletRequest, String param,
212         int defaultValue) {
213 
214         String propertiesValue = properties.getProperty(param, null);
215 
216         int getterUtilValue = GetterUtil.getInteger(
217             propertiesValue, defaultValue);
218 
219         return ParamUtil.get(portletRequest, param, getterUtilValue);
220     }
221 
222     public static int getInteger(
223         UnicodeProperties properties, HttpServletRequest request,
224         String param) {
225 
226         return getInteger(
227             properties, request, param, GetterUtil.DEFAULT_INTEGER);
228     }
229 
230     public static int getInteger(
231         UnicodeProperties properties, HttpServletRequest request, String param,
232         int defaultValue) {
233 
234         String propertiesValue = properties.getProperty(param, null);
235 
236         int getterUtilValue = GetterUtil.getInteger(
237             propertiesValue, defaultValue);
238 
239         return ParamUtil.get(request, param, getterUtilValue);
240     }
241 
242     public static int getInteger(
243         UnicodeProperties properties, PortletRequest portletRequest,
244         String param) {
245 
246         return getInteger(
247             properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
248     }
249 
250     public static int getInteger(
251         UnicodeProperties properties, PortletRequest portletRequest,
252         String param, int defaultValue) {
253 
254         String propertiesValue = properties.getProperty(param, null);
255 
256         int getterUtilValue = GetterUtil.getInteger(
257             propertiesValue, defaultValue);
258 
259         return ParamUtil.get(portletRequest, param, getterUtilValue);
260     }
261 
262     public static long getLong(
263         Properties properties, HttpServletRequest request, String param) {
264 
265         return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
266     }
267 
268     public static long getLong(
269         Properties properties, HttpServletRequest request, String param,
270         long defaultValue) {
271 
272         String propertiesValue = properties.getProperty(param, null);
273 
274         long getterUtilValue = GetterUtil.getLong(
275             propertiesValue, defaultValue);
276 
277         return ParamUtil.get(request, param, getterUtilValue);
278     }
279 
280     public static long getLong(
281         Properties properties, PortletRequest portletRequest, String param) {
282 
283         return getLong(
284             properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
285     }
286 
287     public static long getLong(
288         Properties properties, PortletRequest portletRequest, String param,
289         long defaultValue) {
290 
291         String propertiesValue = properties.getProperty(param, null);
292 
293         long getterUtilValue = GetterUtil.getLong(
294             propertiesValue, defaultValue);
295 
296         return ParamUtil.get(portletRequest, param, getterUtilValue);
297     }
298 
299     public static long getLong(
300         UnicodeProperties properties, HttpServletRequest request,
301         String param) {
302 
303         return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
304     }
305 
306     public static long getLong(
307         UnicodeProperties properties, HttpServletRequest request, String param,
308         long defaultValue) {
309 
310         String propertiesValue = properties.getProperty(param, null);
311 
312         long getterUtilValue = GetterUtil.getLong(
313             propertiesValue, defaultValue);
314 
315         return ParamUtil.get(request, param, getterUtilValue);
316     }
317 
318     public static long getLong(
319         UnicodeProperties properties, PortletRequest portletRequest,
320         String param) {
321 
322         return getLong(
323             properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
324     }
325 
326     public static long getLong(
327         UnicodeProperties properties, PortletRequest portletRequest,
328         String param, long defaultValue) {
329 
330         String propertiesValue = properties.getProperty(param, null);
331 
332         long getterUtilValue = GetterUtil.getLong(
333             propertiesValue, defaultValue);
334 
335         return ParamUtil.get(portletRequest, param, getterUtilValue);
336     }
337 
338     public static UnicodeProperties getProperties(
339         PortletRequest portletRequest, String prefix) {
340 
341         UnicodeProperties properties = new UnicodeProperties(true);
342 
343         for (String param : portletRequest.getParameterMap().keySet()) {
344             if (param.startsWith(prefix) && !param.endsWith(")Checkbox")) {
345                 String key = param.substring(
346                     prefix.length(), param.length() - 1);
347                 String value = portletRequest.getParameter(param);
348 
349                 properties.setProperty(key, value);
350             }
351         }
352 
353         return properties;
354     }
355 
356     public static String getString(
357         Properties properties, HttpServletRequest request, String param) {
358 
359         return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
360     }
361 
362     public static String getString(
363         Properties properties, HttpServletRequest request, String param,
364         String defaultValue) {
365 
366         String propertiesValue = properties.getProperty(param, null);
367 
368         String getterUtilValue = GetterUtil.getString(
369             propertiesValue, defaultValue);
370 
371         return ParamUtil.get(request, param, getterUtilValue);
372     }
373 
374     public static String getString(
375         Properties properties, PortletRequest portletRequest, String param) {
376 
377         return getString(
378             properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
379     }
380 
381     public static String getString(
382         Properties properties, PortletRequest portletRequest, String param,
383         String defaultValue) {
384 
385         String propertiesValue = properties.getProperty(param, null);
386 
387         String getterUtilValue = GetterUtil.getString(
388             propertiesValue, defaultValue);
389 
390         return ParamUtil.get(portletRequest, param, getterUtilValue);
391     }
392 
393     public static String getString(
394         UnicodeProperties properties, HttpServletRequest request,
395         String param) {
396 
397         return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
398     }
399 
400     public static String getString(
401         UnicodeProperties properties, HttpServletRequest request, String param,
402         String defaultValue) {
403 
404         String propertiesValue = properties.getProperty(param, null);
405 
406         String getterUtilValue = GetterUtil.getString(
407             propertiesValue, defaultValue);
408 
409         return ParamUtil.get(request, param, getterUtilValue);
410     }
411 
412     public static String getString(
413         UnicodeProperties properties, PortletRequest portletRequest,
414         String param) {
415 
416         return getString(
417             properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
418     }
419 
420     public static String getString(
421         UnicodeProperties properties, PortletRequest portletRequest,
422         String param, String defaultValue) {
423 
424         String propertiesValue = properties.getProperty(param, null);
425 
426         String getterUtilValue = GetterUtil.getString(
427             propertiesValue, defaultValue);
428 
429         return ParamUtil.get(portletRequest, param, getterUtilValue);
430     }
431 
432 }