1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/NetscapeDomainHandler.java $
3 * $Revision: 653041 $
4 * $Date: 2008-05-03 03:39:28 -0700 (Sat, 03 May 2008) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31package org.apache.http.impl.cookie;
32
33import java.util.Locale;
34import java.util.StringTokenizer;
35
36import org.apache.http.cookie.Cookie;
37import org.apache.http.cookie.CookieOrigin;
38import org.apache.http.cookie.MalformedCookieException;
39
40public class NetscapeDomainHandler extends BasicDomainHandler {
41
42    public NetscapeDomainHandler() {
43        super();
44    }
45
46    @Override
47    public void validate(final Cookie cookie, final CookieOrigin origin)
48            throws MalformedCookieException {
49        super.validate(cookie, origin);
50        // Perform Netscape Cookie draft specific validation
51        String host = origin.getHost();
52        String domain = cookie.getDomain();
53        if (host.contains(".")) {
54            int domainParts = new StringTokenizer(domain, ".").countTokens();
55
56            if (isSpecialDomain(domain)) {
57                if (domainParts < 2) {
58                    throw new MalformedCookieException("Domain attribute \""
59                        + domain
60                        + "\" violates the Netscape cookie specification for "
61                        + "special domains");
62                }
63            } else {
64                if (domainParts < 3) {
65                    throw new MalformedCookieException("Domain attribute \""
66                        + domain
67                        + "\" violates the Netscape cookie specification");
68                }
69            }
70        }
71    }
72
73   /**
74    * Checks if the given domain is in one of the seven special
75    * top level domains defined by the Netscape cookie specification.
76    * @param domain The domain.
77    * @return True if the specified domain is "special"
78    */
79   private static boolean isSpecialDomain(final String domain) {
80       final String ucDomain = domain.toUpperCase(Locale.ENGLISH);
81       return ucDomain.endsWith(".COM")
82               || ucDomain.endsWith(".EDU")
83               || ucDomain.endsWith(".NET")
84               || ucDomain.endsWith(".GOV")
85               || ucDomain.endsWith(".MIL")
86               || ucDomain.endsWith(".ORG")
87               || ucDomain.endsWith(".INT");
88   }
89
90   @Override
91public boolean match(Cookie cookie, CookieOrigin origin) {
92       if (cookie == null) {
93           throw new IllegalArgumentException("Cookie may not be null");
94       }
95       if (origin == null) {
96           throw new IllegalArgumentException("Cookie origin may not be null");
97       }
98       String host = origin.getHost();
99       String domain = cookie.getDomain();
100       if (domain == null) {
101           return false;
102       }
103       return host.endsWith(domain);
104   }
105
106}
107