• 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/BestMatchSpec.java $
3  * $Revision: 657334 $
4  * $Date: 2008-05-17 04:44:16 -0700 (Sat, 17 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.List;
35 
36 import org.apache.http.Header;
37 import org.apache.http.HeaderElement;
38 import org.apache.http.cookie.Cookie;
39 import org.apache.http.cookie.CookieOrigin;
40 import org.apache.http.cookie.CookieSpec;
41 import org.apache.http.cookie.MalformedCookieException;
42 
43 /**
44  * 'Meta' cookie specification that selects a cookie policy depending
45  * on the format of the cookie(s)
46  *
47  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48  *
49  * @since 4.0
50  *
51  * @deprecated Please use {@link java.net.URL#openConnection} instead.
52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
53  *     for further details.
54  */
55 @Deprecated
56 public class BestMatchSpec implements CookieSpec {
57 
58     private final String[] datepatterns;
59     private final boolean oneHeader;
60 
61     private RFC2965Spec strict;
62     private BrowserCompatSpec compat;
63     private NetscapeDraftSpec netscape;
64 
BestMatchSpec(final String[] datepatterns, boolean oneHeader)65     public BestMatchSpec(final String[] datepatterns, boolean oneHeader) {
66         super();
67         this.datepatterns = datepatterns;
68         this.oneHeader = oneHeader;
69     }
70 
BestMatchSpec()71     public BestMatchSpec() {
72         this(null, false);
73     }
74 
getStrict()75     private RFC2965Spec getStrict() {
76         if (this.strict == null) {
77              this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader);
78         }
79         return strict;
80     }
81 
getCompat()82     private BrowserCompatSpec getCompat() {
83         if (this.compat == null) {
84             this.compat = new BrowserCompatSpec(this.datepatterns);
85         }
86         return compat;
87     }
88 
getNetscape()89     private NetscapeDraftSpec getNetscape() {
90         if (this.netscape == null) {
91             String[] patterns = this.datepatterns;
92             if (patterns == null) {
93                 patterns = BrowserCompatSpec.DATE_PATTERNS;
94             }
95             this.netscape = new NetscapeDraftSpec(patterns);
96         }
97         return netscape;
98     }
99 
parse( final Header header, final CookieOrigin origin)100     public List<Cookie> parse(
101             final Header header,
102             final CookieOrigin origin) throws MalformedCookieException {
103         if (header == null) {
104             throw new IllegalArgumentException("Header may not be null");
105         }
106         if (origin == null) {
107            throw new IllegalArgumentException("Cookie origin may not be null");
108         }
109         HeaderElement[] helems = header.getElements();
110         boolean versioned = false;
111         boolean netscape = false;
112         for (HeaderElement helem: helems) {
113             if (helem.getParameterByName("version") != null) {
114                 versioned = true;
115             }
116             if (helem.getParameterByName("expires") != null) {
117                netscape = true;
118             }
119         }
120         if (netscape) {
121 
122         }
123         // Do we have a cookie with a version attribute?
124         if (versioned) {
125             return getStrict().parse(helems, origin);
126         } else if (netscape) {
127             // Need to parse the header again,
128             // because Netscape draft cannot handle
129             // comma separators
130             return getNetscape().parse(header, origin);
131         } else {
132             return getCompat().parse(helems, origin);
133         }
134     }
135 
validate( final Cookie cookie, final CookieOrigin origin)136     public void validate(
137             final Cookie cookie,
138             final CookieOrigin origin) throws MalformedCookieException {
139         if (cookie == null) {
140             throw new IllegalArgumentException("Cookie may not be null");
141         }
142         if (origin == null) {
143             throw new IllegalArgumentException("Cookie origin may not be null");
144         }
145         if (cookie.getVersion() > 0) {
146             getStrict().validate(cookie, origin);
147         } else {
148             getCompat().validate(cookie, origin);
149         }
150     }
151 
match(final Cookie cookie, final CookieOrigin origin)152     public boolean match(final Cookie cookie, final CookieOrigin origin) {
153         if (cookie == null) {
154             throw new IllegalArgumentException("Cookie may not be null");
155         }
156         if (origin == null) {
157             throw new IllegalArgumentException("Cookie origin may not be null");
158         }
159         if (cookie.getVersion() > 0) {
160             return getStrict().match(cookie, origin);
161         } else {
162             return getCompat().match(cookie, origin);
163         }
164     }
165 
formatCookies(final List<Cookie> cookies)166     public List<Header> formatCookies(final List<Cookie> cookies) {
167         if (cookies == null) {
168             throw new IllegalArgumentException("List of cookie may not be null");
169         }
170         int version = Integer.MAX_VALUE;
171         for (Cookie cookie: cookies) {
172             if (cookie.getVersion() < version) {
173                 version = cookie.getVersion();
174             }
175         }
176         if (version > 0) {
177             return getStrict().formatCookies(cookies);
178         } else {
179             return getCompat().formatCookies(cookies);
180         }
181     }
182 
getVersion()183     public int getVersion() {
184         return getStrict().getVersion();
185     }
186 
getVersionHeader()187     public Header getVersionHeader() {
188         return getStrict().getVersionHeader();
189     }
190 
191 }