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.Locale;
018    import java.util.Map;
019    import java.util.Properties;
020    
021    import javax.portlet.PortletRequest;
022    
023    import javax.servlet.http.HttpServletRequest;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PropertiesParamUtil {
029    
030            public static boolean getBoolean(
031                    Properties properties, HttpServletRequest request, String param) {
032    
033                    return getBoolean(
034                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
035            }
036    
037            public static boolean getBoolean(
038                    Properties properties, HttpServletRequest request, String param,
039                    boolean defaultValue) {
040    
041                    String propertiesValue = properties.getProperty(param, null);
042    
043                    boolean getterUtilValue = GetterUtil.getBoolean(
044                            propertiesValue, defaultValue);
045    
046                    return ParamUtil.get(request, param, getterUtilValue);
047            }
048    
049            public static boolean getBoolean(
050                    Properties properties, PortletRequest portletRequest, String param) {
051    
052                    return getBoolean(
053                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
054            }
055    
056            public static boolean getBoolean(
057                    Properties properties, PortletRequest portletRequest, String param,
058                    boolean defaultValue) {
059    
060                    String propertiesValue = properties.getProperty(param, null);
061    
062                    boolean getterUtilValue = GetterUtil.getBoolean(
063                            propertiesValue, defaultValue);
064    
065                    return ParamUtil.get(portletRequest, param, getterUtilValue);
066            }
067    
068            public static boolean getBoolean(
069                    UnicodeProperties properties, HttpServletRequest request,
070                    String param) {
071    
072                    return getBoolean(
073                            properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
074            }
075    
076            public static boolean getBoolean(
077                    UnicodeProperties properties, HttpServletRequest request, String param,
078                    boolean defaultValue) {
079    
080                    String propertiesValue = properties.getProperty(param, null);
081    
082                    boolean getterUtilValue = GetterUtil.getBoolean(
083                            propertiesValue, defaultValue);
084    
085                    return ParamUtil.get(request, param, getterUtilValue);
086            }
087    
088            public static boolean getBoolean(
089                    UnicodeProperties properties, PortletRequest portletRequest,
090                    String param) {
091    
092                    return getBoolean(
093                            properties, portletRequest, param, GetterUtil.DEFAULT_BOOLEAN);
094            }
095    
096            public static boolean getBoolean(
097                    UnicodeProperties properties, PortletRequest portletRequest,
098                    String param, boolean defaultValue) {
099    
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, HttpServletRequest request, String param,
128                    double defaultValue, Locale locale) {
129    
130                    String propertiesValue = properties.getProperty(param, null);
131    
132                    double getterUtilValue = GetterUtil.getDouble(
133                            propertiesValue, defaultValue);
134    
135                    return ParamUtil.getDouble(request, param, getterUtilValue, locale);
136            }
137    
138            public static double getDouble(
139                    Properties properties, HttpServletRequest request, String param,
140                    Locale locale) {
141    
142                    return getDouble(
143                            properties, request, param, GetterUtil.DEFAULT_DOUBLE, locale);
144            }
145    
146            public static double getDouble(
147                    Properties properties, PortletRequest portletRequest, String param) {
148    
149                    return getDouble(
150                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
151            }
152    
153            public static double getDouble(
154                    Properties properties, PortletRequest portletRequest, String param,
155                    double defaultValue) {
156    
157                    String propertiesValue = properties.getProperty(param, null);
158    
159                    double getterUtilValue = GetterUtil.getDouble(
160                            propertiesValue, defaultValue);
161    
162                    return ParamUtil.get(portletRequest, param, getterUtilValue);
163            }
164    
165            public static double getDouble(
166                    Properties properties, PortletRequest portletRequest, String param,
167                    double defaultValue, Locale locale) {
168    
169                    String propertiesValue = properties.getProperty(param, null);
170    
171                    double getterUtilValue = GetterUtil.getDouble(
172                            propertiesValue, defaultValue);
173    
174                    return ParamUtil.getDouble(
175                            portletRequest, param, getterUtilValue, locale);
176            }
177    
178            public static double getDouble(
179                    Properties properties, PortletRequest portletRequest, String param,
180                    Locale locale) {
181    
182                    return getDouble(
183                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE,
184                            locale);
185            }
186    
187            public static double getDouble(
188                    UnicodeProperties properties, HttpServletRequest request,
189                    String param) {
190    
191                    return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
192            }
193    
194            public static double getDouble(
195                    UnicodeProperties properties, HttpServletRequest request, String param,
196                    double defaultValue) {
197    
198                    String propertiesValue = properties.getProperty(param, null);
199    
200                    double getterUtilValue = GetterUtil.getDouble(
201                            propertiesValue, defaultValue);
202    
203                    return ParamUtil.get(request, param, getterUtilValue);
204            }
205    
206            public static double getDouble(
207                    UnicodeProperties properties, HttpServletRequest request, String param,
208                    double defaultValue, Locale locale) {
209    
210                    String propertiesValue = properties.getProperty(param, null);
211    
212                    double getterUtilValue = GetterUtil.getDouble(
213                            propertiesValue, defaultValue);
214    
215                    return ParamUtil.getDouble(request, param, getterUtilValue, locale);
216            }
217    
218            public static double getDouble(
219                    UnicodeProperties properties, HttpServletRequest request, String param,
220                    Locale locale) {
221    
222                    return getDouble(
223                            properties, request, param, GetterUtil.DEFAULT_DOUBLE, locale);
224            }
225    
226            public static double getDouble(
227                    UnicodeProperties properties, PortletRequest portletRequest,
228                    String param) {
229    
230                    return getDouble(
231                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE);
232            }
233    
234            public static double getDouble(
235                    UnicodeProperties properties, PortletRequest portletRequest,
236                    String param, double defaultValue) {
237    
238                    String propertiesValue = properties.getProperty(param, null);
239    
240                    double getterUtilValue = GetterUtil.getDouble(
241                            propertiesValue, defaultValue);
242    
243                    return ParamUtil.get(portletRequest, param, getterUtilValue);
244            }
245    
246            public static double getDouble(
247                    UnicodeProperties properties, PortletRequest portletRequest,
248                    String param, double defaultValue, Locale locale) {
249    
250                    String propertiesValue = properties.getProperty(param, null);
251    
252                    double getterUtilValue = GetterUtil.getDouble(
253                            propertiesValue, defaultValue);
254    
255                    return ParamUtil.getDouble(
256                            portletRequest, param, getterUtilValue, locale);
257            }
258    
259            public static double getDouble(
260                    UnicodeProperties properties, PortletRequest portletRequest,
261                    String param, Locale locale) {
262    
263                    return getDouble(
264                            properties, portletRequest, param, GetterUtil.DEFAULT_DOUBLE,
265                            locale);
266            }
267    
268            public static int getInteger(
269                    Properties properties, HttpServletRequest request, String param) {
270    
271                    return getInteger(
272                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
273            }
274    
275            public static int getInteger(
276                    Properties properties, HttpServletRequest request, String param,
277                    int defaultValue) {
278    
279                    String propertiesValue = properties.getProperty(param, null);
280    
281                    int getterUtilValue = GetterUtil.getInteger(
282                            propertiesValue, defaultValue);
283    
284                    return ParamUtil.get(request, param, getterUtilValue);
285            }
286    
287            public static int getInteger(
288                    Properties properties, PortletRequest portletRequest, String param) {
289    
290                    return getInteger(
291                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
292            }
293    
294            public static int getInteger(
295                    Properties properties, PortletRequest portletRequest, String param,
296                    int defaultValue) {
297    
298                    String propertiesValue = properties.getProperty(param, null);
299    
300                    int getterUtilValue = GetterUtil.getInteger(
301                            propertiesValue, defaultValue);
302    
303                    return ParamUtil.get(portletRequest, param, getterUtilValue);
304            }
305    
306            public static int getInteger(
307                    UnicodeProperties properties, HttpServletRequest request,
308                    String param) {
309    
310                    return getInteger(
311                            properties, request, param, GetterUtil.DEFAULT_INTEGER);
312            }
313    
314            public static int getInteger(
315                    UnicodeProperties properties, HttpServletRequest request, String param,
316                    int defaultValue) {
317    
318                    String propertiesValue = properties.getProperty(param, null);
319    
320                    int getterUtilValue = GetterUtil.getInteger(
321                            propertiesValue, defaultValue);
322    
323                    return ParamUtil.get(request, param, getterUtilValue);
324            }
325    
326            public static int getInteger(
327                    UnicodeProperties properties, PortletRequest portletRequest,
328                    String param) {
329    
330                    return getInteger(
331                            properties, portletRequest, param, GetterUtil.DEFAULT_INTEGER);
332            }
333    
334            public static int getInteger(
335                    UnicodeProperties properties, PortletRequest portletRequest,
336                    String param, int defaultValue) {
337    
338                    String propertiesValue = properties.getProperty(param, null);
339    
340                    int getterUtilValue = GetterUtil.getInteger(
341                            propertiesValue, defaultValue);
342    
343                    return ParamUtil.get(portletRequest, param, getterUtilValue);
344            }
345    
346            public static long getLong(
347                    Properties properties, HttpServletRequest request, String param) {
348    
349                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
350            }
351    
352            public static long getLong(
353                    Properties properties, HttpServletRequest request, String param,
354                    long defaultValue) {
355    
356                    String propertiesValue = properties.getProperty(param, null);
357    
358                    long getterUtilValue = GetterUtil.getLong(
359                            propertiesValue, defaultValue);
360    
361                    return ParamUtil.get(request, param, getterUtilValue);
362            }
363    
364            public static long getLong(
365                    Properties properties, PortletRequest portletRequest, String param) {
366    
367                    return getLong(
368                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
369            }
370    
371            public static long getLong(
372                    Properties properties, PortletRequest portletRequest, String param,
373                    long defaultValue) {
374    
375                    String propertiesValue = properties.getProperty(param, null);
376    
377                    long getterUtilValue = GetterUtil.getLong(
378                            propertiesValue, defaultValue);
379    
380                    return ParamUtil.get(portletRequest, param, getterUtilValue);
381            }
382    
383            public static long getLong(
384                    UnicodeProperties properties, HttpServletRequest request,
385                    String param) {
386    
387                    return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
388            }
389    
390            public static long getLong(
391                    UnicodeProperties properties, HttpServletRequest request, String param,
392                    long defaultValue) {
393    
394                    String propertiesValue = properties.getProperty(param, null);
395    
396                    long getterUtilValue = GetterUtil.getLong(
397                            propertiesValue, defaultValue);
398    
399                    return ParamUtil.get(request, param, getterUtilValue);
400            }
401    
402            public static long getLong(
403                    UnicodeProperties properties, PortletRequest portletRequest,
404                    String param) {
405    
406                    return getLong(
407                            properties, portletRequest, param, GetterUtil.DEFAULT_LONG);
408            }
409    
410            public static long getLong(
411                    UnicodeProperties properties, PortletRequest portletRequest,
412                    String param, long defaultValue) {
413    
414                    String propertiesValue = properties.getProperty(param, null);
415    
416                    long getterUtilValue = GetterUtil.getLong(
417                            propertiesValue, defaultValue);
418    
419                    return ParamUtil.get(portletRequest, param, getterUtilValue);
420            }
421    
422            public static UnicodeProperties getProperties(
423                    HttpServletRequest request, String prefix) {
424    
425                    UnicodeProperties properties = new UnicodeProperties(true);
426    
427                    Map<String, String[]> parameterMap = request.getParameterMap();
428    
429                    for (String param : parameterMap.keySet()) {
430                            if (param.startsWith(prefix) && !param.endsWith("--Checkbox")) {
431                                    String key = param.substring(
432                                            prefix.length(), param.length() - 2);
433    
434                                    String value = request.getParameter(param);
435    
436                                    properties.setProperty(key, value);
437                            }
438                    }
439    
440                    return properties;
441            }
442    
443            public static UnicodeProperties getProperties(
444                    PortletRequest portletRequest, String prefix) {
445    
446                    UnicodeProperties properties = new UnicodeProperties(true);
447    
448                    for (String param : portletRequest.getParameterMap().keySet()) {
449                            if (param.startsWith(prefix) && !param.endsWith("--Checkbox")) {
450                                    String key = param.substring(
451                                            prefix.length(), param.length() - 2);
452    
453                                    String[] values = portletRequest.getParameterValues(param);
454    
455                                    String value = StringUtil.merge(values);
456    
457                                    properties.setProperty(key, value);
458                            }
459                    }
460    
461                    return properties;
462            }
463    
464            public static String getString(
465                    Properties properties, HttpServletRequest request, String param) {
466    
467                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
468            }
469    
470            public static String getString(
471                    Properties properties, HttpServletRequest request, String param,
472                    String defaultValue) {
473    
474                    String propertiesValue = properties.getProperty(param, null);
475    
476                    String getterUtilValue = GetterUtil.getString(
477                            propertiesValue, defaultValue);
478    
479                    return ParamUtil.get(request, param, getterUtilValue);
480            }
481    
482            public static String getString(
483                    Properties properties, PortletRequest portletRequest, String param) {
484    
485                    return getString(
486                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
487            }
488    
489            public static String getString(
490                    Properties properties, PortletRequest portletRequest, String param,
491                    String defaultValue) {
492    
493                    String propertiesValue = properties.getProperty(param, null);
494    
495                    String getterUtilValue = GetterUtil.getString(
496                            propertiesValue, defaultValue);
497    
498                    return ParamUtil.get(portletRequest, param, getterUtilValue);
499            }
500    
501            public static String getString(
502                    UnicodeProperties properties, HttpServletRequest request,
503                    String param) {
504    
505                    return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
506            }
507    
508            public static String getString(
509                    UnicodeProperties properties, HttpServletRequest request, String param,
510                    String defaultValue) {
511    
512                    String propertiesValue = properties.getProperty(param, null);
513    
514                    String getterUtilValue = GetterUtil.getString(
515                            propertiesValue, defaultValue);
516    
517                    return ParamUtil.get(request, param, getterUtilValue);
518            }
519    
520            public static String getString(
521                    UnicodeProperties properties, PortletRequest portletRequest,
522                    String param) {
523    
524                    return getString(
525                            properties, portletRequest, param, GetterUtil.DEFAULT_STRING);
526            }
527    
528            public static String getString(
529                    UnicodeProperties properties, PortletRequest portletRequest,
530                    String param, String defaultValue) {
531    
532                    String propertiesValue = properties.getProperty(param, null);
533    
534                    String getterUtilValue = GetterUtil.getString(
535                            propertiesValue, defaultValue);
536    
537                    return ParamUtil.get(portletRequest, param, getterUtilValue);
538            }
539    
540    }