KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > util > RequestUtil


1 package org.appfuse.webapp.util;
2
3 import javax.servlet.http.Cookie JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import javax.servlet.http.HttpServletResponse JavaDoc;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10
11 /**
12  * Convenience class for setting and retrieving cookies.
13  */

14 public class RequestUtil {
15     private transient static Log log = LogFactory.getLog(RequestUtil.class);
16
17     /**
18      * Convenience method to set a cookie
19      *
20      * @param response
21      * @param name
22      * @param value
23      * @param path
24      */

25     public static void setCookie(HttpServletResponse JavaDoc response, String JavaDoc name,
26                                  String JavaDoc value, String JavaDoc path) {
27         if (log.isDebugEnabled()) {
28             log.debug("Setting cookie '" + name + "' on path '" + path + "'");
29         }
30
31         Cookie JavaDoc cookie = new Cookie JavaDoc(name, value);
32         cookie.setSecure(false);
33         cookie.setPath(path);
34         cookie.setMaxAge(3600 * 24 * 30); // 30 days
35

36         response.addCookie(cookie);
37     }
38
39     /**
40      * Convenience method to get a cookie by name
41      *
42      * @param request the current request
43      * @param name the name of the cookie to find
44      *
45      * @return the cookie (if found), null if not found
46      */

47     public static Cookie JavaDoc getCookie(HttpServletRequest JavaDoc request, String JavaDoc name) {
48         Cookie JavaDoc[] cookies = request.getCookies();
49         Cookie JavaDoc returnCookie = null;
50
51         if (cookies == null) {
52             return returnCookie;
53         }
54
55         for (int i = 0; i < cookies.length; i++) {
56             Cookie JavaDoc thisCookie = cookies[i];
57
58             if (thisCookie.getName().equals(name)) {
59                 // cookies with no value do me no good!
60
if (!thisCookie.getValue().equals("")) {
61                     returnCookie = thisCookie;
62
63                     break;
64                 }
65             }
66         }
67
68         return returnCookie;
69     }
70
71     /**
72      * Convenience method for deleting a cookie by name
73      *
74      * @param response the current web response
75      * @param cookie the cookie to delete
76      * @param path the path on which the cookie was set (i.e. /appfuse)
77      */

78     public static void deleteCookie(HttpServletResponse JavaDoc response,
79                                     Cookie JavaDoc cookie, String JavaDoc path) {
80         if (cookie != null) {
81             // Delete the cookie by setting its maximum age to zero
82
cookie.setMaxAge(0);
83             cookie.setPath(path);
84             response.addCookie(cookie);
85         }
86     }
87     
88     /**
89      * Convenience method to get the application's URL based on request
90      * variables.
91      */

92     public static String JavaDoc getAppURL(HttpServletRequest JavaDoc request) {
93         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
94         int port = request.getServerPort();
95         if (port < 0) {
96             port = 80; // Work around java.net.URL bug
97
}
98         String JavaDoc scheme = request.getScheme();
99         url.append(scheme);
100         url.append("://");
101         url.append(request.getServerName());
102         if ((scheme.equals("http") && (port != 80)) || (scheme.equals("https") && (port != 443))) {
103             url.append(':');
104             url.append(port);
105         }
106         url.append(request.getContextPath());
107         return url.toString();
108     }
109 }
110
Popular Tags