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