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