001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Map;
018    import java.util.Properties;
019    
020    import javax.portlet.PortletRequest;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class PropertiesParamUtil {
028    
029            public static boolean getBoolean(
030                    Properties properties, HttpServletRequest request, String param) {
031    
032                    return getBoolean(
033                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
034            }
035    
036            public static boolean getBoolean(
037                    Properties properties, HttpServletRequest request, String param,
038                    boolean defaultValue) {
039    
040                    String propertiesValue = properties.getProperty(param, null);
041    
042                    boolean getterUtilValue = GetterUtil.getBoolean(
043                            propertiesValue, defaultValue);
044    
045                    return ParamUtil.get(request, param, getterUtilValue);
046            }
047    
048            public static boolean getBoolean(
049                    Properties properties, PortletRequest portletRequest, String param) {
050    
051                    return getBoolean(
052                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
053            }
054    
055            public static boolean getBoolean(
056                    Properties properties, PortletRequest portletRequest, String param,
057                    boolean defaultValue) {
058    
059                    String propertiesValue = properties.getProperty(param, null);
060    
061                    boolean getterUtilValue = GetterUtil.getBoolean(
062                            propertiesValue, defaultValue);
063    
064                    return ParamUtil.get(portletRequest, param, getterUtilValue);
065            }
066    
067            public static boolean getBoolean(
068                    UnicodeProperties properties, HttpServletRequest request,
069                    String param) {
070    
071                    return getBoolean(
072                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
073            }
074    
075            public static boolean getBoolean(
076                    UnicodeProperties properties, HttpServletRequest request, String param,
077                    boolean defaultValue) {
078    
079                    String propertiesValue = properties.getProperty(param, null);
080    
081                    boolean getterUtilValue = GetterUtil.getBoolean(
082                            propertiesValue, defaultValue);
083    
084                    return ParamUtil.get(request, param, getterUtilValue);
085            }
086    
087            public static boolean getBoolean(
088                    UnicodeProperties properties, PortletRequest portletRequest,
089                    String param) {
090    
091                    return getBoolean(
092                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
093            }
094    
095            public static boolean getBoolean(
096                    UnicodeProperties properties, PortletRequest portletRequest,
097                    String param, boolean defaultValue) {
098    
099                    String propertiesValue = properties.getProperty(param, null);
100    
101                    boolean getterUtilValue = GetterUtil.getBoolean(
102                            propertiesValue, defaultValue);
103    
104                    return ParamUtil.get(portletRequest, param, getterUtilValue);
105            }
106    
107            public static double getDouble(
108                    Properties properties, HttpServletRequest request, String param) {
109    
110                    return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
111            }
112    
113            public static double getDouble(
114                    Properties properties, HttpServletRequest request, String param,
115                    double defaultValue) {
116    
117                    String propertiesValue = properties.getProperty(param, null);
118    
119                    double getterUtilValue = GetterUtil.getDouble(
120                            propertiesValue, defaultValue);
121    
122                    return ParamUtil.get(request, param, getterUtilValue);
123            }
124    
125            public static double getDouble(
126                    Properties properties, PortletRequest portletRequest, String param) {
127    
128                    return getDouble(
129                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
130            }
131    
132            public static double getDouble(
133                    Properties properties, PortletRequest portletRequest, String param,
134                    double defaultValue) {
135    
136                    String propertiesValue = properties.getProperty(param, null);
137    
138                    double getterUtilValue = GetterUtil.getDouble(
139                            propertiesValue, defaultValue);
140    
141                    return ParamUtil.get(portletRequest, param, getterUtilValue);
142            }
143    
144            public static double getDouble(
145                    UnicodeProperties properties, HttpServletRequest request,
146                    String param) {
147    
148                    return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
149            }
150    
151            public static double getDouble(
152                    UnicodeProperties properties, HttpServletRequest request, String param,
153                    double defaultValue) {
154    
155                    String propertiesValue = properties.getProperty(param, null);
156    
157                    double getterUtilValue = GetterUtil.getDouble(
158                            propertiesValue, defaultValue);
159    
160                    return ParamUtil.get(request, param, getterUtilValue);
161            }
162    
163            public static double getDouble(
164                    UnicodeProperties properties, PortletRequest portletRequest,
165                    String param) {
166    
167                    return getDouble(
168                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
169            }
170    
171            public static double getDouble(
172                    UnicodeProperties properties, PortletRequest portletRequest,
173                    String param, double defaultValue) {
174    
175                    String propertiesValue = properties.getProperty(param, null);
176    
177                    double getterUtilValue = GetterUtil.getDouble(
178                            propertiesValue, defaultValue);
179    
180                    return ParamUtil.get(portletRequest, param, getterUtilValue);
181            }
182    
183            public static int getInteger(
184                    Properties properties, HttpServletRequest request, String param) {
185    
186                    return getInteger(
187                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
188            }
189    
190            public static int getInteger(
191                    Properties properties, HttpServletRequest request, String param,
192                    int defaultValue) {
193    
194                    String propertiesValue = properties.getProperty(param, null);
195    
196                    int getterUtilValue = GetterUtil.getInteger(
197                            propertiesValue, defaultValue);
198    
199                    return ParamUtil.get(request, param, getterUtilValue);
200            }
201    
202            public static int getInteger(
203                    Properties properties, PortletRequest portletRequest, String param) {
204    
205                    return getInteger(
206                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
207            }
208    
209            public static int getInteger(
210                    Properties properties, PortletRequest portletRequest, String param,
211                    int defaultValue) {
212    
213                    String propertiesValue = properties.getProperty(param, null);
214    
215                    int getterUtilValue = GetterUtil.getInteger(
216                            propertiesValue, defaultValue);
217    
218                    return ParamUtil.get(portletRequest, param, getterUtilValue);
219            }
220    
221            public static int getInteger(
222                    UnicodeProperties properties, HttpServletRequest request,
223                    String param) {
224    
225                    return getInteger(
226                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
227            }
228    
229            public static int getInteger(
230                    UnicodeProperties properties, HttpServletRequest request, String param,
231                    int defaultValue) {
232    
233                    String propertiesValue = properties.getProperty(param, null);
234    
235                    int getterUtilValue = GetterUtil.getInteger(
236                            propertiesValue, defaultValue);
237    
238                    return ParamUtil.get(request, param, getterUtilValue);
239            }
240    
241            public static int getInteger(
242                    UnicodeProperties properties, PortletRequest portletRequest,
243                    String param) {
244    
245                    return getInteger(
246                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
247            }
248    
249            public static int getInteger(
250                    UnicodeProperties properties, PortletRequest portletRequest,
251                    String param, int defaultValue) {
252    
253                    String propertiesValue = properties.getProperty(param, null);
254    
255                    int getterUtilValue = GetterUtil.getInteger(
256                            propertiesValue, defaultValue);
257    
258                    return ParamUtil.get(portletRequest, param, getterUtilValue);
259            }
260    
261            public static long getLong(
262                    Properties properties, HttpServletRequest request, String param) {
263    
264                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
265            }
266    
267            public static long getLong(
268                    Properties properties, HttpServletRequest request, String param,
269                    long defaultValue) {
270    
271                    String propertiesValue = properties.getProperty(param, null);
272    
273                    long getterUtilValue = GetterUtil.getLong(
274                            propertiesValue, defaultValue);
275    
276                    return ParamUtil.get(request, param, getterUtilValue);
277            }
278    
279            public static long getLong(
280                    Properties properties, PortletRequest portletRequest, String param) {
281    
282                    return getLong(
283                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
284            }
285    
286            public static long getLong(
287                    Properties properties, PortletRequest portletRequest, String param,
288                    long defaultValue) {
289    
290                    String propertiesValue = properties.getProperty(param, null);
291    
292                    long getterUtilValue = GetterUtil.getLong(
293                            propertiesValue, defaultValue);
294    
295                    return ParamUtil.get(portletRequest, param, getterUtilValue);
296            }
297    
298            public static long getLong(
299                    UnicodeProperties properties, HttpServletRequest request,
300                    String param) {
301    
302                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
303            }
304    
305            public static long getLong(
306                    UnicodeProperties properties, HttpServletRequest request, String param,
307                    long defaultValue) {
308    
309                    String propertiesValue = properties.getProperty(param, null);
310    
311                    long getterUtilValue = GetterUtil.getLong(
312                            propertiesValue, defaultValue);
313    
314                    return ParamUtil.get(request, param, getterUtilValue);
315            }
316    
317            public static long getLong(
318                    UnicodeProperties properties, PortletRequest portletRequest,
319                    String param) {
320    
321                    return getLong(
322                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
323            }
324    
325            public static long getLong(
326                    UnicodeProperties properties, PortletRequest portletRequest,
327                    String param, long defaultValue) {
328    
329                    String propertiesValue = properties.getProperty(param, null);
330    
331                    long getterUtilValue = GetterUtil.getLong(
332                            propertiesValue, defaultValue);
333    
334                    return ParamUtil.get(portletRequest, param, getterUtilValue);
335            }
336    
337            public static UnicodeProperties getProperties(
338                    HttpServletRequest request, String prefix) {
339    
340                    UnicodeProperties properties = new UnicodeProperties(true);
341    
342                    Map<String, String[]> parameterMap = request.getParameterMap();
343    
344                    for (String param : parameterMap.keySet()) {
345                            if (param.startsWith(prefix) && !param.endsWith("--Checkbox")) {
346                                    String key = param.substring(
347                                            prefix.length(), param.length() - 2);
348                                    String value = request.getParameter(param);
349    
350                                    properties.setProperty(key, value);
351                            }
352                    }
353    
354                    return properties;
355            }
356    
357            public static UnicodeProperties getProperties(
358                    PortletRequest portletRequest, String prefix) {
359    
360                    UnicodeProperties properties = new UnicodeProperties(true);
361    
362                    for (String param : portletRequest.getParameterMap().keySet()) {
363                            if (param.startsWith(prefix) && !param.endsWith("--Checkbox")) {
364                                    String key = param.substring(
365                                            prefix.length(), param.length() - 2);
366                                    String value = portletRequest.getParameter(param);
367    
368                                    properties.setProperty(key, value);
369                            }
370                    }
371    
372                    return properties;
373            }
374    
375            public static String getString(
376                    Properties properties, HttpServletRequest request, String param) {
377    
378                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
379            }
380    
381            public static String getString(
382                    Properties properties, HttpServletRequest request, 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(request, param, getterUtilValue);
391            }
392    
393            public static String getString(
394                    Properties properties, PortletRequest portletRequest, String param) {
395    
396                    return getString(
397                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
398            }
399    
400            public static String getString(
401                    Properties properties, PortletRequest portletRequest, 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(portletRequest, param, getterUtilValue);
410            }
411    
412            public static String getString(
413                    UnicodeProperties properties, HttpServletRequest request,
414                    String param) {
415    
416                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
417            }
418    
419            public static String getString(
420                    UnicodeProperties properties, HttpServletRequest request, String param,
421                    String defaultValue) {
422    
423                    String propertiesValue = properties.getProperty(param, null);
424    
425                    String getterUtilValue = GetterUtil.getString(
426                            propertiesValue, defaultValue);
427    
428                    return ParamUtil.get(request, param, getterUtilValue);
429            }
430    
431            public static String getString(
432                    UnicodeProperties properties, PortletRequest portletRequest,
433                    String param) {
434    
435                    return getString(
436                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
437            }
438    
439            public static String getString(
440                    UnicodeProperties properties, PortletRequest portletRequest,
441                    String param, String defaultValue) {
442    
443                    String propertiesValue = properties.getProperty(param, null);
444    
445                    String getterUtilValue = GetterUtil.getString(
446                            propertiesValue, defaultValue);
447    
448                    return ParamUtil.get(portletRequest, param, getterUtilValue);
449            }
450    
451    }