KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > util > ServletUtils


1 package org.ashkelon.util;
2 /**
3  * Copyright UptoData Inc. 2001
4  * March 2001
5  */

6
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import javax.servlet.ServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15
16 /**
17  * Contains various utility methods for dealing with http stuff
18  * using the servlet api
19  *
20  * @author Eitan Suez
21  */

22 public class ServletUtils
23 {
24    public static int getIntParam(ServletRequest JavaDoc request, String JavaDoc name)
25    {
26       // if name is null or blank, set to 0
27
String JavaDoc value = getRequestParam(request, name);
28       if (StringUtils.isBlank(value))
29          value = "0";
30       try
31       {
32          return Integer.parseInt(value);
33       }
34       catch (NumberFormatException JavaDoc ex)
35       {
36          return -1;
37       }
38    }
39    
40    public static String JavaDoc getRequestParam(ServletRequest JavaDoc request, String JavaDoc key)
41    {
42       Object JavaDoc val = request.getAttribute(key);
43       String JavaDoc 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 JavaDoc getQueryString(ServletRequest JavaDoc request)
52    {
53       Enumeration JavaDoc e = request.getParameterNames();
54       Map JavaDoc pairs = new HashMap JavaDoc(6);
55       String JavaDoc name;
56       while (e.hasMoreElements())
57       {
58          name = (String JavaDoc) e.nextElement();
59          pairs.put(name, request.getParameter(name));
60       }
61       e = request.getAttributeNames();
62       Object JavaDoc attr_value;
63       while (e.hasMoreElements())
64       {
65          name = (String JavaDoc) e.nextElement();
66          attr_value = request.getAttribute(name);
67          if (attr_value instanceof String JavaDoc)
68             pairs.put(name, attr_value);
69       }
70       
71       String JavaDoc[] strpairs = new String JavaDoc[pairs.size()];
72       Set JavaDoc entrySet = pairs.entrySet();
73       Iterator JavaDoc itr = entrySet.iterator();
74       Map.Entry JavaDoc entry;
75       int i=0;
76       while (itr.hasNext())
77       {
78          entry = (Map.Entry JavaDoc) itr.next();
79          strpairs[i++] = (String JavaDoc) entry.getKey() + "=" + entry.getValue();
80       }
81       return StringUtils.join(strpairs, "&");
82    }
83
84    public static String JavaDoc getCommand(HttpServletRequest JavaDoc request)
85    {
86        String JavaDoc path = request.getServletPath();
87        //debugRequest(request);
88
int len = path.length();
89        String JavaDoc cmd = path.substring(1,len-3); // trim prefixed / and suffixed .do
90
return cmd;
91    }
92    private static void debugRequest(HttpServletRequest JavaDoc 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 JavaDoc getCommand(HttpServletRequest JavaDoc request, String JavaDoc defaultCmd)
104    {
105       String JavaDoc 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