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.ServletRequest ; 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpSession ; 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() { } 53 54 57 private static DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy"); 58 59 public static String getContextPath() { 60 return MVNCoreConfig.getContextPath(); 61 } 62 63 public static String getServerPath() { 64 return MVNCoreConfig.getServerPath(); 65 } 66 67 public static String getServer(HttpServletRequest request) { 68 69 StringBuffer server = new StringBuffer (128); 70 String scheme = request.getScheme(); 71 int port = request.getServerPort(); 72 if (port < 0) { 73 port = 80; } 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 getServer2(HttpServletRequest request) { 87 88 StringBuffer server = new StringBuffer (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 getParameter(HttpServletRequest request, String param) { 96 97 String ret = request.getParameter(param); 98 if (ret == null) ret = ""; 99 return ret.trim(); 100 } 101 102 public static String getParameterFilter(HttpServletRequest request, String param) { 103 return DisableHtmlTagFilter.filter(getParameter(request, param)); 104 } 105 106 public static String getParameter(HttpServletRequest request, String param, boolean checkEmpty) 107 throws BadInputException { 108 109 String ret = request.getParameter(param); 110 if (ret == null) ret = ""; 111 ret = ret.trim(); 112 if ( checkEmpty && (ret.length() == 0) ) { 113 Locale locale = I18nUtil.getLocaleInRequest(request); 114 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_allow_to_be_empty", new Object [] {DisableHtmlTagFilter.filter(param)}); 115 throw new BadInputException(localizedMessage); 116 } 117 return ret; 118 } 119 120 public static String getParameterFilter(HttpServletRequest request, String param, boolean checkEmpty) 121 throws BadInputException { 122 return DisableHtmlTagFilter.filter(getParameter(request, param, checkEmpty)); 123 } 124 125 126 public static String getParameterSafe(HttpServletRequest request, String param, boolean checkEmpty) 127 throws BadInputException { 128 129 String ret = getParameter(request, param, checkEmpty); 130 if ( (ret.indexOf('<') != -1) || 131 (ret.indexOf('>') != -1)) { 132 Locale locale = I18nUtil.getLocaleInRequest(request); 133 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.parameter_safe", new Object [] {DisableHtmlTagFilter.filter(param)}); 134 throw new BadInputException(localizedMessage); 135 } 136 return ret; 137 } 138 139 public static int getParameterInt(HttpServletRequest request, String param) 140 throws BadInputException { 141 142 String inputStr = getParameter(request, param, true); 143 int ret; 144 try { 145 ret = Integer.parseInt(inputStr); 146 } catch (NumberFormatException e) { 147 Locale locale = I18nUtil.getLocaleInRequest(request); 148 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "int"}); 149 throw new BadInputException(localizedMessage); 150 } 151 return ret; 152 } 153 154 public static int getParameterUnsignedInt(HttpServletRequest request, String param) 155 throws BadInputException { 156 157 int retValue = getParameterInt(request, param); 158 if (retValue < 0) { 159 Locale locale = I18nUtil.getLocaleInRequest(request); 160 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object [] {DisableHtmlTagFilter.filter(param)}); 161 throw new BadInputException(localizedMessage); 162 } 163 return retValue; 164 } 165 166 public static int getParameterInt(HttpServletRequest request, String param, int defaultValue) 167 throws BadInputException { 168 169 String 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 e) { 177 Locale locale = I18nUtil.getLocaleInRequest(request); 178 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "int"}); 179 throw new BadInputException(localizedMessage); 180 } 181 return ret; 182 } 183 184 public static int getParameterUnsignedInt(HttpServletRequest request, String param, int defaultValue) 185 throws BadInputException { 186 187 int retValue = getParameterInt(request, param, defaultValue); 188 if (retValue < 0) { 189 Locale locale = I18nUtil.getLocaleInRequest(request); 190 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.must_be_unsigned_value", new Object [] {DisableHtmlTagFilter.filter(param)}); 191 throw new BadInputException(localizedMessage); 192 } 193 return retValue; 194 } 195 196 public static long getParameterLong(HttpServletRequest request, String param) 197 throws BadInputException { 198 199 String inputStr = getParameter(request, param, true); 200 long ret; 201 try { 202 ret = Long.parseLong(inputStr); 203 } catch (NumberFormatException e) { 204 Locale locale = I18nUtil.getLocaleInRequest(request); 205 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "long"}); 206 throw new BadInputException(localizedMessage); 207 } 208 return ret; 209 } 210 211 public static long getParameterLong(HttpServletRequest request, String param, long defaultValue) 212 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(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "long"}); 225 throw new BadInputException(localizedMessage); 226 } 227 return ret; 228 } 229 230 234 public static boolean getParameterBoolean(HttpServletRequest request, String param) { 235 236 String inputStr = getParameter(request, param); 237 if (inputStr.length() == 0) return false; 238 return true; 239 } 240 241 public static byte getParameterByte(HttpServletRequest request, String param) 242 throws BadInputException { 243 244 String inputStr = getParameter(request, param, true); 245 byte ret; 246 try { 247 ret = Byte.parseByte(inputStr); 248 } catch (NumberFormatException e) { 249 Locale locale = I18nUtil.getLocaleInRequest(request); 250 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "byte"}); 251 throw new BadInputException(localizedMessage); 252 } 253 return ret; 254 } 255 256 public static double getParameterDouble(HttpServletRequest request, String param) 257 throws BadInputException { 258 259 String inputStr = getParameter(request, param, true); 260 double ret; 261 try { 262 ret = Double.parseDouble(inputStr); 263 } catch (NumberFormatException e) { 264 Locale locale = I18nUtil.getLocaleInRequest(request); 265 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object [] {DisableHtmlTagFilter.filter(param), "double"}); 266 throw new BadInputException(localizedMessage); 267 } 268 return ret; 269 } 270 271 public static String getParameterUrl(HttpServletRequest request, String param) 272 throws BadInputException { 273 274 String ret = getParameter(request, param); 275 if ( ret.length() > 0 ) { 276 if ( !ret.startsWith("http://") && 277 !ret.startsWith("https://") && 278 !ret.startsWith("ftp://") ) { 279 Locale locale = I18nUtil.getLocaleInRequest(request); 280 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.not_url", new Object [] {DisableHtmlTagFilter.filter(param)}); 281 throw new BadInputException(localizedMessage); 282 } 283 } 284 return ret; 285 } 286 287 public static String getParameterPassword(HttpServletRequest request, String param, int minLength, int option) 288 throws BadInputException { 289 290 if (minLength < 1) minLength = 1; 291 292 String ret = request.getParameter(param); 293 if (ret == null) ret = ""; 294 ret = ret.trim(); 295 296 if ( ret.length() < minLength ) { 297 Locale locale = I18nUtil.getLocaleInRequest(request); 298 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.password_too_short", new Object [] {new Integer (minLength)}); 299 throw new BadInputException(localizedMessage); 300 } 301 302 303 if (option == 1) { 305 } else if (option == 2) { 307 } 308 return ret; 309 } 310 311 public static String getParameterEmail(HttpServletRequest request, String param) 312 throws BadInputException { 313 314 String email = getParameterSafe(request, param, true); 315 MailUtil.checkGoodEmail(email); 316 return email; 317 } 318 319 322 public static java.sql.Date getParameterDate(HttpServletRequest request, String param) 323 throws BadInputException { 324 325 String inputStr = getParameter(request, param, true); 326 java.util.Date ret; 327 try { 328 ret = dateFormat.parse(inputStr); 329 } catch (java.text.ParseException e) { 330 Locale locale = I18nUtil.getLocaleInRequest(request); 331 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(param), "Date"}); 332 throw new BadInputException(localizedMessage); 333 } 334 return new java.sql.Date (ret.getTime()); 335 } 336 337 340 public static java.util.Date getParameterDateUtil(HttpServletRequest request, String param) 341 throws BadInputException { 342 343 String inputStr = getParameter(request, param, true); 344 java.util.Date ret; 345 try { 346 ret = dateFormat.parse(inputStr); 347 } catch (java.text.ParseException e) { 348 Locale locale = I18nUtil.getLocaleInRequest(request); 349 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(param), "Date"}); 350 throw new BadInputException(localizedMessage); 351 } 352 return ret; 353 } 354 355 358 public static java.sql.Date getParameterDate(HttpServletRequest request, String paramDay, String paramMonth, String 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 buffer = new StringBuffer (); 365 buffer.append(day).append("/").append(month).append("/").append(year); 366 String inputStr = buffer.toString(); 367 368 java.util.Date ret; 369 try { 370 ret = dateFormat.parse(inputStr); 371 } catch (java.text.ParseException e) { 372 Locale locale = I18nUtil.getLocaleInRequest(request); 373 String localizedMessage = MVNCoreResourceBundle.getString(locale, "mvncore.exception.BadInputException.cannot_parse", new Object []{DisableHtmlTagFilter.filter(inputStr), "Date"}); 374 throw new BadInputException(localizedMessage); 375 } 376 return new java.sql.Date (ret.getTime()); 377 } 378 379 public static double getParameterTimeZone(HttpServletRequest request, String 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 getAttribute(HttpSession session, String name) { 390 391 String ret = (String )session.getAttribute(name); 392 if (ret == null) ret = ""; 393 return ret.trim(); 394 } 395 396 404 public static String getAttribute(ServletRequest request, String name) { 405 406 String ret = (String )request.getAttribute(name); 407 if (ret == null) ret = ""; 408 return ret.trim(); 409 } 410 411 420 public static String getAttribute(HttpServletRequest request, String name) { 421 422 String ret = (String )request.getAttribute(name); 423 if (ret == null) ret = ""; 424 return ret.trim(); 425 } 426 427 } 428 | Popular Tags |