1 11 12 package org.jivesoftware.util; 13 14 import javax.servlet.http.HttpServletRequest ; 15 16 19 public class ParamUtils { 20 21 30 public static String getParameter(HttpServletRequest request, String name) { 31 return getParameter(request, name, false); 32 } 33 34 44 public static String getParameter(HttpServletRequest request, String name, 45 boolean emptyStringsOK) 46 { 47 String temp = request.getParameter(name); 48 if (temp != null) { 49 if (temp.equals("") && !emptyStringsOK) { 50 return null; 51 } 52 else { 53 return temp; 54 } 55 } 56 else { 57 return null; 58 } 59 } 60 61 68 public static String [] getParameters(HttpServletRequest request, String name) { 69 if (name == null) { 70 return new String [0]; 71 } 72 String [] paramValues = request.getParameterValues(name); 73 if (paramValues == null || paramValues.length == 0) { 74 return new String [0]; 75 } 76 else { 77 java.util.List values = new java.util.ArrayList (paramValues.length); 78 for (int i = 0; i < paramValues.length; i++) { 79 if (paramValues[i] != null && !"".equals(paramValues[i])) { 80 values.add(paramValues[i]); 81 } 82 } 83 return (String [])values.toArray(new String []{}); 84 } 85 } 86 87 95 public static boolean getBooleanParameter(HttpServletRequest request, String name) { 96 return getBooleanParameter(request, name, false); 97 } 98 99 107 public static boolean getBooleanParameter(HttpServletRequest request, 108 String name, boolean defaultVal) 109 { 110 String temp = request.getParameter(name); 111 if ("true".equals(temp) || "on".equals(temp)) { 112 return true; 113 } 114 else if ("false".equals(temp) || "off".equals(temp)) { 115 return false; 116 } 117 else { 118 return defaultVal; 119 } 120 } 121 122 131 public static int getIntParameter(HttpServletRequest request, 132 String name, int defaultNum) { 133 String temp = request.getParameter(name); 134 if (temp != null && !temp.equals("")) { 135 int num = defaultNum; 136 try { 137 num = Integer.parseInt(temp); 138 } 139 catch (Exception ignored) { 140 } 141 return num; 142 } 143 else { 144 return defaultNum; 145 } 146 } 147 148 157 public static int[] getIntParameters(HttpServletRequest request, 158 String name, int defaultNum) { 159 String [] paramValues = request.getParameterValues(name); 160 if (paramValues == null || paramValues.length == 0) { 161 return new int[0]; 162 } 163 int[] values = new int[paramValues.length]; 164 for (int i = 0; i < paramValues.length; i++) { 165 try { 166 values[i] = Integer.parseInt(paramValues[i]); 167 } 168 catch (Exception e) { 169 values[i] = defaultNum; 170 } 171 } 172 return values; 173 } 174 175 184 public static double getDoubleParameter(HttpServletRequest request, String name, double defaultNum) { 185 String temp = request.getParameter(name); 186 if (temp != null && !temp.equals("")) { 187 double num = defaultNum; 188 try { 189 num = Double.parseDouble(temp); 190 } 191 catch (Exception ignored) { 192 } 193 return num; 194 } 195 else { 196 return defaultNum; 197 } 198 } 199 200 209 public static long getLongParameter(HttpServletRequest request, String name, long defaultNum) { 210 String temp = request.getParameter(name); 211 if (temp != null && !temp.equals("")) { 212 long num = defaultNum; 213 try { 214 num = Long.parseLong(temp); 215 } 216 catch (Exception ignored) { 217 } 218 return num; 219 } 220 else { 221 return defaultNum; 222 } 223 } 224 225 234 public static long[] getLongParameters(HttpServletRequest request, String name, 235 long defaultNum) 236 { 237 String [] paramValues = request.getParameterValues(name); 238 if (paramValues == null || paramValues.length == 0) { 239 return new long[0]; 240 } 241 long[] values = new long[paramValues.length]; 242 for (int i = 0; i < paramValues.length; i++) { 243 try { 244 values[i] = Long.parseLong(paramValues[i]); 245 } 246 catch (Exception e) { 247 values[i] = defaultNum; 248 } 249 } 250 return values; 251 } 252 253 261 public static String getAttribute(HttpServletRequest request, String name) { 262 return getAttribute(request, name, false); 263 } 264 265 274 public static String getAttribute(HttpServletRequest request, String name, 275 boolean emptyStringsOK) 276 { 277 String temp = (String )request.getAttribute(name); 278 if (temp != null) { 279 if (temp.equals("") && !emptyStringsOK) { 280 return null; 281 } 282 else { 283 return temp; 284 } 285 } 286 else { 287 return null; 288 } 289 } 290 291 298 public static boolean getBooleanAttribute(HttpServletRequest request, String name) { 299 String temp = (String )request.getAttribute(name); 300 if (temp != null && temp.equals("true")) { 301 return true; 302 } 303 else { 304 return false; 305 } 306 } 307 308 316 public static int getIntAttribute(HttpServletRequest request, String name, int defaultNum) { 317 String temp = (String )request.getAttribute(name); 318 if (temp != null && !temp.equals("")) { 319 int num = defaultNum; 320 try { 321 num = Integer.parseInt(temp); 322 } 323 catch (Exception ignored) { 324 } 325 return num; 326 } 327 else { 328 return defaultNum; 329 } 330 } 331 332 340 public static long getLongAttribute(HttpServletRequest request, String name, long defaultNum) { 341 String temp = (String )request.getAttribute(name); 342 if (temp != null && !temp.equals("")) { 343 long num = defaultNum; 344 try { 345 num = Long.parseLong(temp); 346 } 347 catch (Exception ignored) { 348 } 349 return num; 350 } 351 else { 352 return defaultNum; 353 } 354 } 355 } | Popular Tags |