1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
37  public class PropertiesParamUtil {
38  
39      public static boolean getBoolean(
40          Properties properties, HttpServletRequest request, String param) {
41  
42          return getBoolean(
43              properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
44      }
45  
46      public static boolean getBoolean(
47          Properties properties, HttpServletRequest request, String param,
48          boolean defaultValue) {
49  
50          String propertiesValue = properties.getProperty(param, null);
51  
52          boolean getterUtilValue = GetterUtil.getBoolean(
53              propertiesValue, defaultValue);
54  
55          return ParamUtil.get(request, param, getterUtilValue);
56      }
57  
58      public static boolean getBoolean(
59          UnicodeProperties properties, HttpServletRequest request,
60          String param) {
61  
62          return getBoolean(
63              properties, request, param, GetterUtil.DEFAULT_BOOLEAN);
64      }
65  
66      public static boolean getBoolean(
67          UnicodeProperties properties, HttpServletRequest request, String param,
68          boolean defaultValue) {
69  
70          String propertiesValue = properties.getProperty(param, null);
71  
72          boolean getterUtilValue = GetterUtil.getBoolean(
73              propertiesValue, defaultValue);
74  
75          return ParamUtil.get(request, param, getterUtilValue);
76      }
77  
78      public static double getDouble(
79          Properties properties, HttpServletRequest request, String param) {
80  
81          return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
82      }
83  
84      public static double getDouble(
85          Properties properties, HttpServletRequest request, String param,
86          double defaultValue) {
87  
88          String propertiesValue = properties.getProperty(param, null);
89  
90          double getterUtilValue = GetterUtil.getDouble(
91              propertiesValue, defaultValue);
92  
93          return ParamUtil.get(request, param, getterUtilValue);
94      }
95  
96      public static double getDouble(
97          UnicodeProperties properties, HttpServletRequest request,
98          String param) {
99  
100         return getDouble(properties, request, param, GetterUtil.DEFAULT_DOUBLE);
101     }
102 
103     public static double getDouble(
104         UnicodeProperties properties, HttpServletRequest request, String param,
105         double defaultValue) {
106 
107         String propertiesValue = properties.getProperty(param, null);
108 
109         double getterUtilValue = GetterUtil.getDouble(
110             propertiesValue, defaultValue);
111 
112         return ParamUtil.get(request, param, getterUtilValue);
113     }
114 
115     public static int getInteger(
116         Properties properties, HttpServletRequest request, String param) {
117 
118         return getInteger(
119             properties, request, param, GetterUtil.DEFAULT_INTEGER);
120     }
121 
122     public static int getInteger(
123         Properties properties, HttpServletRequest request, String param,
124         int defaultValue) {
125 
126         String propertiesValue = properties.getProperty(param, null);
127 
128         int getterUtilValue = GetterUtil.getInteger(
129             propertiesValue, defaultValue);
130 
131         return ParamUtil.get(request, param, getterUtilValue);
132     }
133 
134     public static int getInteger(
135         UnicodeProperties properties, HttpServletRequest request,
136         String param) {
137 
138         return getInteger(
139             properties, request, param, GetterUtil.DEFAULT_INTEGER);
140     }
141 
142     public static int getInteger(
143         UnicodeProperties properties, HttpServletRequest request, String param,
144         int defaultValue) {
145 
146         String propertiesValue = properties.getProperty(param, null);
147 
148         int getterUtilValue = GetterUtil.getInteger(
149             propertiesValue, defaultValue);
150 
151         return ParamUtil.get(request, param, getterUtilValue);
152     }
153 
154     public static long getLong(
155         Properties properties, HttpServletRequest request, String param) {
156 
157         return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
158     }
159 
160     public static long getLong(
161         Properties properties, HttpServletRequest request, String param,
162         long defaultValue) {
163 
164         String propertiesValue = properties.getProperty(param, null);
165 
166         long getterUtilValue = GetterUtil.getLong(
167             propertiesValue, defaultValue);
168 
169         return ParamUtil.get(request, param, getterUtilValue);
170     }
171 
172     public static long getLong(
173         UnicodeProperties properties, HttpServletRequest request,
174         String param) {
175 
176         return getLong(properties, request, param, GetterUtil.DEFAULT_LONG);
177     }
178 
179     public static long getLong(
180         UnicodeProperties properties, HttpServletRequest request, String param,
181         long defaultValue) {
182 
183         String propertiesValue = properties.getProperty(param, null);
184 
185         long getterUtilValue = GetterUtil.getLong(
186             propertiesValue, defaultValue);
187 
188         return ParamUtil.get(request, param, getterUtilValue);
189     }
190 
191     public static UnicodeProperties getProperties(
192         PortletRequest portletRequest, String prefix) {
193 
194         UnicodeProperties properties = new UnicodeProperties(true);
195 
196         for (String param : portletRequest.getParameterMap().keySet()) {
197             if (param.startsWith(prefix) && !param.endsWith(")Checkbox")) {
198                 String key = param.substring(
199                     prefix.length(), param.length() - 1);
200                 String value = portletRequest.getParameter(param);
201 
202                 properties.setProperty(key, value);
203             }
204         }
205 
206         return properties;
207     }
208 
209     public static String getString(
210         Properties properties, HttpServletRequest request, String param) {
211 
212         return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
213     }
214 
215     public static String getString(
216         Properties properties, HttpServletRequest request, String param,
217         String defaultValue) {
218 
219         String propertiesValue = properties.getProperty(param, null);
220 
221         String getterUtilValue = GetterUtil.getString(
222             propertiesValue, defaultValue);
223 
224         return ParamUtil.get(request, param, getterUtilValue);
225     }
226 
227     public static String getString(
228         UnicodeProperties properties, HttpServletRequest request,
229         String param) {
230 
231         return getString(properties, request, param, GetterUtil.DEFAULT_STRING);
232     }
233 
234     public static String getString(
235         UnicodeProperties properties, HttpServletRequest request, String param,
236         String defaultValue) {
237 
238         String propertiesValue = properties.getProperty(param, null);
239 
240         String getterUtilValue = GetterUtil.getString(
241             propertiesValue, defaultValue);
242 
243         return ParamUtil.get(request, param, getterUtilValue);
244     }
245 
246 }