KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > util > GenericParamUtil


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/GenericParamUtil.java,v 1.6.2.1 2006/07/10 12:55:29 phuongpdd Exp $
3  * $Author: phuongpdd $
4  * $Revision: 1.6.2.1 $
5  * $Date: 2006/07/10 12:55:29 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  * @author: Mai Nguyen
33  */

34 package net.myvietnam.mvncore.util;
35
36 import java.text.DateFormat JavaDoc;
37 import java.text.SimpleDateFormat JavaDoc;
38 import java.util.Locale JavaDoc;
39
40 import javax.servlet.http.HttpSession JavaDoc;
41
42 import net.myvietnam.mvncore.MVNCoreResourceBundle;
43 import net.myvietnam.mvncore.exception.BadInputException;
44 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
45 import net.myvietnam.mvncore.web.GenericRequest;
46
47 public final class GenericParamUtil {
48
49     private GenericParamUtil() { // prevent instantiation
50
}
51
52     //private static String contextPath = (new ParamOptions()).contextPath;
53
//private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block
54

55     private static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc ("dd/MM/yyyy");
56
57     public static String JavaDoc getParameter(GenericRequest request, String JavaDoc param) {
58
59         String JavaDoc ret = request.getParameter(param);
60         if (ret == null) ret = "";
61         return ret.trim();
62     }
63
64     public static String JavaDoc getParameterFilter(GenericRequest request, String JavaDoc param) {
65         return DisableHtmlTagFilter.filter(getParameter(request, param));
66     }
67     
68     public static String JavaDoc getParameter(GenericRequest request, String JavaDoc param, boolean checkEmpty)
69         throws BadInputException {
70
71         String JavaDoc ret = request.getParameter(param);
72         if (ret == null) ret = "";
73         ret = ret.trim();
74         if ( checkEmpty && (ret.length() == 0) ) {
75             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
76             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
77             throw new BadInputException(localizedMessage);
78         }
79         return ret;
80     }
81
82     public static String JavaDoc getParameterFilter(GenericRequest request, String JavaDoc param, boolean checkEmpty)
83         throws BadInputException {
84         return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty));
85     }
86
87     /** @todo review this method */
88     public static String JavaDoc getParameterSafe(GenericRequest request, String JavaDoc param, boolean checkEmpty)
89         throws BadInputException {
90
91         String JavaDoc ret = getParameter(request, param, checkEmpty);
92         if ( (ret.indexOf('<') != -1) ||
93              (ret.indexOf('>') != -1)) {
94             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
95             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
96             throw new BadInputException(localizedMessage);
97         }
98         return ret;
99     }
100
101     public static int getParameterInt(GenericRequest request, String JavaDoc param)
102         throws BadInputException {
103
104         String JavaDoc inputStr = getParameter(request, param, true);
105         int ret;
106         try {
107             ret = Integer.parseInt(inputStr);
108         } catch (NumberFormatException JavaDoc e) {
109             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
110             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "int"});
111             throw new BadInputException(localizedMessage);
112         }
113         return ret;
114     }
115
116     public static int getParameterUnsignedInt(GenericRequest request, String JavaDoc param)
117         throws BadInputException {
118
119         int retValue = getParameterInt(request, param);
120         if (retValue < 0) {
121             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
122             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
123             throw new BadInputException(localizedMessage);
124         }
125         return retValue;
126     }
127
128     public static int getParameterInt(GenericRequest request, String JavaDoc param, int defaultValue)
129         throws BadInputException {
130
131         String JavaDoc inputStr = getParameter(request, param, false);
132         if (inputStr.length() == 0) {
133             return defaultValue;
134         }
135         int ret;
136         try {
137             ret = Integer.parseInt(inputStr);
138         } catch (NumberFormatException JavaDoc e) {
139             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
140             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "int"});
141             throw new BadInputException(localizedMessage);
142         }
143         return ret;
144     }
145
146     public static int getParameterUnsignedInt(GenericRequest request, String JavaDoc param, int defaultValue)
147         throws BadInputException {
148
149         int retValue = getParameterInt(request, param, defaultValue);
150         if (retValue < 0) {
151             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
152             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
153             throw new BadInputException(localizedMessage);
154         }
155         return retValue;
156     }
157
158     public static long getParameterLong(GenericRequest request, String JavaDoc param)
159         throws BadInputException {
160
161         String JavaDoc inputStr = getParameter(request, param, true);
162         long ret;
163         try {
164             ret = Long.parseLong(inputStr);
165         } catch (NumberFormatException JavaDoc e) {
166             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
167             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "long"});
168             throw new BadInputException(localizedMessage);
169         }
170         return ret;
171     }
172
173     public static long getParameterLong(GenericRequest request, String JavaDoc param, long defaultValue)
174         throws BadInputException {
175
176         String JavaDoc inputStr = getParameter(request, param, false);
177         if (inputStr.length() == 0) {
178             return defaultValue;
179         }
180
181         long ret;
182         try {
183             ret = Long.parseLong(inputStr);
184         } catch (NumberFormatException JavaDoc e) {
185             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
186             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "long"});
187             throw new BadInputException(localizedMessage);
188         }
189         return ret;
190     }
191
192     /**
193      * @param param is the name of variable
194      * @return true if the value of param is not empty
195      */

