1 34 package net.myvietnam.mvncore.util; 35 36 import java.text.DateFormat ; 37 import java.text.SimpleDateFormat ; 38 import java.util.Locale ; 39 40 import javax.servlet.http.HttpSession ; 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() { } 51 52 55 private static DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy"); 56 57 public static String getParameter(GenericRequest request, String param) { 58 59 String ret = request.getParameter(param); 60 if (ret == null) ret = ""; 61 return ret.trim(); 62 } 63 64 public static String getParameterFilter(GenericRequest request, String param) { 65 return DisableHtmlTagFilter.filter(getParameter(request, param)); 66 } 67 68 public static String getParameter(GenericRequest request, String param, boolean checkEmpty) 69 throws BadInputException { 70 71 String ret = request.getParameter(param); 72 if (ret == null) ret = ""; 73 ret = ret.trim(); 74 if ( checkEmpty && (ret.length() == 0) ) { 75 Locale locale = I18nUtil.getLocaleInRequest(request); 76 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object [] {DisableHtmlTagFilter.filter(param)}); 77 throw new BadInputException(localizedMessage); 78 } 79 return ret; 80 } 81 82 public static String getParameterFilter(GenericRequest request, String param, boolean checkEmpty) 83 throws BadInputException { 84 return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty)); 85 } 86 87 88 public static String getParameterSafe(GenericRequest request, String param, boolean checkEmpty) 89 throws BadInputException { 90 91 String ret = getParameter(request, param, checkEmpty); 92 if ( (ret.indexOf('<') != -1) || 93 (ret.indexOf('>') != -1)) { 94 Locale locale = I18nUtil.getLocaleInRequest(request); 95 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object [] {DisableHtmlTagFilter.filter(param)}); 96 throw new BadInputException(localizedMessage); 97 } 98 return ret; 99 } 100 101 public static int getParameterInt(GenericRequest request, String param) 102 throws BadInputException { 103 104 String inputStr = getParameter(request, param, true); 105 int ret; 106 try { 107 ret = Integer.parseInt(inputStr); 108 } catch (NumberFormatException e) { 109 Locale locale = I18nUtil.getLocaleInRequest(request); 110 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "int"}); 111 throw new BadInputException(localizedMessage); 112 } 113 return ret; 114 } 115 116 public static int getParameterUnsignedInt(GenericRequest request, String param) 117 throws BadInputException { 118 119 int retValue = getParameterInt(request, param); 120 if (retValue < 0) { 121 Locale locale = I18nUtil.getLocaleInRequest(request); 122 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object [] {DisableHtmlTagFilter.filter(param)}); 123 throw new BadInputException(localizedMessage); 124 } 125 return retValue; 126 } 127 128 public static int getParameterInt(GenericRequest request, String param, int defaultValue) 129 throws BadInputException { 130 131 String 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 e) { 139 Locale locale = I18nUtil.getLocaleInRequest(request); 140 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "int"}); 141 throw new BadInputException(localizedMessage); 142 } 143 return ret; 144 } 145 146 public static int getParameterUnsignedInt(GenericRequest request, String param, int defaultValue) 147 throws BadInputException { 148 149 int retValue = getParameterInt(request, param, defaultValue); 150 if (retValue < 0) { 151 Locale locale = I18nUtil.getLocaleInRequest(request); 152 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object [] {DisableHtmlTagFilter.filter(param)}); 153 throw new BadInputException(localizedMessage); 154 } 155 return retValue; 156 } 157 158 public static long getParameterLong(GenericRequest request, String param) 159 throws BadInputException { 160 161 String inputStr = getParameter(request, param, true); 162 long ret; 163 try { 164 ret = Long.parseLong(inputStr); 165 } catch (NumberFormatException e) { 166 Locale locale = I18nUtil.getLocaleInRequest(request); 167 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "long"}); 168 throw new BadInputException(localizedMessage); 169 } 170 return ret; 171 } 172 173 public static long getParameterLong(GenericRequest request, String param, long defaultValue) 174 throws BadInputException { 175 176 String 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 e) { 185 Locale locale = I18nUtil.getLocaleInRequest(request); 186 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "long"}); 187 throw new BadInputException(localizedMessage); 188 } 189 return ret; 190 } 191 192 196 public static boolean getParameterBoolean(GenericRequest request, String param) { 197 198 String inputStr = getParameter(request, param); 199 if (inputStr.length() == 0) return false; 200 return true; 201 } 202 203 public static byte getParameterByte(GenericRequest request, String param) 204 throws BadInputException { 205 206 String inputStr = getParameter(request, param, true); 207 byte ret; 208 try { 209 ret = Byte.parseByte(inputStr); 210 } catch (NumberFormatException e) { 211 Locale locale = I18nUtil.getLocaleInRequest(request); 212 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "byte"}); 213 throw new BadInputException(localizedMessage); 214 } 215 return ret; 216 } 217 218 public static double getParameterDouble(GenericRequest request, String param) 219 throws BadInputException { 220 221 String inputStr = getParameter(request, param, true); 222 double ret; 223 try { 224 ret = Double.parseDouble(inputStr); 225 } catch (NumberFormatException e) { 226 Locale locale = I18nUtil.getLocaleInRequest(request); 227 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "double"}); 228 throw new BadInputException(localizedMessage); 229 } 230 return ret; 231 } 232 233 public static String getParameterUrl(GenericRequest request, String param) 234 throws BadInputException { 235 236 String ret = getParameter(request, param); 237 if ( ret.length() > 0 ) { 238 if ( !ret.startsWith("http://") && 239 !ret.startsWith("https://") && 240 !ret.startsWith("ftp://") ) { 241 Locale locale = I18nUtil.getLocaleInRequest(request); 242 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_url", new Object [] {DisableHtmlTagFilter.filter(param)}); 243 throw new BadInputException(localizedMessage); 244 } 245 } 246 return ret; 247 } 248 249 public static String getParameterPassword(GenericRequest request, String param, int minLength, int option) 250 throws BadInputException { 251 252 if (minLength < 1) minLength = 1; 253 254 String ret = request.getParameter(param); 255 if (ret == null) ret = ""; 256 ret = ret.trim(); 257 258 if ( ret.length() < minLength ) { 259 Locale locale = I18nUtil.getLocaleInRequest(request); 260 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.password_too_short", new Object [] {new Integer (minLength)}); 261 throw new BadInputException(localizedMessage); 262 } 263 264 265 if (option == 1) { 267 } else if (option == 2) { 269 } 270 return ret; 271 } 272 273 public static String getParameterEmail(GenericRequest request, String param) 274 throws BadInputException { 275 276 String email = getParameterSafe(request, param, true); 277 MailUtil.checkGoodEmail(email); 278 return email; 279 } 280 281 284 public static java.sql.Date getParameterDate(GenericRequest request, String param) 285 throws BadInputException { 286 287 String inputStr = getParameter(request, param, true); 288 java.util.Date ret; 289 try { 290 ret = dateFormat.parse(inputStr); 291 } catch (java.text.ParseException e) { 292 Locale locale = I18nUtil.getLocaleInRequest(request); 293 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(param), "Date"}); 294 throw new BadInputException(localizedMessage); 295 } 296 return new java.sql.Date (ret.getTime()); 297 } 298 299 302 public static java.util.Date getParameterDateUtil(GenericRequest request, String param) 303 throws BadInputException { 304 305 String inputStr = getParameter(request, param, true); 306 java.util.Date ret; 307 try { 308 ret = dateFormat.parse(inputStr); 309 } catch (java.text.ParseException e) { 310 Locale locale = I18nUtil.getLocaleInRequest(request); 311 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(param), "Date"}); 312 throw new BadInputException(localizedMessage); 313 } 314 return ret; 315 } 316 317 320 public static java.sql.Date getParameterDate(GenericRequest request, String paramDay, String paramMonth, String 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 buffer = new StringBuffer (); 327 buffer.append(day).append("/").append(month).append("/").append(year); 328 String inputStr = buffer.toString(); 329 330 java.util.Date ret; 331 try { 332 ret = dateFormat.parse(inputStr); 333 } catch (java.text.ParseException e) { 334 Locale locale = I18nUtil.getLocaleInRequest(request); 335 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(inputStr), "Date"}); 336 throw new BadInputException(localizedMessage); 337 } 338 return new java.sql.Date (ret.getTime()); 339 } 340 341 public static double getParameterTimeZone(GenericRequest request, String 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 getAttribute(HttpSession session, String name) { 352 353 String ret = (String )session.getAttribute(name); 354 if (ret == null) ret = ""; 355 return ret.trim(); 356 } 357 358 366 public static String getAttribute(GenericRequest request, String name) { 367 368 String ret = (String )request.getAttribute(name); 369 if (ret == null) ret = ""; 370 return ret.trim(); 371 } 372 373 } 374 | Popular Tags |