KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > servlet > CookieUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.webapp.servlet;
12
13 import java.io.*;
14
15 import javax.servlet.http.*;
16
17 import org.eclipse.help.internal.webapp.*;
18 import org.eclipse.help.internal.webapp.data.*;
19
20 /**
21  * Utilities for working with cookies
22  *
23  * @since 3.0
24  */

25 public class CookieUtil {
26     private static final int COOKIE_LIFE = 5 * 365 * 24 * 60 * 60;
27     private static final int MAX_COOKIE_PAYLOAD = 4096
28             - "wset01=".length() - "81920<".length() - 1; //$NON-NLS-1$ //$NON-NLS-2$
29

30     /**
31      * @return null or String
32      */

33     public static String JavaDoc getCookieValue(String JavaDoc name, HttpServletRequest request) {
34         String JavaDoc ret = null;
35         Cookie[] cookies = request.getCookies();
36         if (cookies != null) {
37             for (int i = 0; i < cookies.length; i++) {
38                 if (name.equals(cookies[i].getName())) {
39                     ret = cookies[i].getValue();
40                     break;
41                 }
42             }
43         }
44         if (HelpWebappPlugin.DEBUG_WORKINGSETS) {
45             System.out.println("CookieUtil.getCookieValue(" //$NON-NLS-1$
46
+ name + ", " //$NON-NLS-1$
47
+ request.getRequestURI() + ") returning " //$NON-NLS-1$
48
+ ret);
49         }
50         return ret;
51     }
52     public static void setCookieValue(String JavaDoc name, String JavaDoc value,
53             HttpServletResponse response) {
54         Cookie cookie = new Cookie(name, value);
55         cookie.setMaxAge(COOKIE_LIFE);
56         response.addCookie(cookie);
57         if (HelpWebappPlugin.DEBUG_WORKINGSETS) {
58             System.out
59                     .println("CookieUtil.setCookieValue(" + name + ", " + value + ",...)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
60
}
61     }
62
63     public static void deleteCookie(String JavaDoc name, HttpServletResponse response) {
64         Cookie cookie = new Cookie(name, ""); //$NON-NLS-1$
65
cookie.setMaxAge(0);
66         response.addCookie(cookie);
67     }
68     /**
69      * Saves string in multiple browser cookies. Cookies can store limited
70      * length string. This method will attemt to split string among multiple
71      * cookies. The following cookies will be set name1=length <substing1
72      * name2=substrging2 ... namen=substringn
73      *
74      * @param data
75      * a string containing legal characters for cookie value
76      * @throws IOException
77      * when data is too long.
78      */

79     public static void saveString(String JavaDoc name, String JavaDoc data, int maxCookies,
80             HttpServletRequest request, HttpServletResponse response)
81             throws IOException {
82         int len = data.length();
83         int n = len / MAX_COOKIE_PAYLOAD;
84         if (n > maxCookies) {
85             throw new IOException(WebappResources.getString(
86                     "CookieUtil.tooManyCookiesNeeded", UrlUtil.getLocaleObj( //$NON-NLS-1$
87
request, response)));
88         }
89         for (int i = 1; i <= n; i++) {
90             if (i == 1) {
91                 setCookieValue(name + "1", //$NON-NLS-1$
92
len + "<" + data.substring(0, MAX_COOKIE_PAYLOAD), //$NON-NLS-1$
93
response);
94             } else {
95                 setCookieValue(name + i, data.substring(MAX_COOKIE_PAYLOAD
96                         * (i - 1), MAX_COOKIE_PAYLOAD * i), response);
97             }
98         }
99         if (len % MAX_COOKIE_PAYLOAD > 0) {
100             if (n == 0) {
101                 setCookieValue(name + "1", //$NON-NLS-1$
102
len + "<" + data.substring(0, len), //$NON-NLS-1$
103
response);
104             } else {
105                 setCookieValue(name + (n + 1), data.substring(
106                         MAX_COOKIE_PAYLOAD * n, len), response);
107             }
108         }
109
110         // if using less cookies than maximum, delete not needed cookies from
111
// last time
112
for (int i = n + 1; i <= maxCookies; i++) {
113             if (i == n + 1 && len % MAX_COOKIE_PAYLOAD > 0) {
114                 continue;
115             }
116             if (getCookieValue(name + i, request) != null) {
117                 deleteCookie(name + i, response);
118             } else {
119                 break;
120             }
121         }
122     }
123     /**
124      * @return null or String
125      */

126     public static String JavaDoc restoreString(String JavaDoc name, HttpServletRequest request) {
127         String JavaDoc value1 = CookieUtil.getCookieValue(name + "1", request); //$NON-NLS-1$
128
if (value1 == null) {
129             // no cookie
130
return null;
131         }
132         String JavaDoc lengthAndSubstring1[] = value1.split("<"); //$NON-NLS-1$
133
if (lengthAndSubstring1.length < 2) {
134             return null;
135         }
136         int len = 0;
137         try {
138             len = Integer.parseInt(lengthAndSubstring1[0]);
139         } catch (NumberFormatException JavaDoc nfe) {
140             return null;
141         }
142         if (len <= 0) {
143             return null;
144         }
145         StringBuffer JavaDoc data = new StringBuffer JavaDoc(len);
146         data.append(lengthAndSubstring1[1]);
147         int n = len / MAX_COOKIE_PAYLOAD;
148         for (int i = 2; i <= n; i++) {
149             String JavaDoc substring = CookieUtil.getCookieValue(name + i, request);
150             if (substring == null) {
151                 return null;
152             }
153             data.append(substring);
154         }
155         if (len % MAX_COOKIE_PAYLOAD > 0 && n > 0) {
156             String JavaDoc substring = CookieUtil.getCookieValue(name + (n + 1),
157                     request);
158             if (substring == null) {
159                 return null;
160             }
161             data.append(substring);
162         }
163
164         if (data.length() != len) {
165             return null;
166         }
167
168         return data.toString();
169     }
170 }
171
Popular Tags