KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/ParamUtil.java,v 1.25.2.1 2006/07/10 12:55:30 phuongpdd Exp $
3  * $Author: phuongpdd $
4  * $Revision: 1.25.2.1 $
5  * $Date: 2006/07/10 12:55:30 $
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.ServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43
44 import net.myvietnam.mvncore.MVNCoreConfig;
45 import net.myvietnam.mvncore.MVNCoreResourceBundle;
46 import net.myvietnam.mvncore.exception.BadInputException;
47 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
48
49 public final class ParamUtil {
50
51     private ParamUtil() { // prevent instantiation
52
}
53
54     //private static String contextPath = (new ParamOptions()).contextPath;
55
//private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block
56

57     private static DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc ("dd/MM/yyyy");
58
59     public static String JavaDoc getContextPath() {
60         return MVNCoreConfig.getContextPath();
61     }
62
63     public static String JavaDoc getServerPath() {
64         return MVNCoreConfig.getServerPath();
65     }
66
67     public static String JavaDoc getServer(HttpServletRequest JavaDoc request) {
68
69         StringBuffer JavaDoc server = new StringBuffer JavaDoc(128);
70         String JavaDoc scheme = request.getScheme();
71         int port = request.getServerPort();
72         if (port < 0) {
73             port = 80; // Work around java.net.URL bug
74
}
75         server.append(scheme);
76         server.append ("://");
77         server.append(request.getServerName());
78         if ( (scheme.equals("http") && (port != 80))
79             || (scheme.equals("https") && (port != 443)) ) {
80             server.append(':');
81             server.append(port);
82         }
83         return server.toString();
84     }
85
86     public static String JavaDoc getServer2(HttpServletRequest JavaDoc request) {
87
88         StringBuffer JavaDoc server = new StringBuffer JavaDoc(128);
89         server.append(request.getScheme());
90         server.append ("://");
91         server.append(request.getHeader("host"));
92         return server.toString();
93     }
94
95     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc param) {
96
97         String JavaDoc ret = request.getParameter(param);
98         if (ret == null) ret = "";
99         return ret.trim();
100     }
101
102     public static String JavaDoc getParameterFilter(HttpServletRequest JavaDoc request, String JavaDoc param) {
103         return DisableHtmlTagFilter.filter(getParameter(request, param));
104     }
105
106     public static String JavaDoc getParameter(HttpServletRequest JavaDoc request, String JavaDoc param, boolean checkEmpty)
107         throws BadInputException {
108
109         String JavaDoc ret = request.getParameter(param);
110         if (ret == null) ret = "";
111         ret = ret.trim();
112         if ( checkEmpty && (ret.length() == 0) ) {
113             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
114             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
115             throw new BadInputException(localizedMessage);
116         }
117         return ret;
118     }
119
120     public static String JavaDoc getParameterFilter(HttpServletRequest JavaDoc request, String JavaDoc param, boolean checkEmpty)
121         throws BadInputException {
122         return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty));
123     }
124
125     /** @todo review this method */
126     public static String JavaDoc getParameterSafe(HttpServletRequest JavaDoc request, String JavaDoc param, boolean checkEmpty)
127         throws BadInputException {
128
129         String JavaDoc ret = getParameter(request, param, checkEmpty);
130         if ( (ret.indexOf('<') != -1) ||
131              (ret.indexOf('>') != -1)) {
132             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
133             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
134             throw new BadInputException(localizedMessage);
135         }
136         return ret;
137     }
138
139     public static int getParameterInt(HttpServletRequest JavaDoc request, String JavaDoc param)
140         throws BadInputException {
141
142         String JavaDoc inputStr = getParameter(request, param, true);
143         int ret;
144         try {
145             ret = Integer.parseInt(inputStr);
146         } catch (NumberFormatException JavaDoc e) {
147             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
148             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "int"});
149             throw new BadInputException(localizedMessage);
150         }
151         return ret;
152     }
153
154     public static int getParameterUnsignedInt(HttpServletRequest JavaDoc request, String JavaDoc param)
155         throws BadInputException {
156
157         int retValue = getParameterInt(request, param);
158         if (retValue < 0) {
159             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
160             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
161             throw new BadInputException(localizedMessage);
162         }
163         return retValue;
164     }
165
166     public static int getParameterInt(HttpServletRequest JavaDoc request, String JavaDoc param, int defaultValue)
167         throws BadInputException {
168
169         String JavaDoc inputStr = getParameter(request, param, false);
170         if (inputStr.length() == 0) {
171             return defaultValue;
172         }
173         int ret;
174         try {
175             ret = Integer.parseInt(inputStr);
176         } catch (NumberFormatException JavaDoc e) {
177             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
178             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "int"});
179             throw new BadInputException(localizedMessage);
180         }
181         return ret;
182     }
183
184     public static int getParameterUnsignedInt(HttpServletRequest JavaDoc request, String JavaDoc param, int defaultValue)
185         throws BadInputException {
186
187         int retValue = getParameterInt(request, param, defaultValue);
188         if (retValue < 0) {
189             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
190             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
191             throw new BadInputException(localizedMessage);
192         }
193         return retValue;
194     }
195
196     public static long getParameterLong(HttpServletRequest JavaDoc request, String JavaDoc param)
197         throws BadInputException {
198
199         String JavaDoc inputStr = getParameter(request, param, true);
200         long ret;
201         try {
202             ret = Long.parseLong(inputStr);
203         } catch (NumberFormatException JavaDoc e) {
204             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
205             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "long"});
206             throw new BadInputException(localizedMessage);
207         }
208         return ret;
209     }
210
211     public static long getParameterLong(HttpServletRequest JavaDoc request, String JavaDoc param, long defaultValue)
212         throws BadInputException {
213
214         String JavaDoc inputStr = getParameter(request, param, false);
215         if (inputStr.length() == 0) {
216             return defaultValue;
217         }
218
219         long ret;
220         try {
221             ret = Long.parseLong(inputStr);
222         } catch (NumberFormatException JavaDoc e) {
223             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
224             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "long"});
225             throw new BadInputException(localizedMessage);
226         }
227         return ret;
228     }
229
230     /**
231      * @param param is the name of variable
232      * @return true if the value of param is not empty
233      */

