001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/util/ParamUtil.java,v 1.29 2007/06/22 03:19:24 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.29 $
005: * $Date: 2007/06/22 03:19:24 $
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.ServletRequest;
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpSession;
043:
044: import net.myvietnam.mvncore.MVNCoreConfig;
045: import net.myvietnam.mvncore.MVNCoreResourceBundle;
046: import net.myvietnam.mvncore.exception.BadInputException;
047: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
048:
049: public final class ParamUtil {
050:
051: private ParamUtil() { // prevent instantiation
052: }
053:
054: //private static String contextPath = (new ParamOptions()).contextPath;
055: //private static String serverPath = (new ParamOptions()).serverPath;//@todo combine 2 line to a static block
056:
057: private static DateFormat dateFormat = new SimpleDateFormat(
058: "dd/MM/yyyy");
059:
060: private static String contextPath;
061:
062: public static String getContextPath() {
063: return MVNCoreConfig.getContextPath();
064: }
065:
066: public static void setContextPath(String path) {
067: contextPath = path;
068: }
069:
070: public static String getServerPath() {
071: return MVNCoreConfig.getServerPath();
072: }
073:
074: public static String getServer(HttpServletRequest request) {
075:
076: StringBuffer server = new StringBuffer(128);
077: String scheme = request.getScheme();
078: int port = request.getServerPort();
079: if (port < 0) {
080: port = 80; // Work around java.net.URL bug
081: }
082: server.append(scheme);
083: server.append("://");
084: server.append(request.getServerName());
085: if ((scheme.equals("http") && (port != 80))
086: || (scheme.equals("https") && (port != 443))) {
087: server.append(':');
088: server.append(port);
089: }
090: return server.toString();
091: }
092:
093: public static String getServer2(HttpServletRequest request) {
094:
095: StringBuffer server = new StringBuffer(128);
096: server.append(request.getScheme());
097: server.append("://");
098: server.append(request.getHeader("host"));
099: return server.toString();
100: }
101:
102: public static String getParameter(HttpServletRequest request,
103: String param) {
104:
105: String ret = request.getParameter(param);
106: if (ret == null)
107: ret = "";
108: return ret.trim();
109: }
110:
111: public static String getParameterFilter(HttpServletRequest request,
112: String param) {
113: return DisableHtmlTagFilter
114: .filter(getParameter(request, param));
115: }
116:
117: public static String getParameter(HttpServletRequest request,
118: String param, boolean checkEmpty) throws BadInputException {
119:
120: String ret = request.getParameter(param);
121: if (ret == null)
122: ret = "";
123: ret = ret.trim();
124: if (checkEmpty && (ret.length() == 0)) {
125: Locale locale = I18nUtil.getLocaleInRequest(request);
126: String localizedMessage = MVNCoreResourceBundle
127: .getString(
128: locale,
129: "mvncore.exception.BadInputException.not_allow_to_be_empty",
130: new Object[] { DisableHtmlTagFilter
131: .filter(param) });
132: throw new BadInputException(localizedMessage);
133: }
134: return ret;
135: }
136:
137: public static String getParameterFilter(HttpServletRequest request,
138: String param, boolean checkEmpty) throws BadInputException {
139: return DisableHtmlTagFilter.filter(getParameter(request, param,
140: checkEmpty));
141: }
142:
143: /** @todo review this method */
144: public static String getParameterSafe(HttpServletRequest request,
145: String param, boolean checkEmpty) throws BadInputException {
146:
147: String ret = getParameter(request, param, checkEmpty);
148: if ((ret.indexOf('<') != -1) || (ret.indexOf('>') != -1)) {
149: Locale locale = I18nUtil.getLocaleInRequest(request);
150: String localizedMessage = MVNCoreResourceBundle
151: .getString(
152: locale,
153: "mvncore.exception.BadInputException.parameter_safe",
154: new Object[] { DisableHtmlTagFilter
155: .filter(param) });
156: throw new BadInputException(localizedMessage);
157: }
158: return ret;
159: }
160:
161: public static int getParameterInt(HttpServletRequest request,
162: String param) throws BadInputException {
163:
164: String inputStr = getParameter(request, param, true);
165: int ret;
166: try {
167: ret = Integer.parseInt(inputStr);
168: } catch (NumberFormatException e) {
169: Locale locale = I18nUtil.getLocaleInRequest(request);
170: String localizedMessage = MVNCoreResourceBundle.getString(
171: locale,
172: "mvncore.exception.BadInputException.cannot_parse",
173: new Object[] { DisableHtmlTagFilter.filter(param),
174: "int" });
175: throw new BadInputException(localizedMessage);
176: }
177: return ret;
178: }
179:
180: public static int getParameterUnsignedInt(
181: HttpServletRequest request, String param)
182: throws BadInputException {
183:
184: int retValue = getParameterInt(request, param);
185: if (retValue < 0) {
186: Locale locale = I18nUtil.getLocaleInRequest(request);
187: String localizedMessage = MVNCoreResourceBundle
188: .getString(
189: locale,
190: "mvncore.exception.BadInputException.must_be_unsigned_value",
191: new Object[] { DisableHtmlTagFilter
192: .filter(param) });
193: throw new BadInputException(localizedMessage);
194: }
195: return retValue;
196: }
197:
198: public static int getParameterInt(HttpServletRequest request,
199: String param, int defaultValue) throws BadInputException {
200:
201: String inputStr = getParameter(request, param, false);
202: if (inputStr.length() == 0) {
203: return defaultValue;
204: }
205: int ret;
206: try {
207: ret = Integer.parseInt(inputStr);
208: } catch (NumberFormatException e) {
209: Locale locale = I18nUtil.getLocaleInRequest(request);
210: String localizedMessage = MVNCoreResourceBundle.getString(
211: locale,
212: "mvncore.exception.BadInputException.cannot_parse",
213: new Object[] { DisableHtmlTagFilter.filter(param),
214: "int" });
215: throw new BadInputException(localizedMessage);
216: }
217: return ret;
218: }
219:
220: public static int getParameterUnsignedInt(
221: HttpServletRequest request, String param, int defaultValue)
222: throws BadInputException {
223:
224: int retValue = getParameterInt(request, param, defaultValue);
225: if (retValue < 0) {
226: Locale locale = I18nUtil.getLocaleInRequest(request);
227: String localizedMessage = MVNCoreResourceBundle
228: .getString(
229: locale,
230: "mvncore.exception.BadInputException.must_be_unsigned_value",
231: new Object[] { DisableHtmlTagFilter
232: .filter(param) });
233: throw new BadInputException(localizedMessage);
234: }
235: return retValue;
236: }
237:
238: public static long getParameterLong(HttpServletRequest request,
239: String param) throws BadInputException {
240:
241: String inputStr = getParameter(request, param, true);
242: long ret;
243: try {
244: ret = Long.parseLong(inputStr);
245: } catch (NumberFormatException e) {
246: Locale locale = I18nUtil.getLocaleInRequest(request);
247: String localizedMessage = MVNCoreResourceBundle.getString(
248: locale,
249: "mvncore.exception.BadInputException.cannot_parse",
250: new Object[] { DisableHtmlTagFilter.filter(param),
251: "long" });
252: throw new BadInputException(localizedMessage);
253: }
254: return ret;
255: }
256:
257: public static long getParameterLong(HttpServletRequest request,
258: String param, long defaultValue) throws BadInputException {
259:
260: String inputStr = getParameter(request, param, false);
261: if (inputStr.length() == 0) {
262: return defaultValue;
263: }
264:
265: long ret;
266: try {
267: ret = Long.parseLong(inputStr);
268: } catch (NumberFormatException e) {
269: Locale locale = I18nUtil.getLocaleInRequest(request);
270: String localizedMessage = MVNCoreResourceBundle.getString(
271: locale,
272: "mvncore.exception.BadInputException.cannot_parse",
273: new Object[] { DisableHtmlTagFilter.filter(param),
274: "long" });
275: throw new BadInputException(localizedMessage);
276: }
277: return ret;
278: }
279:
280: /**
281: * @param param is the name of variable
282: * @return true if the value of param is not empty
283: */
284: public static boolean getParameterBoolean(
285: HttpServletRequest request, String param) {
286:
287: String inputStr = getParameter(request, param);
288: if (inputStr.length() == 0)
289: return false;
290: return true;
291: }
292:
293: public static byte getParameterByte(HttpServletRequest request,
294: String param) throws BadInputException {
295:
296: String inputStr = getParameter(request, param, true);
297: byte ret;
298: try {
299: ret = Byte.parseByte(inputStr);
300: } catch (NumberFormatException e) {
301: Locale locale = I18nUtil.getLocaleInRequest(request);
302: String localizedMessage = MVNCoreResourceBundle.getString(
303: locale,
304: "mvncore.exception.BadInputException.cannot_parse",
305: new Object[] { DisableHtmlTagFilter.filter(param),
306: "byte" });
307: throw new BadInputException(localizedMessage);
308: }
309: return ret;
310: }
311:
312: public static double getParameterDouble(HttpServletRequest request,
313: String param) throws BadInputException {
314:
315: String inputStr = getParameter(request, param, true);
316: double ret;
317: try {
318: ret = Double.parseDouble(inputStr);
319: } catch (NumberFormatException e) {
320: Locale locale = I18nUtil.getLocaleInRequest(request);
321: String localizedMessage = MVNCoreResourceBundle.getString(
322: locale,
323: "mvncore.exception.BadInputException.cannot_parse",
324: new Object[] { DisableHtmlTagFilter.filter(param),
325: "double" });
326: throw new BadInputException(localizedMessage);
327: }
328: return ret;
329: }
330:
331: public static String getParameterUrl(HttpServletRequest request,
332: String param) throws BadInputException {
333:
334: String ret = getParameter(request, param);
335: if (ret.length() > 0) {
336: if (!ret.startsWith("http://")
337: && !ret.startsWith("https://")
338: && !ret.startsWith("ftp://")) {
339: Locale locale = I18nUtil.getLocaleInRequest(request);
340: String localizedMessage = MVNCoreResourceBundle
341: .getString(
342: locale,
343: "mvncore.exception.BadInputException.not_url",
344: new Object[] { DisableHtmlTagFilter
345: .filter(param) });
346: throw new BadInputException(localizedMessage);
347: }
348: }
349: return ret;
350: }
351:
352: public static String getParameterPassword(
353: HttpServletRequest request, String param, int minLength,
354: int option) throws BadInputException {
355:
356: if (minLength < 1)
357: minLength = 1;
358: String ret = request.getParameter(param);
359: if (ret == null)
360: ret = "";
361: ret = ret.trim();
362:
363: if (ret.length() < minLength) {
364: Locale locale = I18nUtil.getLocaleInRequest(request);
365: String localizedMessage = MVNCoreResourceBundle
366: .getString(
367: locale,
368: "mvncore.exception.BadInputException.password_too_short",
369: new Object[] { new Integer(minLength) });
370: throw new BadInputException(localizedMessage);
371: }
372:
373: /** @todo implement this feature */
374: if (option == 1) {//char and number
375:
376: } else if (option == 2) {// lower char, upper char and number
377:
378: }
379: return ret;
380: }
381:
382: public static String getParameterEmail(HttpServletRequest request,
383: String param) throws BadInputException {
384: String email = getParameterSafe(request, param, true);
385: MailUtil.checkGoodEmail(email);
386: return email;
387: }
388:
389: /**
390: *
391: */
392: public static java.sql.Date getParameterDate(
393: HttpServletRequest request, String param)
394: throws BadInputException {
395:
396: String inputStr = getParameter(request, param, true);
397: java.util.Date ret;
398: try {
399: ret = dateFormat.parse(inputStr);
400: } catch (java.text.ParseException e) {
401: Locale locale = I18nUtil.getLocaleInRequest(request);
402: String localizedMessage = MVNCoreResourceBundle.getString(
403: locale,
404: "mvncore.exception.BadInputException.cannot_parse",
405: new Object[] { DisableHtmlTagFilter.filter(param),
406: "Date" });
407: throw new BadInputException(localizedMessage);
408: }
409: return new java.sql.Date(ret.getTime());
410: }
411:
412: /**
413: *
414: */
415: public static java.util.Date getParameterDateUtil(
416: HttpServletRequest request, String param)
417: throws BadInputException {
418:
419: String inputStr = getParameter(request, param, true);
420: java.util.Date ret;
421: try {
422: ret = dateFormat.parse(inputStr);
423: } catch (java.text.ParseException e) {
424: Locale locale = I18nUtil.getLocaleInRequest(request);
425: String localizedMessage = MVNCoreResourceBundle.getString(
426: locale,
427: "mvncore.exception.BadInputException.cannot_parse",
428: new Object[] { DisableHtmlTagFilter.filter(param),
429: "Date" });
430: throw new BadInputException(localizedMessage);
431: }
432: return ret;
433: }
434:
435: /**
436: *
437: */
438: public static java.sql.Date getParameterDate(
439: HttpServletRequest request, String paramDay,
440: String paramMonth, String paramYear)
441: throws BadInputException {
442:
443: int day = getParameterInt(request, paramDay);
444: int month = getParameterInt(request, paramMonth);
445: int year = getParameterInt(request, paramYear);
446: StringBuffer buffer = new StringBuffer();
447: buffer.append(day).append("/").append(month).append("/")
448: .append(year);
449: String inputStr = buffer.toString();
450:
451: java.util.Date ret;
452: try {
453: ret = dateFormat.parse(inputStr);
454: } catch (java.text.ParseException e) {
455: Locale locale = I18nUtil.getLocaleInRequest(request);
456: String localizedMessage = MVNCoreResourceBundle.getString(
457: locale,
458: "mvncore.exception.BadInputException.cannot_parse",
459: new Object[] {
460: DisableHtmlTagFilter.filter(inputStr),
461: "Date" });
462: throw new BadInputException(localizedMessage);
463: }
464: return new java.sql.Date(ret.getTime());
465: }
466:
467: public static double getParameterTimeZone(
468: HttpServletRequest request, String param)
469: throws BadInputException {
470:
471: double timeZone = getParameterDouble(request, param);
472: if (timeZone < -12 || timeZone > 13) {
473: timeZone = 0;
474: }
475: return timeZone;
476: }
477:
478: public static String getAttribute(HttpSession session, String name) {
479:
480: String ret = (String) session.getAttribute(name);
481: if (ret == null)
482: ret = "";
483: return ret.trim();
484: }
485:
486: /**
487: * Note: sometime in the dispatched request, we dont receive HttpServletRequest
488: * but receive ServletRequest, such as com.caucho.server.webapp.DispatchRequest
489: *
490: * @param request ServletRequest
491: * @param name String
492: * @return String
493: */
494: public static String getAttribute(ServletRequest request,
495: String name) {
496:
497: String ret = (String) request.getAttribute(name);
498: if (ret == null)
499: ret = "";
500: return ret.trim();
501: }
502:
503: /**
504: * Note: we have to use this method because (very strange) that the
505: * HttpServletRequest object does not accept above method
506: * getAttribute(ServletRequest request, String name)
507: *
508: * @param request HttpServletRequest
509: * @param name String
510: * @return String
511: */
512: public static String getAttribute(HttpServletRequest request,
513: String name) {
514:
515: String ret = (String) request.getAttribute(name);
516: if (ret == null)
517: ret = "";
518: return ret.trim();
519: }
520:
521: }
|