KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > cookie > NetscapeDraftSpec


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v 1.7.2.1 2004/02/22 18:21:15 olegk Exp $
3  * $Revision: 1.7.2.1 $
4  * $Date: 2004/02/22 18:21:15 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.cookie;
33
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.Locale JavaDoc;
37 import java.text.DateFormat JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39 import java.text.ParseException JavaDoc;
40 import org.apache.commons.httpclient.NameValuePair;
41 import org.apache.commons.httpclient.Cookie;
42
43 /**
44  * <P>Netscape cookie draft specific cookie management functions
45  *
46  * @author B.C. Holmes
47  * @author <a HREF="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
48  * @author <a HREF="mailto:dsale@us.britannica.com">Doug Sale</a>
49  * @author Rod Waldhoff
50  * @author dIon Gillard
51  * @author Sean C. Sullivan
52  * @author <a HREF="mailto:JEvans@Cyveillance.com">John Evans</a>
53  * @author Marc A. Saegesser
54  * @author <a HREF="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
55  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
56  *
57  * @since 2.0
58  */

59
60 public class NetscapeDraftSpec extends CookieSpecBase {
61
62     /** Default constructor */
63     public NetscapeDraftSpec() {
64         super();
65     }
66
67
68     /**
69       * Parse the cookie attribute and update the corresponsing {@link Cookie}
70       * properties as defined by the Netscape draft specification
71       *
72       * @param attribute {@link NameValuePair} cookie attribute from the
73       * <tt>Set- Cookie</tt>
74       * @param cookie {@link Cookie} to be updated
75       * @throws MalformedCookieException if an exception occurs during parsing
76       */

77     public void parseAttribute(
78         final NameValuePair attribute, final Cookie cookie)
79         throws MalformedCookieException {
80             
81         if (attribute == null) {
82             throw new IllegalArgumentException JavaDoc("Attribute may not be null.");
83         }
84         if (cookie == null) {
85             throw new IllegalArgumentException JavaDoc("Cookie may not be null.");
86         }
87         final String JavaDoc paramName = attribute.getName().toLowerCase();
88         final String JavaDoc paramValue = attribute.getValue();
89
90         if (paramName.equals("expires")) {
91
92             if (paramValue == null) {
93                 throw new MalformedCookieException(
94                     "Missing value for expires attribute");
95             }
96             try {
97                 DateFormat JavaDoc expiryFormat = new SimpleDateFormat JavaDoc(
98                     "EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
99                 Date JavaDoc date = expiryFormat.parse(paramValue);
100                 cookie.setExpiryDate(date);
101             } catch (ParseException JavaDoc e) {
102                 throw new MalformedCookieException("Invalid expires "
103                     + "attribute: " + e.getMessage());
104             }
105         } else {
106             super.parseAttribute(attribute, cookie);
107         }
108     }
109
110     /**
111       * Performs Netscape draft compliant {@link Cookie} validation
112       *
113       * @param host the host from which the {@link Cookie} was received
114       * @param port the port from which the {@link Cookie} was received
115       * @param path the path from which the {@link Cookie} was received
116       * @param secure <tt>true</tt> when the {@link Cookie} was received
117       * using a secure connection
118       * @param cookie The cookie to validate.
119       * @throws MalformedCookieException if an exception occurs during
120       * validation
121       */

122     public void validate(String JavaDoc host, int port, String JavaDoc path,
123         boolean secure, final Cookie cookie)
124         throws MalformedCookieException {
125             
126         LOG.trace("enterNetscapeDraftCookieProcessor "
127             + "RCF2109CookieProcessor.validate(Cookie)");
128         // Perform generic validation
129
super.validate(host, port, path, secure, cookie);
130         // Perform Netscape Cookie draft specific validation
131
if (host.indexOf(".") >= 0) {
132             int domainParts = new StringTokenizer JavaDoc(cookie.getDomain(), ".")
133                 .countTokens();
134
135             if (isSpecialDomain(cookie.getDomain())) {
136                 if (domainParts < 2) {
137                     throw new MalformedCookieException("Domain attribute \""
138                         + cookie.getDomain()
139                         + "\" violates the Netscape cookie specification for "
140                         + "special domains");
141                 }
142             } else {
143                 if (domainParts < 3) {
144                     throw new MalformedCookieException("Domain attribute \""
145                         + cookie.getDomain()
146                         + "\" violates the Netscape cookie specification");
147                 }
148             }
149         }
150     }
151     
152     /**
153      * Checks if the given domain is in one of the seven special
154      * top level domains defined by the Netscape cookie specification.
155      * @param domain The domain.
156      * @return True if the specified domain is "special"
157      */

158     private static boolean isSpecialDomain(final String JavaDoc domain) {
159         final String JavaDoc ucDomain = domain.toUpperCase();
160         if (ucDomain.endsWith(".COM")
161            || ucDomain.endsWith(".EDU")
162            || ucDomain.endsWith(".NET")
163            || ucDomain.endsWith(".GOV")
164            || ucDomain.endsWith(".MIL")
165            || ucDomain.endsWith(".ORG")
166            || ucDomain.endsWith(".INT")) {
167             return true;
168         }
169         return false;
170     }
171 }
172
Popular Tags