001: /**
002: * $RCSfile$
003: * $Revision
004: * $Date: 2004-11-09 16:36:36 -0800 (Tue, 09 Nov 2004) $
005: *
006: * Copyright (C) 1999-2004 Jive Software. All rights reserved.
007: *
008: * This software is the proprietary information of Jive Software. Use is subject to license terms.
009: */package org.jivesoftware.util;
010:
011: import javax.servlet.http.Cookie;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: public class CookieUtils {
016:
017: /**
018: * Returns the specified cookie, or <tt>null</tt> if the cookie
019: * does not exist. Note: because of the way that cookies are implemented
020: * it's possible for multiple cookies with the same name to exist (but with
021: * different domain values). This method will return the first cookie that
022: * has a name match.
023: *
024: * @param request the servlet request.
025: * @param name the name of the cookie.
026: * @return the Cookie object if it exists, otherwise <tt>null</tt>.
027: */
028: public static Cookie getCookie(HttpServletRequest request,
029: String name) {
030: Cookie cookies[] = request.getCookies();
031: // Return null if there are no cookies or the name is invalid.
032: if (cookies == null || name == null || name.length() == 0) {
033: return null;
034: }
035: // Otherwise, we do a linear scan for the cookie.
036: Cookie cookie = null;
037: for (int i = 0; i < cookies.length; i++) {
038: // If the current cookie name matches the one we're looking for, we've
039: // found a matching cookie.
040: if (cookies[i].getName().equals(name)) {
041: cookie = cookies[i];
042: // The best matching cookie will be the one that has the correct
043: // domain name. If we've found the cookie with the correct domain name,
044: // return it. Otherwise, we'll keep looking for a better match.
045: if (request.getServerName().equals(cookie.getDomain())) {
046: break;
047: }
048: }
049: }
050: return cookie;
051: }
052:
053: /**
054: * Deletes the specified cookie.
055: *
056: * @param request the servlet request.
057: * @param response the servlet response.
058: * @param cookie the cookie object to be deleted.
059: */
060: public static void deleteCookie(HttpServletRequest request,
061: HttpServletResponse response, Cookie cookie) {
062: if (cookie != null) {
063: // Invalidate the cookie
064: String path = request.getContextPath() == null ? "/"
065: : request.getContextPath();
066: if ("".equals(path)) {
067: path = "/";
068: }
069: cookie.setPath(path);
070: cookie.setValue("");
071: cookie.setMaxAge(0);
072: response.addCookie(cookie);
073: }
074: }
075:
076: /**
077: * Stores a value in a cookie. By default this cookie will persist for 30 days.
078: *
079: * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String,int)
080: * @param request the servlet request.
081: * @param response the servlet response.
082: * @param name a name to identify the cookie.
083: * @param value the value to store in the cookie.
084: */
085: public static void setCookie(HttpServletRequest request,
086: HttpServletResponse response, String name, String value) {
087: // Save the cookie value for 1 month
088: setCookie(request, response, name, value, 60 * 60 * 24 * 30);
089: }
090:
091: /**
092: * Stores a value in a cookie. This cookie will persist for the amount
093: * specified in the <tt>saveTime</tt> parameter.
094: *
095: * @see #setCookie(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,String,String)
096: * @param request the servlet request.
097: * @param response the servlet response.
098: * @param name a name to identify the cookie.
099: * @param value the value to store in the cookie.
100: * @param maxAge the time (in seconds) this cookie should live.
101: */
102: public static void setCookie(HttpServletRequest request,
103: HttpServletResponse response, String name, String value,
104: int maxAge) {
105: // Check to make sure the new value is not null (appservers like Tomcat
106: // 4 blow up if the value is null).
107: if (value == null) {
108: value = "";
109: }
110: String path = request.getContextPath() == null ? "/" : request
111: .getContextPath();
112: if ("".equals(path)) {
113: path = "/";
114: }
115: Cookie cookie = new Cookie(name, value);
116: cookie.setMaxAge(maxAge);
117: cookie.setPath(path);
118: response.addCookie(cookie);
119: }
120: }
|