196     public static boolean getParameterBoolean(GenericRequest request, String JavaDoc param) {
197
198         String JavaDoc inputStr = getParameter(request, param);
199         if (inputStr.length() == 0) return false;
200         return true;
201     }
202
203     public static byte getParameterByte(GenericRequest request, String JavaDoc param)
204         throws BadInputException {
205
206         String JavaDoc inputStr = getParameter(request, param, true);
207         byte ret;
208         try {
209             ret = Byte.parseByte(inputStr);
210         } catch (NumberFormatException JavaDoc e) {
211             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
212             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "byte"});
213             throw new BadInputException(localizedMessage);
214         }
215         return ret;
216     }
217
218     public static double getParameterDouble(GenericRequest request, String JavaDoc param)
219         throws BadInputException {
220
221         String JavaDoc inputStr = getParameter(request, param, true);
222         double ret;
223         try {
224             ret = Double.parseDouble(inputStr);
225         } catch (NumberFormatException JavaDoc e) {
226             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
227             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "double"});
228             throw new BadInputException(localizedMessage);
229         }
230         return ret;
231     }
232
233     public static String JavaDoc getParameterUrl(GenericRequest request, String JavaDoc param)
234         throws BadInputException {
235
236         String JavaDoc ret = getParameter(request, param);
237         if ( ret.length() > 0 ) {
238             if ( !ret.startsWith("http://") &&
239                  !ret.startsWith("https://") &&
240                  !ret.startsWith("ftp://") ) {
241                 Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
242                 String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_url", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
243                 throw new BadInputException(localizedMessage);
244             }
245         }
246         return ret;
247     }
248
249     public static String JavaDoc getParameterPassword(GenericRequest request, String JavaDoc param, int minLength, int option)
250         throws BadInputException {
251
252         if (minLength < 1) minLength = 1;
253
254         String JavaDoc ret = request.getParameter(param);
255         if (ret == null) ret = "";
256         ret = ret.trim();
257
258         if ( ret.length() < minLength ) {
259             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
260             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.password_too_short", new Object JavaDoc[] {new Integer JavaDoc(minLength)});
261             throw new BadInputException(localizedMessage);
262         }
263
264         /** @todo implement this feature */
265         if (option == 1) {//char and number
266

267         } else if (option == 2) {// lower char, upper char and number
268

269         }
270         return ret;
271     }
272
273     public static String JavaDoc getParameterEmail(GenericRequest request, String JavaDoc param)
274         throws BadInputException {
275
276         String JavaDoc email = getParameterSafe(request, param, true);
277         MailUtil.checkGoodEmail(email);
278         return email;
279     }
280
281     /**
282      *
283      */

284     public static java.sql.Date JavaDoc getParameterDate(GenericRequest request, String JavaDoc param)
285         throws BadInputException {
286
287         String JavaDoc inputStr = getParameter(request, param, true);
288         java.util.Date JavaDoc ret;
289         try {
290             ret = dateFormat.parse(inputStr);
291         } catch (java.text.ParseException JavaDoc e) {
292             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
293             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(param), "Date"});
294             throw new BadInputException(localizedMessage);
295         }
296         return new java.sql.Date JavaDoc(ret.getTime());
297     }
298
299     /**
300      *
301      */

302     public static java.util.Date JavaDoc getParameterDateUtil(GenericRequest request, String JavaDoc param)
303         throws BadInputException {
304
305         String JavaDoc inputStr = getParameter(request, param, true);
306         java.util.Date JavaDoc ret;
307         try {
308             ret = dateFormat.parse(inputStr);
309         } catch (java.text.ParseException JavaDoc e) {
310             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
311             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(param), "Date"});
312             throw new BadInputException(localizedMessage);
313         }
314         return ret;
315     }
316
317     /**
318      *
319      */

320     public static java.sql.Date JavaDoc getParameterDate(GenericRequest request, String JavaDoc paramDay, String JavaDoc paramMonth, String JavaDoc paramYear)
321         throws BadInputException {
322
323         int day = getParameterInt(request, paramDay);
324         int month = getParameterInt(request, paramMonth);
325         int year = getParameterInt(request, paramYear);
326         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
327         buffer.append(day).append("/").append(month).append("/").append(year);
328         String JavaDoc inputStr = buffer.toString();
329
330         java.util.Date JavaDoc ret;
331         try {
332             ret = dateFormat.parse(inputStr);
333         } catch (java.text.ParseException JavaDoc e) {
334             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
335             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(inputStr), "Date"});
336             throw new BadInputException(localizedMessage);
337         }
338         return new java.sql.Date JavaDoc(ret.getTime());
339     }
340
341     public static double getParameterTimeZone(GenericRequest request, String JavaDoc param)
342         throws BadInputException {
343
344         double timeZone = getParameterDouble(request, param);
345         if (timeZone < -12 || timeZone > 13) {
346             timeZone = 0;
347         }
348         return timeZone;
349     }
350
351     public static String JavaDoc getAttribute(HttpSession JavaDoc session, String JavaDoc name) {
352
353         String JavaDoc ret = (String JavaDoc)session.getAttribute(name);
354         if (ret == null) ret = "";
355         return ret.trim();
356     }
357
358     /**
359      * Note: sometime in the dispatched request, we dont receive HttpServletRequest
360      * but receive ServletRequest, such as com.caucho.server.webapp.DispatchRequest
361      *
362      * @param request ServletRequest
363      * @param name String
364      * @return String
365      */

366     public static String JavaDoc getAttribute(GenericRequest request, String JavaDoc name) {
367
368         String JavaDoc ret = (String JavaDoc)request.getAttribute(name);
369         if (ret == null) ret = "";
370         return ret.trim();
371     }
372
373 }
374
Popular Tags