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