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/**
40 * @deprecated Please use {@link java.net.URL#openConnection} instead.
41 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
42 *     for further details.
43*/
44
45@Deprecated
46public class NetscapeDomainHandler extends BasicDomainHandler {
47
48    public NetscapeDomainHandler() {
49        super();
50    }
51
52    @Override
53    public void validate(final Cookie cookie, final CookieOrigin origin)
54            throws MalformedCookieException {
55        super.validate(cookie, origin);
56        // Perform Netscape Cookie draft specific validation
57        String host = origin.getHost();
58        String domain = cookie.getDomain();
59        if (host.contains(".")) {
60            int domainParts = new StringTokenizer(domain, ".").countTokens();
61
62            if (isSpecialDomain(domain)) {
63                if (domainParts < 2) {
64                    throw new MalformedCookieException("Domain attribute \""
65                        + domain
66                        + "\" violates the Netscape cookie specification for "
67                        + "special domains");
68                }
69            } else {
70                if (domainParts < 3) {
71                    throw new MalformedCookieException("Domain attribute \""
72                        + domain
73                        + "\" violates the Netscape cookie specification");
74                }
75            }
76        }
77    }
78
79   /**
80    * Checks if the given domain is in one of the seven special
81    * top level domains defined by the Netscape cookie specification.
82    * @param domain The domain.
83    * @return True if the specified domain is "special"
84    */
85   private static boolean isSpecialDomain(final String domain) {
86       final String ucDomain = domain.toUpperCase(Locale.ENGLISH);
87       return ucDomain.endsWith(".COM")
88               || ucDomain.endsWith(".EDU")
89               || ucDomain.endsWith(".NET")
90               || ucDomain.endsWith(".GOV")
91               || ucDomain.endsWith(".MIL")
92               || ucDomain.endsWith(".ORG")
93               || ucDomain.endsWith(".INT");
94   }
95
96   @Override
97public boolean match(Cookie cookie, CookieOrigin origin) {
98       if (cookie == null) {
99           throw new IllegalArgumentException("Cookie may not be null");
100       }
101       if (origin == null) {
102           throw new IllegalArgumentException("Cookie origin may not be null");
103       }
104       String host = origin.getHost();
105       String domain = cookie.getDomain();
106       if (domain == null) {
107           return false;
108       }
109       return host.endsWith(domain);
110   }
111
112}
113