KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > CookieUtils


1 /**
2  * $RCSfile: CookieUtils.java,v $
3  * $Revision
4  * $Date: 2004/11/10 00:36:36 $
5  *
6  * Copyright (C) 1999-2004 Jive Software. All rights reserved.
7  *
8  * This software is the proprietary information of Jive Software. Use is subject to license terms.
9  */

10
11 package org.jivesoftware.util;
12
13 import javax.servlet.http.Cookie JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 public class CookieUtils {
18
19     /**
20      * Returns the specified cookie, or <tt>null</tt> if the cookie
21      * does not exist. Note: because of the way that cookies are implemented
22      * it's possible for multiple cookies with the same name to exist (but with
23      * different domain values). This method will return the first cookie that
24      * has a name match.
25      *
26      * @param request the servlet request.
27      * @param name the name of the cookie.
28      * @return the Cookie object if it exists, otherwise <tt>null</tt>.
29      */

30     public static Cookie JavaDoc getCookie(HttpServletRequest JavaDoc request, String JavaDoc name) {
31         Cookie JavaDoc cookies[] = request.getCookies();
32         // Return null if there are no cookies or the name is invalid.
33
if (cookies == null || name == null || name.length() == 0) {
34             return null;
35         }
36         // Otherwise, we do a linear scan for the cookie.
37
Cookie JavaDoc cookie = null;
38         for (int i = 0; i < cookies.length; i++) {
39             // If the current cookie name matches the one we're looking for, we've
40
// found a matching cookie.
41
if (cookies[i].getName().equals(name)) {
42                 cookie = cookies[i];
43                 // The best matching cookie will be the one that has the correct
44
// domain name. If we've found the cookie with the correct domain name,
45
// return it. Otherwise, we'll keep looking for a better match.
46
if (request.getServerName().equals(cookie.getDomain())) {
47                     break;
48                 }
49             }
50         }
51         return cookie;
52     }
53
54     /**
55      * Deletes the specified cookie.
56      *
57      * @param request the servlet request.
58      * @param response the servlet response.
59      * @param cookie the cookie object to be deleted.
60      */

61     public static void deleteCookie(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
62             Cookie JavaDoc cookie)
63     {
64         if (cookie != null) {
65             // Invalidate the cookie
66
String JavaDoc path = request.getContextPath() == null ? "/" : request.getContextPath();
67             if ("".equals(path)) {
68                 path = "/";
69             }
70             cookie.setPath(path);
71             cookie.setValue("");
72             cookie.setMaxAge(0);
73             response.addCookie(cookie);
74         }
75     }
76
77     /**
78      * Stores a value in a cookie. By default this cookie will persist for 30 days.
79      *
80      * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String,int)
81      * @param request the servlet request.
82      * @param response the servlet response.
83      * @param name a name to identify the cookie.
84      * @param value the value to store in the cookie.
85      */

86     public static void setCookie(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
87             String JavaDoc name, String JavaDoc value)
88     {
89         // Save the cookie value for 1 month
90
setCookie(request, response, name, value, 60*60*24*30);
91     }
92
93     /**
94      * Stores a value in a cookie. This cookie will persist for the amount
95      * specified in the <tt>saveTime</tt> parameter.
96      *
97      * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
98      * @param request the servlet request.
99      * @param response the servlet response.
100      * @param name a name to identify the cookie.
101      * @param value the value to store in the cookie.
102      * @param maxAge the time (in seconds) this cookie should live.
103      */

104     public static void setCookie(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
105             String JavaDoc name, String JavaDoc value, int maxAge)
106     {
107         // Check to make sure the new value is not null (appservers like Tomcat
108
// 4 blow up if the value is null).
109
if (value == null) {
110             value = "";
111         }
112         String JavaDoc path = request.getContextPath() == null ? "/" : request.getContextPath();
113         if ("".equals(path)) {
114             path = "/";
115         }
116         Cookie JavaDoc cookie = new Cookie JavaDoc(name, value);
117         cookie.setMaxAge(maxAge);
118         cookie.setPath(path);
119         response.addCookie(cookie);
120     }
121 }
122
Popular Tags