KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > CookieTools


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.util;
20
21 import java.text.*;
22 import java.util.*;
23
24 import javax.servlet.http.Cookie JavaDoc;
25
26 // XXX use only one Date instance/request, reuse it.
27

28 /**
29  * Cookie utils - generate cookie header, etc
30  *
31  * @author Original Author Unknown
32  * @author duncan@eng.sun.com
33  */

34 public class CookieTools {
35
36     /** Return the header name to set the cookie, based on cookie
37      * version
38      */

39     public static String JavaDoc getCookieHeaderName(Cookie JavaDoc cookie) {
40         int version = cookie.getVersion();
41
42         if (version == 1) {
43             return "Set-Cookie2";
44         } else {
45             return "Set-Cookie";
46         }
47     }
48
49     /** Return the header value used to set this cookie
50      * @deprecated Use StringBuffer version
51      */

52     public static String JavaDoc getCookieHeaderValue(Cookie JavaDoc cookie) {
53         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
54         getCookieHeaderValue( cookie, buf );
55         return buf.toString();
56     }
57
58     /** Return the header value used to set this cookie
59      */

60     public static void getCookieHeaderValue(Cookie JavaDoc cookie, StringBuffer JavaDoc buf) {
61         int version = cookie.getVersion();
62
63         // this part is the same for all cookies
64

65         String JavaDoc name = cookie.getName(); // Avoid NPE on malformed cookies
66
if (name == null)
67             name = "";
68         String JavaDoc value = cookie.getValue();
69         if (value == null)
70             value = "";
71         
72         buf.append(name);
73         buf.append("=");
74         maybeQuote(version, buf, value);
75
76         // add version 1 specific information
77
if (version == 1) {
78             // Version=1 ... required
79
buf.append ("; Version=1");
80
81             // Comment=comment
82
if (cookie.getComment() != null) {
83                 buf.append ("; Comment=");
84                 maybeQuote (version, buf, cookie.getComment());
85             }
86         }
87
88         // add domain information, if present
89

90         if (cookie.getDomain() != null) {
91             buf.append("; Domain=");
92             maybeQuote (version, buf, cookie.getDomain());
93         }
94
95         // Max-Age=secs/Discard ... or use old "Expires" format
96
if (cookie.getMaxAge() >= 0) {
97             if (version == 0) {
98                 buf.append ("; Expires=");
99                 if (cookie.getMaxAge() == 0)
100                     DateTool.oldCookieFormat.format(new Date(10000), buf,
101                                                     new FieldPosition(0));
102                 else
103                     DateTool.oldCookieFormat.format
104                         (new Date( System.currentTimeMillis() +
105                                    cookie.getMaxAge() *1000L), buf,
106                          new FieldPosition(0));
107             } else {
108                 buf.append ("; Max-Age=");
109                 buf.append (cookie.getMaxAge());
110             }
111         } else if (version == 1)
112           buf.append ("; Discard");
113
114         // Path=path
115
if (cookie.getPath() != null) {
116             buf.append ("; Path=");
117             maybeQuote (version, buf, cookie.getPath());
118         }
119
120         // Secure
121
if (cookie.getSecure()) {
122           buf.append ("; Secure");
123         }
124     }
125
126     static void maybeQuote (int version, StringBuffer JavaDoc buf,
127                                     String JavaDoc value)
128     {
129         if (version == 0 || isToken (value))
130             buf.append (value);
131         else {
132             buf.append ('"');
133             buf.append (value);
134             buf.append ('"');
135         }
136     }
137
138         //
139
// from RFC 2068, token special case characters
140
//
141
private static final String JavaDoc tspecials = "()<>@,;:\\\"/[]?={} \t";
142
143     /*
144      * Return true iff the string counts as an HTTP/1.1 "token".
145      */

146     private static boolean isToken (String JavaDoc value) {
147         int len = value.length ();
148
149         for (int i = 0; i < len; i++) {
150             char c = value.charAt (i);
151
152             if (c < 0x20 || c >= 0x7f || tspecials.indexOf (c) != -1)
153               return false;
154         }
155         return true;
156     }
157
158
159 }
160
Popular Tags