001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: import java.text.*;
020: import java.util.*;
021:
022: import javax.servlet.http.Cookie;
023:
024: // XXX use only one Date instance/request, reuse it.
025:
026: /**
027: * Cookie utils - generate cookie header, etc
028: *
029: * @author Original Author Unknown
030: * @author duncan@eng.sun.com
031: */
032: public class CookieTools {
033:
034: /** Return the header name to set the cookie, based on cookie
035: * version
036: */
037: public static String getCookieHeaderName(Cookie cookie) {
038: int version = cookie.getVersion();
039:
040: if (version == 1) {
041: return "Set-Cookie2";
042: } else {
043: return "Set-Cookie";
044: }
045: }
046:
047: /** Return the header value used to set this cookie
048: * @deprecated Use StringBuffer version
049: */
050: public static String getCookieHeaderValue(Cookie cookie) {
051: StringBuffer buf = new StringBuffer();
052: getCookieHeaderValue(cookie, buf);
053: return buf.toString();
054: }
055:
056: /** Return the header value used to set this cookie
057: */
058: public static void getCookieHeaderValue(Cookie cookie,
059: StringBuffer buf) {
060: int version = cookie.getVersion();
061:
062: // this part is the same for all cookies
063:
064: String name = cookie.getName(); // Avoid NPE on malformed cookies
065: if (name == null)
066: name = "";
067: String value = cookie.getValue();
068: if (value == null)
069: value = "";
070:
071: buf.append(name);
072: buf.append("=");
073: maybeQuote(version, buf, value);
074:
075: // add version 1 specific information
076: if (version == 1) {
077: // Version=1 ... required
078: buf.append("; Version=1");
079:
080: // Comment=comment
081: if (cookie.getComment() != null) {
082: buf.append("; Comment=");
083: maybeQuote(version, buf, cookie.getComment());
084: }
085: }
086:
087: // add domain information, if present
088:
089: if (cookie.getDomain() != null) {
090: buf.append("; Domain=");
091: maybeQuote(version, buf, cookie.getDomain());
092: }
093:
094: // Max-Age=secs/Discard ... or use old "Expires" format
095: if (cookie.getMaxAge() >= 0) {
096: if (version == 0) {
097: buf.append("; Expires=");
098: if (cookie.getMaxAge() == 0)
099: DateTool.oldCookieFormat.format(new Date(10000),
100: buf, new FieldPosition(0));
101: else
102: DateTool.oldCookieFormat.format(new Date(System
103: .currentTimeMillis()
104: + cookie.getMaxAge() * 1000L), buf,
105: new FieldPosition(0));
106: } else {
107: buf.append("; Max-Age=");
108: buf.append(cookie.getMaxAge());
109: }
110: } else if (version == 1)
111: buf.append("; Discard");
112:
113: // Path=path
114: if (cookie.getPath() != null) {
115: buf.append("; Path=");
116: maybeQuote(version, buf, cookie.getPath());
117: }
118:
119: // Secure
120: if (cookie.getSecure()) {
121: buf.append("; Secure");
122: }
123: }
124:
125: static void maybeQuote(int version, StringBuffer buf, String value) {
126: if (version == 0 || isToken(value))
127: buf.append(value);
128: else {
129: buf.append('"');
130: buf.append(value);
131: buf.append('"');
132: }
133: }
134:
135: //
136: // from RFC 2068, token special case characters
137: //
138: private static final String tspecials = "()<>@,;:\\\"/[]?={} \t";
139:
140: /*
141: * Return true iff the string counts as an HTTP/1.1 "token".
142: */
143: private static boolean isToken(String value) {
144: int len = value.length();
145:
146: for (int i = 0; i < len; i++) {
147: char c = value.charAt(i);
148:
149: if (c < 0x20 || c >= 0x7f || tspecials.indexOf(c) != -1)
150: return false;
151: }
152: return true;
153: }
154:
155: }
|