234     public static boolean getParameterBoolean(HttpServletRequest JavaDoc request, String JavaDoc param) {
235
236         String JavaDoc inputStr = getParameter(request, param);
237         if (inputStr.length() == 0) return false;
238         return true;
239     }
240
241     public static byte getParameterByte(HttpServletRequest JavaDoc request, String JavaDoc param)
242         throws BadInputException {
243
244         String JavaDoc inputStr = getParameter(request, param, true);
245         byte ret;
246         try {
247             ret = Byte.parseByte(inputStr);
248         } catch (NumberFormatException JavaDoc e) {
249             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
250             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "byte"});
251             throw new BadInputException(localizedMessage);
252         }
253         return ret;
254     }
255
256     public static double getParameterDouble(HttpServletRequest JavaDoc request, String JavaDoc param)
257         throws BadInputException {
258
259         String JavaDoc inputStr = getParameter(request, param, true);
260         double ret;
261         try {
262             ret = Double.parseDouble(inputStr);
263         } catch (NumberFormatException JavaDoc e) {
264             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
265             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param), "double"});
266             throw new BadInputException(localizedMessage);
267         }
268         return ret;
269     }
270
271     public static String JavaDoc getParameterUrl(HttpServletRequest JavaDoc request, String JavaDoc param)
272         throws BadInputException {
273
274         String JavaDoc ret = getParameter(request, param);
275         if ( ret.length() > 0 ) {
276             if ( !ret.startsWith("http://") &&
277                  !ret.startsWith("https://") &&
278                  !ret.startsWith("ftp://") ) {
279                 Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
280                 String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_url", new Object JavaDoc[] {DisableHtmlTagFilter.filter(param)});
281                 throw new BadInputException(localizedMessage);
282             }
283         }
284         return ret;
285     }
286
287     public static String JavaDoc getParameterPassword(HttpServletRequest JavaDoc request, String JavaDoc param, int minLength, int option)
288         throws BadInputException {
289
290         if (minLength < 1) minLength = 1;
291
292         String JavaDoc ret = request.getParameter(param);
293         if (ret == null) ret = "";
294         ret = ret.trim();
295
296         if ( ret.length() < minLength ) {
297             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
298             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.password_too_short", new Object JavaDoc[] {new Integer JavaDoc(minLength)});
299             throw new BadInputException(localizedMessage);
300         }
301
302         /** @todo implement this feature */
303         if (option == 1) {//char and number
304

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

307         }
308         return ret;
309     }
310
311     public static String JavaDoc getParameterEmail(HttpServletRequest JavaDoc request, String JavaDoc param)
312         throws BadInputException {
313
314         String JavaDoc email = getParameterSafe(request, param, true);
315         MailUtil.checkGoodEmail(email);
316         return email;
317     }
318
319     /**
320      *
321      */

