Source Code Cross Referenced for GenericParamUtil.java in  » Forum » mvnforum-1.1 » net » myvietnam » mvncore » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » mvnforum 1.1 » net.myvietnam.mvncore.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/GenericParamUtil.java,v 1.11 2008/01/18 01:59:44 phuongpdd Exp $
003:         * $Author: phuongpdd $
004:         * $Revision: 1.11 $
005:         * $Date: 2008/01/18 01:59:44 $
006:         *
007:         * ====================================================================
008:         *
009:         * Copyright (C) 2002-2007 by MyVietnam.net
010:         *
011:         * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012:         * MUST remain intact in the scripts and source code.
013:         *
014:         * This library is free software; you can redistribute it and/or
015:         * modify it under the terms of the GNU Lesser General Public
016:         * License as published by the Free Software Foundation; either
017:         * version 2.1 of the License, or (at your option) any later version.
018:         *
019:         * This library is distributed in the hope that it will be useful,
020:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
021:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
022:         * Lesser General Public License for more details.
023:         *
024:         * You should have received a copy of the GNU Lesser General Public
025:         * License along with this library; if not, write to the Free Software
026:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
027:         *
028:         * Correspondence and Marketing Questions can be sent to:
029:         * info at MyVietnam net
030:         *
031:         * @author: Minh Nguyen  
032:         * @author: Mai  Nguyen  
033:         */
034:        package net.myvietnam.mvncore.util;
035:
036:        import java.text.DateFormat;
037:        import java.text.SimpleDateFormat;
038:        import java.util.Locale;
039:
040:        import javax.servlet.http.HttpSession;
041:
042:        import net.myvietnam.mvncore.MVNCoreResourceBundle;
043:        import net.myvietnam.mvncore.exception.BadInputException;
044:        import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
045:        import net.myvietnam.mvncore.web.GenericRequest;
046:
047:        public final class GenericParamUtil {
048:
049:            private GenericParamUtil() { // prevent instantiation
050:            }
051:
052:            //private static String contextPath = (new ParamOptions()).contextPath;
053:            //private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block
054:
055:            private static DateFormat dateFormat = new SimpleDateFormat(
056:                    "dd/MM/yyyy");
057:
058:            public static String getParameter(GenericRequest request,
059:                    String param) {
060:
061:                String ret = request.getParameter(param);
062:                if (ret == null)
063:                    ret = "";
064:                return ret.trim();
065:            }
066:
067:            public static String getParameterFilter(GenericRequest request,
068:                    String param) {
069:                return DisableHtmlTagFilter
070:                        .filter(getParameter(request, param));
071:            }
072:
073:            public static String getParameter(GenericRequest request,
074:                    String param, boolean checkEmpty) throws BadInputException {
075:
076:                String ret = request.getParameter(param);
077:                if (ret == null)
078:                    ret = "";
079:                ret = ret.trim();
080:                if (checkEmpty && (ret.length() == 0)) {
081:                    Locale locale = I18nUtil.getLocaleInRequest(request);
082:                    String localizedMessage = MVNCoreResourceBundle
083:                            .getString(
084:                                    locale,
085:                                    "mvncore.exception.BadInputException.not_allow_to_be_empty",
086:                                    new Object[] { DisableHtmlTagFilter
087:                                            .filter(param) });
088:                    throw new BadInputException(localizedMessage);
089:                }
090:                return ret;
091:            }
092:
093:            public static String getParameterFilter(GenericRequest request,
094:                    String param, boolean checkEmpty) throws BadInputException {
095:                return DisableHtmlTagFilter.filter(getParameter(request, param,
096:                        checkEmpty));
097:            }
098:
099:            /** @todo review this method */
100:            public static String getParameterSafe(GenericRequest request,
101:                    String param, boolean checkEmpty) throws BadInputException {
102:
103:                String ret = getParameter(request, param, checkEmpty);
104:                if ((ret.indexOf('<') != -1) || (ret.indexOf('>') != -1)) {
105:                    Locale locale = I18nUtil.getLocaleInRequest(request);
106:                    String localizedMessage = MVNCoreResourceBundle
107:                            .getString(
108:                                    locale,
109:                                    "mvncore.exception.BadInputException.parameter_safe",
110:                                    new Object[] { DisableHtmlTagFilter
111:                                            .filter(param) });
112:                    throw new BadInputException(localizedMessage);
113:                }
114:                return ret;
115:            }
116:
117:            public static int getParameterInt(GenericRequest request,
118:                    String param) throws BadInputException {
119:
120:                String inputStr = getParameter(request, param, true);
121:                int ret;
122:                try {
123:                    ret = Integer.parseInt(inputStr);
124:                } catch (NumberFormatException e) {
125:                    Locale locale = I18nUtil.getLocaleInRequest(request);
126:                    String localizedMessage = MVNCoreResourceBundle.getString(
127:                            locale,
128:                            "mvncore.exception.BadInputException.cannot_parse",
129:                            new Object[] { DisableHtmlTagFilter.filter(param),
130:                                    "int" });
131:                    throw new BadInputException(localizedMessage);
132:                }
133:                return ret;
134:            }
135:
136:            public static int getParameterUnsignedInt(GenericRequest request,
137:                    String param) throws BadInputException {
138:
139:                int retValue = getParameterInt(request, param);
140:                if (retValue < 0) {
141:                    Locale locale = I18nUtil.getLocaleInRequest(request);
142:                    String localizedMessage = MVNCoreResourceBundle
143:                            .getString(
144:                                    locale,
145:                                    "mvncore.exception.BadInputException.must_be_unsigned_value",
146:                                    new Object[] { DisableHtmlTagFilter
147:                                            .filter(param) });
148:                    throw new BadInputException(localizedMessage);
149:                }
150:                return retValue;
151:            }
152:
153:            public static int getParameterInt(GenericRequest request,
154:                    String param, int defaultValue) throws BadInputException {
155:
156:                String inputStr = getParameter(request, param, false);
157:                if (inputStr.length() == 0) {
158:                    return defaultValue;
159:                }
160:                int ret;
161:                try {
162:                    ret = Integer.parseInt(inputStr);
163:                } catch (NumberFormatException e) {
164:                    Locale locale = I18nUtil.getLocaleInRequest(request);
165:                    String localizedMessage = MVNCoreResourceBundle.getString(
166:                            locale,
167:                            "mvncore.exception.BadInputException.cannot_parse",
168:                            new Object[] { DisableHtmlTagFilter.filter(param),
169:                                    "int" });
170:                    throw new BadInputException(localizedMessage);
171:                }
172:                return ret;
173:            }
174:
175:            public static int getParameterUnsignedInt(GenericRequest request,
176:                    String param, int defaultValue) throws BadInputException {
177:
178:                int retValue = getParameterInt(request, param, defaultValue);
179:                if (retValue < 0) {
180:                    Locale locale = I18nUtil.getLocaleInRequest(request);
181:                    String localizedMessage = MVNCoreResourceBundle
182:                            .getString(
183:                                    locale,
184:                                    "mvncore.exception.BadInputException.must_be_unsigned_value",
185:                                    new Object[] { DisableHtmlTagFilter
186:                                            .filter(param) });
187:                    throw new BadInputException(localizedMessage);
188:                }
189:                return retValue;
190:            }
191:
192:            public static long getParameterLong(GenericRequest request,
193:                    String param) throws BadInputException {
194:
195:                String inputStr = getParameter(request, param, true);
196:                long ret;
197:                try {
198:                    ret = Long.parseLong(inputStr);
199:                } catch (NumberFormatException e) {
200:                    Locale locale = I18nUtil.getLocaleInRequest(request);
201:                    String localizedMessage = MVNCoreResourceBundle.getString(
202:                            locale,
203:                            "mvncore.exception.BadInputException.cannot_parse",
204:                            new Object[] { DisableHtmlTagFilter.filter(param),
205:                                    "long" });
206:                    throw new BadInputException(localizedMessage);
207:                }
208:                return ret;
209:            }
210:
211:            public static long getParameterLong(GenericRequest request,
212:                    String param, long defaultValue) throws BadInputException {
213:
214:                String 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 e) {
223:                    Locale locale = I18nUtil.getLocaleInRequest(request);
224:                    String localizedMessage = MVNCoreResourceBundle.getString(
225:                            locale,
226:                            "mvncore.exception.BadInputException.cannot_parse",
227:                            new Object[] { DisableHtmlTagFilter.filter(param),
228:                                    "long" });
229:                    throw new BadInputException(localizedMessage);
230:                }
231:                return ret;
232:            }
233:
234:            /**
235:             * @param  param is the name of variable
236:             * @return true if the value of param is not empty
237:             */
238:            public static boolean getParameterBoolean(GenericRequest request,
239:                    String param) {
240:
241:                String inputStr = getParameter(request, param);
242:                if (inputStr.length() == 0)
243:                    return false;
244:                return true;
245:            }
246:
247:            public static byte getParameterByte(GenericRequest request,
248:                    String param) throws BadInputException {
249:
250:                String inputStr = getParameter(request, param, true);
251:                byte ret;
252:                try {
253:                    ret = Byte.parseByte(inputStr);
254:                } catch (NumberFormatException e) {
255:                    Locale locale = I18nUtil.getLocaleInRequest(request);
256:                    String localizedMessage = MVNCoreResourceBundle.getString(
257:                            locale,
258:                            "mvncore.exception.BadInputException.cannot_parse",
259:                            new Object[] { DisableHtmlTagFilter.filter(param),
260:                                    "byte" });
261:                    throw new BadInputException(localizedMessage);
262:                }
263:                return ret;
264:            }
265:
266:            public static double getParameterDouble(GenericRequest request,
267:                    String param) throws BadInputException {
268:
269:                String inputStr = getParameter(request, param, true);
270:                double ret;
271:                try {
272:                    ret = Double.parseDouble(inputStr);
273:                } catch (NumberFormatException e) {
274:                    Locale locale = I18nUtil.getLocaleInRequest(request);
275:                    String localizedMessage = MVNCoreResourceBundle.getString(
276:                            locale,
277:                            "mvncore.exception.BadInputException.cannot_parse",
278:                            new Object[] { DisableHtmlTagFilter.filter(param),
279:                                    "double" });
280:                    throw new BadInputException(localizedMessage);
281:                }
282:                return ret;
283:            }
284:
285:            public static String getParameterUrl(GenericRequest request,
286:                    String param) throws BadInputException {
287:
288:                String ret = getParameter(request, param);
289:                if (ret.length() > 0) {
290:                    if (!ret.startsWith("http://")
291:                            && !ret.startsWith("https://")
292:                            && !ret.startsWith("ftp://")) {
293:                        Locale locale = I18nUtil.getLocaleInRequest(request);
294:                        String localizedMessage = MVNCoreResourceBundle
295:                                .getString(
296:                                        locale,
297:                                        "mvncore.exception.BadInputException.not_url",
298:                                        new Object[] { DisableHtmlTagFilter
299:                                                .filter(param) });
300:                        throw new BadInputException(localizedMessage);
301:                    }
302:                }
303:                return ret;
304:            }
305:
306:            public static String getParameterPassword(GenericRequest request,
307:                    String param, int minLength, int option)
308:                    throws BadInputException {
309:
310:                if (minLength < 1)
311:                    minLength = 1;
312:
313:                String ret = request.getParameter(param);
314:                if (ret == null)
315:                    ret = "";
316:                ret = ret.trim();
317:
318:                if (ret.length() < minLength) {
319:                    Locale locale = I18nUtil.getLocaleInRequest(request);
320:                    String localizedMessage = MVNCoreResourceBundle
321:                            .getString(
322:                                    locale,
323:                                    "mvncore.exception.BadInputException.password_too_short",
324:                                    new Object[] { new Integer(minLength) });
325:                    throw new BadInputException(localizedMessage);
326:                }
327:
328:                /** @todo implement this feature */
329:                //if (option == 1) {//char and number
330:                //} else if (option == 2) {// lower char, upper char and number
331:                //}
332:                return ret;
333:            }
334:
335:            public static String getParameterEmail(GenericRequest request,
336:                    String param) throws BadInputException {
337:
338:                String email = getParameterSafe(request, param, true);
339:                MailUtil.checkGoodEmail(email);
340:                return email;
341:            }
342:
343:            /**
344:             *
345:             */
346:            public static java.sql.Date getParameterDate(
347:                    GenericRequest request, String param)
348:                    throws BadInputException {
349:
350:                String inputStr = getParameter(request, param, true);
351:                java.util.Date ret;
352:                try {
353:                    ret = dateFormat.parse(inputStr);
354:                } catch (java.text.ParseException e) {
355:                    Locale locale = I18nUtil.getLocaleInRequest(request);
356:                    String localizedMessage = MVNCoreResourceBundle.getString(
357:                            locale,
358:                            "mvncore.exception.BadInputException.cannot_parse",
359:                            new Object[] { DisableHtmlTagFilter.filter(param),
360:                                    "Date" });
361:                    throw new BadInputException(localizedMessage);
362:                }
363:                return new java.sql.Date(ret.getTime());
364:            }
365:
366:            /**
367:             *
368:             */
369:            public static java.util.Date getParameterDateUtil(
370:                    GenericRequest request, String param)
371:                    throws BadInputException {
372:
373:                String inputStr = getParameter(request, param, true);
374:                java.util.Date ret;
375:                try {
376:                    ret = dateFormat.parse(inputStr);
377:                } catch (java.text.ParseException e) {
378:                    Locale locale = I18nUtil.getLocaleInRequest(request);
379:                    String localizedMessage = MVNCoreResourceBundle.getString(
380:                            locale,
381:                            "mvncore.exception.BadInputException.cannot_parse",
382:                            new Object[] { DisableHtmlTagFilter.filter(param),
383:                                    "Date" });
384:                    throw new BadInputException(localizedMessage);
385:                }
386:                return ret;
387:            }
388:
389:            /**
390:             *
391:             */
392:            public static java.sql.Date getParameterDate(
393:                    GenericRequest request, String paramDay, String paramMonth,
394:                    String paramYear) throws BadInputException {
395:
396:                int day = getParameterInt(request, paramDay);
397:                int month = getParameterInt(request, paramMonth);
398:                int year = getParameterInt(request, paramYear);
399:                StringBuffer buffer = new StringBuffer();
400:                buffer.append(day).append("/").append(month).append("/")
401:                        .append(year);
402:                String inputStr = buffer.toString();
403:
404:                java.util.Date ret;
405:                try {
406:                    ret = dateFormat.parse(inputStr);
407:                } catch (java.text.ParseException e) {
408:                    Locale locale = I18nUtil.getLocaleInRequest(request);
409:                    String localizedMessage = MVNCoreResourceBundle.getString(
410:                            locale,
411:                            "mvncore.exception.BadInputException.cannot_parse",
412:                            new Object[] {
413:                                    DisableHtmlTagFilter.filter(inputStr),
414:                                    "Date" });
415:                    throw new BadInputException(localizedMessage);
416:                }
417:                return new java.sql.Date(ret.getTime());
418:            }
419:
420:            public static java.sql.Date getParameterDateSafe(
421:                    GenericRequest request, String paramDay, String paramMonth,
422:                    String paramYear) throws BadInputException {
423:
424:                int day = 0;
425:                int month = 0;
426:                int year = 0;
427:                try {
428:                    day = getParameterInt(request, paramDay);
429:                    month = getParameterInt(request, paramMonth);
430:                    year = getParameterInt(request, paramYear);
431:                } catch (BadInputException e) {
432:                    //do nothing
433:                }
434:                StringBuffer buffer = new StringBuffer();
435:                buffer.append(day).append("/").append(month).append("/")
436:                        .append(year);
437:                String inputStr = buffer.toString();
438:
439:                java.util.Date ret;
440:                try {
441:                    ret = dateFormat.parse(inputStr);
442:                } catch (java.text.ParseException e) {
443:                    Locale locale = I18nUtil.getLocaleInRequest(request);
444:                    String localizedMessage = MVNCoreResourceBundle.getString(
445:                            locale,
446:                            "mvncore.exception.BadInputException.cannot_parse",
447:                            new Object[] {
448:                                    DisableHtmlTagFilter.filter(inputStr),
449:                                    "Date" });
450:                    throw new BadInputException(localizedMessage);
451:                }
452:                return new java.sql.Date(ret.getTime());
453:            }
454:
455:            public static double getParameterTimeZone(GenericRequest request,
456:                    String param) throws BadInputException {
457:
458:                double timeZone = getParameterDouble(request, param);
459:                if (timeZone < -12 || timeZone > 13) {
460:                    timeZone = 0;
461:                }
462:                return timeZone;
463:            }
464:
465:            public static String getAttribute(HttpSession session, String name) {
466:
467:                String ret = (String) session.getAttribute(name);
468:                if (ret == null)
469:                    ret = "";
470:                return ret.trim();
471:            }
472:
473:            /**
474:             * Note: sometime in the dispatched request, we dont receive HttpServletRequest
475:             * but receive ServletRequest, such as com.caucho.server.webapp.DispatchRequest
476:             *
477:             * @param request ServletRequest
478:             * @param name String
479:             * @return String
480:             */
481:            public static String getAttribute(GenericRequest request,
482:                    String name) {
483:
484:                String ret = (String) request.getAttribute(name);
485:                if (ret == null)
486:                    ret = "";
487:                return ret.trim();
488:            }
489:
490:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.