• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/CookieSpecBase.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  */
31 
32 package org.apache.http.impl.cookie;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Locale;
37 
38 import org.apache.http.HeaderElement;
39 import org.apache.http.NameValuePair;
40 import org.apache.http.cookie.Cookie;
41 import org.apache.http.cookie.CookieAttributeHandler;
42 import org.apache.http.cookie.CookieOrigin;
43 import org.apache.http.cookie.MalformedCookieException;
44 
45 /**
46  * Cookie management functions shared by all specification.
47  *
48  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
49  *
50  * @since 4.0
51  *
52  * @deprecated Please use {@link java.net.URL#openConnection} instead.
53  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
54  *     for further details.
55  */
56 @Deprecated
57 public abstract class CookieSpecBase extends AbstractCookieSpec {
58 
getDefaultPath(final CookieOrigin origin)59     protected static String getDefaultPath(final CookieOrigin origin) {
60         String defaultPath = origin.getPath();
61         int lastSlashIndex = defaultPath.lastIndexOf('/');
62         if (lastSlashIndex >= 0) {
63             if (lastSlashIndex == 0) {
64                 //Do not remove the very first slash
65                 lastSlashIndex = 1;
66             }
67             defaultPath = defaultPath.substring(0, lastSlashIndex);
68         }
69         return defaultPath;
70     }
71 
getDefaultDomain(final CookieOrigin origin)72     protected static String getDefaultDomain(final CookieOrigin origin) {
73         return origin.getHost();
74     }
75 
parse(final HeaderElement[] elems, final CookieOrigin origin)76     protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin)
77                 throws MalformedCookieException {
78         List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
79         for (HeaderElement headerelement : elems) {
80             String name = headerelement.getName();
81             String value = headerelement.getValue();
82             if (name == null || name.length() == 0) {
83                 throw new MalformedCookieException("Cookie name may not be empty");
84             }
85 
86             BasicClientCookie cookie = new BasicClientCookie(name, value);
87             cookie.setPath(getDefaultPath(origin));
88             cookie.setDomain(getDefaultDomain(origin));
89 
90             // cycle through the parameters
91             NameValuePair[] attribs = headerelement.getParameters();
92             for (int j = attribs.length - 1; j >= 0; j--) {
93                 NameValuePair attrib = attribs[j];
94                 String s = attrib.getName().toLowerCase(Locale.ENGLISH);
95 
96                 cookie.setAttribute(s, attrib.getValue());
97 
98                 CookieAttributeHandler handler = findAttribHandler(s);
99                 if (handler != null) {
100                     handler.parse(cookie, attrib.getValue());
101                 }
102             }
103             cookies.add(cookie);
104         }
105         return cookies;
106     }
107 
validate(final Cookie cookie, final CookieOrigin origin)108     public void validate(final Cookie cookie, final CookieOrigin origin)
109             throws MalformedCookieException {
110         if (cookie == null) {
111             throw new IllegalArgumentException("Cookie may not be null");
112         }
113         if (origin == null) {
114             throw new IllegalArgumentException("Cookie origin may not be null");
115         }
116         for (CookieAttributeHandler handler: getAttribHandlers()) {
117             handler.validate(cookie, origin);
118         }
119     }
120 
match(final Cookie cookie, final CookieOrigin origin)121     public boolean match(final Cookie cookie, final CookieOrigin origin) {
122         if (cookie == null) {
123             throw new IllegalArgumentException("Cookie may not be null");
124         }
125         if (origin == null) {
126             throw new IllegalArgumentException("Cookie origin may not be null");
127         }
128         for (CookieAttributeHandler handler: getAttribHandlers()) {
129             if (!handler.match(cookie, origin)) {
130                 return false;
131             }
132         }
133         return true;
134     }
135 
136 }
137