322     public static java.sql.Date JavaDoc getParameterDate(HttpServletRequest JavaDoc request, String JavaDoc param)
323         throws BadInputException {
324
325         String JavaDoc inputStr = getParameter(request, param, true);
326         java.util.Date JavaDoc ret;
327         try {
328             ret = dateFormat.parse(inputStr);
329         } catch (java.text.ParseException JavaDoc e) {
330             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
331             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(param), "Date"});
332             throw new BadInputException(localizedMessage);
333         }
334         return new java.sql.Date JavaDoc(ret.getTime());
335     }
336
337     /**
338      *
339      */

340     public static java.util.Date JavaDoc getParameterDateUtil(HttpServletRequest JavaDoc request, String JavaDoc param)
341         throws BadInputException {
342
343         String JavaDoc inputStr = getParameter(request, param, true);
344         java.util.Date JavaDoc ret;
345         try {
346             ret = dateFormat.parse(inputStr);
347         } catch (java.text.ParseException JavaDoc e) {
348             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
349             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(param), "Date"});
350             throw new BadInputException(localizedMessage);
351         }
352         return ret;
353     }
354
355     /**
356      *
357      */

358     public static java.sql.Date JavaDoc getParameterDate(HttpServletRequest JavaDoc request, String JavaDoc paramDay, String JavaDoc paramMonth, String JavaDoc paramYear)
359         throws BadInputException {
360
361         int day = getParameterInt(request, paramDay);
362         int month = getParameterInt(request, paramMonth);
363         int year = getParameterInt(request, paramYear);
364         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
365         buffer.append(day).append("/").append(month).append("/").append(year);
366         String JavaDoc inputStr = buffer.toString();
367
368         java.util.Date JavaDoc ret;
369         try {
370             ret = dateFormat.parse(inputStr);
371         } catch (java.text.ParseException JavaDoc e) {
372             Locale JavaDoc locale = I18nUtil.getLocaleInRequest(request);
373             String JavaDoc localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object JavaDoc[]{DisableHtmlTagFilter.filter(inputStr), "Date"});
374             throw new BadInputException(localizedMessage);
375         }
376         return new java.sql.Date JavaDoc(ret.getTime());
377     }
378
379     public static double getParameterTimeZone(HttpServletRequest JavaDoc request, String JavaDoc param)
380         throws BadInputException {
381
382         double timeZone = getParameterDouble(request, param);
383         if (timeZone < -12 || timeZone > 13) {
384             timeZone = 0;
385         }
386         return timeZone;
387     }
388
389     public static String JavaDoc getAttribute(HttpSession JavaDoc session, String JavaDoc name) {
390
391         String JavaDoc ret = (String JavaDoc)session.getAttribute(name);
392         if (ret == null) ret = "";
393         return ret.trim();
394     }
395
396     /**
397      * Note: sometime in the dispatched request, we dont receive HttpServletRequest
398      * but receive ServletRequest, such as com.caucho.server.webapp.DispatchRequest
399      *
400      * @param request ServletRequest
401      * @param name String
402      * @return String
403      */

404     public static String JavaDoc getAttribute(ServletRequest JavaDoc request, String JavaDoc name) {
405
406         String JavaDoc ret = (String JavaDoc)request.getAttribute(name);
407         if (ret == null) ret = "";
408         return ret.trim();
409     }
410
411     /**
412      * Note: we have to use this method because (very strange) that the
413      * HttpServletRequest object does not accept above method
414      * getAttribute(ServletRequest request, String name)
415      *
416      * @param request HttpServletRequest
417      * @param name String
418      * @return String
419      */

420     public static String JavaDoc getAttribute(HttpServletRequest JavaDoc request, String JavaDoc name) {
421
422         String JavaDoc ret = (String JavaDoc)request.getAttribute(name);
423         if (ret == null) ret = "";
424         return ret.trim();
425     }
426
427 }
428
Popular Tags