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