1 package org.ashkelon.util; 2 6 7 import java.util.Enumeration ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import java.util.Set ; 12 13 import javax.servlet.ServletRequest ; 14 import javax.servlet.http.HttpServletRequest ; 15 16 22 public class ServletUtils 23 { 24 public static int getIntParam(ServletRequest request, String name) 25 { 26 String value = getRequestParam(request, name); 28 if (StringUtils.isBlank(value)) 29 value = "0"; 30 try 31 { 32 return Integer.parseInt(value); 33 } 34 catch (NumberFormatException ex) 35 { 36 return -1; 37 } 38 } 39 40 public static String getRequestParam(ServletRequest request, String key) 41 { 42 Object val = request.getAttribute(key); 43 String value; 44 if (val == null) 45 value = StringUtils.avoidNull(request.getParameter(key)); 46 else 47 value = val.toString(); 48 return value; 49 } 50 51 public static String getQueryString(ServletRequest request) 52 { 53 Enumeration e = request.getParameterNames(); 54 Map pairs = new HashMap (6); 55 String name; 56 while (e.hasMoreElements()) 57 { 58 name = (String ) e.nextElement(); 59 pairs.put(name, request.getParameter(name)); 60 } 61 e = request.getAttributeNames(); 62 Object attr_value; 63 while (e.hasMoreElements()) 64 { 65 name = (String ) e.nextElement(); 66 attr_value = request.getAttribute(name); 67 if (attr_value instanceof String ) 68 pairs.put(name, attr_value); 69 } 70 71 String [] strpairs = new String [pairs.size()]; 72 Set entrySet = pairs.entrySet(); 73 Iterator itr = entrySet.iterator(); 74 Map.Entry entry; 75 int i=0; 76 while (itr.hasNext()) 77 { 78 entry = (Map.Entry ) itr.next(); 79 strpairs[i++] = (String ) entry.getKey() + "=" + entry.getValue(); 80 } 81 return StringUtils.join(strpairs, "&"); 82 } 83 84 public static String getCommand(HttpServletRequest request) 85 { 86 String path = request.getServletPath(); 87 int len = path.length(); 89 String cmd = path.substring(1,len-3); return cmd; 91 } 92 private static void debugRequest(HttpServletRequest request) 93 { 94 Logger logger = Logger.getInstance(); 95 logger.verbose("getServletPath: "+request.getServletPath()); 96 logger.verbose("getRequestURI: "+request.getRequestURI()); 97 logger.verbose("getQueryString: "+request.getQueryString()); 98 logger.verbose("getPathTranslated: "+request.getPathTranslated()); 99 logger.verbose("getPathInfo: "+request.getPathInfo()); 100 logger.verbose("getContextPath: "+request.getContextPath()); 101 } 102 103 public static String getCommand(HttpServletRequest request, String defaultCmd) 104 { 105 String cmd = getCommand(request); 106 if (StringUtils.isBlank(cmd)) 107 { 108 cmd = defaultCmd; 109 Logger.getInstance().traceln("defaulting to cmd: "+cmd); 110 } 111 return cmd; 112 } 113 114 } 115 | Popular Tags |