KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > http > HttpCookieManager


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: HttpCookieManager.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.http;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import javax.servlet.http.Cookie JavaDoc;
34
35 /**
36  * This object manages cookies defined in http request and
37  * response headers.
38  *
39  * @author Kyle Clark
40  * @version $Revision: 1.2 $
41  * @since Harmony2.0
42  */

43 public class HttpCookieManager {
44
45     /**
46      * The set of cookies managed by this object.
47      */

48     private Vector JavaDoc cookies;
49
50     /**
51      * Constructs a new cookie manager.
52      */

53     public HttpCookieManager () {
54         cookies = new Vector JavaDoc();
55     }
56
57     /**
58      * Returns the string representation of all the cookies being managed
59      * by this object as an http cookie request header.
60      * The format of the header is:
61      * <pre>
62      * NAME1=VALUE1; NAME2=VALUE2; ....
63      * </pre>
64      *
65      * @return the "Cookie" header as it would appear in an http request header.
66      */

67     public String JavaDoc getRequestHeader () {
68         return getRequestHeader(null, null);
69     }
70
71     /**
72      * Returns the string representation of the cookies being managed
73      * by this object as an http cookie request header
74      * The format of the header is:
75      * <pre>
76      * NAME1=VALUE1; NAME2=VALUE2; ....
77      * </pre>
78      *
79      * @param path
80      * Only return the cookies that are valid in the specified
81      * path. If path is null then path is ignored.
82      * @param domain
83      * Only return the cookies that are valid in the specified
84      * domain. If null, the domain is ignored.
85      * @return the "Cookie" header as it would appear in an http request
86      * or null if this object contains no cookies that apply
87      * to the path and domain contraints.
88      */

89     public String JavaDoc getRequestHeader(String JavaDoc path, String JavaDoc domain) {
90         // FIX: use string buffer
91
String JavaDoc header = null;
92         Enumeration JavaDoc e = cookies.elements();
93         while (e.hasMoreElements()) {
94             Cookie JavaDoc c = (Cookie JavaDoc)e.nextElement();
95             boolean matchPath = true;
96             boolean matchDomain = true;
97             if (path != null) {
98                 if (c.getPath() != null)
99                     matchPath = c.getPath().startsWith(path);
100                 else
101                     matchPath = true;
102             }
103             if (domain != null) {
104                 // FIX: read spec, need to do this correctly
105
if (c.getDomain() != null)
106                     matchDomain = c.getDomain().endsWith(domain);
107                 else
108                     matchDomain = true;
109             }
110             if (matchPath && matchDomain) {
111                 if (header == null)
112                     header = c.getName() + "=" + c.getValue();
113                 else
114                     header += "; " + c.getName() + "=" + c.getValue();
115             }
116         }
117         return header;
118     }
119
120     /**
121      * Returns the string representation of the cookies being managed
122      * by this object as an http cookie response ("Set-Cookie")
123      * header. The format of the header is a comma (,) separated
124      * list of:
125      * <pre>
126      * name=value [;EXPIRES=dateValue] [;DOMAIN=domainName]
127      * [;PATH=path] [;SECURE]
128      * </pre>
129      *
130      * @return the string representation of the cookies being managed
131      * by this object as an http cookie response header ("Set-Cookie").
132      * null is returned if there aren't any cookies
133      * being managed by this cookie manager.
134      */

135     public String JavaDoc getResponseHeader() {
136         String JavaDoc header = null;
137         Enumeration JavaDoc e = cookies.elements();
138         while (e.hasMoreElements()) {
139             Cookie JavaDoc c = (Cookie JavaDoc)e.nextElement();
140             if (header == null)
141                 header = c.toString();
142             else
143                 header += ", " + c.toString();
144         }
145         return header;
146     }
147
148     /**
149      * Merges the cookies contained in the specified http response header
150      * into the set of cookies being managed by this object.
151      *
152      * @param header the Set-Cookie http response header to merge.
153      */

154     public synchronized void mergeHeader(String JavaDoc header) {
155         if (header == null) {
156             return;
157         }
158         Vector JavaDoc newCookies = HttpCookieParser.parseResponseHeader(header);
159         Cookie JavaDoc [] merge = new Cookie JavaDoc[cookies.size()];
160         Cookie JavaDoc [] mergeNew = new Cookie JavaDoc[newCookies.size()];
161         cookies.copyInto(merge);
162         newCookies.copyInto(mergeNew);
163
164         //
165
// Remove from the original set any cookies that are
166
// redefined in the new set.
167
//
168
for (int idx=0; idx<merge.length; idx++) {
169             for (int idxNew=0; idxNew<mergeNew.length; idxNew++) {
170                 if (testEquality(merge[idx], mergeNew[idxNew])) {
171                     merge[idx] = null;
172                     break;
173                 }
174             }
175         }
176  
177         //
178
// Reconstruct the cookie set.
179
//
180
cookies = new Vector JavaDoc();
181         for (int idx=0; idx<merge.length; idx++) {
182             if (merge[idx] != null)
183                 cookies.addElement(merge[idx]);
184         }
185         for (int idx=0; idx<mergeNew.length; idx++) {
186             cookies.addElement(mergeNew[idx]);
187         }
188     }
189
190     /**
191      * Returns an array of the cookies being managed.
192      *
193      * @return enumeration of javax.servlet.http.Cookie
194      * objects.
195      * @see javax.servlet.http.Cookie
196      */

197     public Cookie JavaDoc[] getCookies() {
198         Cookie JavaDoc[] cookieTab = new Cookie JavaDoc[cookies.size()];
199         cookies.copyInto(cookieTab);
200         return cookieTab;
201     }
202
203     /**
204      * Removes all cookies from this manager.
205      */

206     public synchronized void clear() {
207         cookies.setSize(0);
208     }
209
210     /**
211      * Returns the string representation of the headers/cookies being managed
212      * by this object as an http cookie response header (i.e. Set-Cookie
213      * header).
214      *
215      * @return the string representation of the cookies being managed
216      * by this object as an http cookie response header (i.e. Set-Cookie
217      * header).
218      */

219     public String JavaDoc toString() {
220         return getResponseHeader();
221     }
222
223     /**
224      * Compares two cookies. If the name, path, and domain are the
225      * same then the cookies are equal equal.
226      *
227      * @return true if the cookies are equal.
228      */

229
230     private boolean testEquality(Cookie JavaDoc c1, Cookie JavaDoc c2) {
231         String JavaDoc name1 = c1.getName();
232         String JavaDoc name2 = c2.getName();
233         if ((name1 == null) && (name2 != null))
234             return false;
235         if ((name1 != null) && (name2 == null))
236             return false;
237         if ((name1 != null) && (name2 != null) && !name1.equals(name2))
238             return false;
239
240         String JavaDoc path1 = c1.getPath();
241         String JavaDoc path2 = c2.getPath();
242         if ((path1 == null) && (path2 != null))
243             return false;
244         if ((path1 != null) && (path2 == null))
245             return false;
246         if ((path1 != null) && (path2 != null) && !path1.equals(path2))
247             return false;
248
249         String JavaDoc domain1 = c1.getDomain();
250         String JavaDoc domain2 = c2.getDomain();
251         if ((domain1 == null) && (domain2 != null))
252             return false;
253         if ((domain1 != null) && (domain2 == null))
254             return false;
255         if ((domain1 != null) && (domain2 != null) && !domain1.equals(domain2))
256             return false;
257
258         return true;
259     }
260 }
261
Popular Tags