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 32package org.apache.http.impl.cookie; 33 34import java.util.List; 35 36import org.apache.http.Header; 37import org.apache.http.HeaderElement; 38import org.apache.http.cookie.Cookie; 39import org.apache.http.cookie.CookieOrigin; 40import org.apache.http.cookie.CookieSpec; 41import 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 */ 51public class BestMatchSpec implements CookieSpec { 52 53 private final String[] datepatterns; 54 private final boolean oneHeader; 55 56 private RFC2965Spec strict; 57 private BrowserCompatSpec compat; 58 private NetscapeDraftSpec netscape; 59 60 public BestMatchSpec(final String[] datepatterns, boolean oneHeader) { 61 super(); 62 this.datepatterns = datepatterns; 63 this.oneHeader = oneHeader; 64 } 65 66 public BestMatchSpec() { 67 this(null, false); 68 } 69 70 private RFC2965Spec getStrict() { 71 if (this.strict == null) { 72 this.strict = new RFC2965Spec(this.datepatterns, this.oneHeader); 73 } 74 return strict; 75 } 76 77 private BrowserCompatSpec getCompat() { 78 if (this.compat == null) { 79 this.compat = new BrowserCompatSpec(this.datepatterns); 80 } 81 return compat; 82 } 83 84 private NetscapeDraftSpec getNetscape() { 85 if (this.netscape == null) { 86 String[] patterns = this.datepatterns; 87 if (patterns == null) { 88 patterns = BrowserCompatSpec.DATE_PATTERNS; 89 } 90 this.netscape = new NetscapeDraftSpec(patterns); 91 } 92 return netscape; 93 } 94 95 public List<Cookie> parse( 96 final Header header, 97 final CookieOrigin origin) throws MalformedCookieException { 98 if (header == null) { 99 throw new IllegalArgumentException("Header may not be null"); 100 } 101 if (origin == null) { 102 throw new IllegalArgumentException("Cookie origin may not be null"); 103 } 104 HeaderElement[] helems = header.getElements(); 105 boolean versioned = false; 106 boolean netscape = false; 107 for (HeaderElement helem: helems) { 108 if (helem.getParameterByName("version") != null) { 109 versioned = true; 110 } 111 if (helem.getParameterByName("expires") != null) { 112 netscape = true; 113 } 114 } 115 if (netscape) { 116 117 } 118 // Do we have a cookie with a version attribute? 119 if (versioned) { 120 return getStrict().parse(helems, origin); 121 } else if (netscape) { 122 // Need to parse the header again, 123 // because Netscape draft cannot handle 124 // comma separators 125 return getNetscape().parse(header, origin); 126 } else { 127 return getCompat().parse(helems, origin); 128 } 129 } 130 131 public void validate( 132 final Cookie cookie, 133 final CookieOrigin origin) throws MalformedCookieException { 134 if (cookie == null) { 135 throw new IllegalArgumentException("Cookie may not be null"); 136 } 137 if (origin == null) { 138 throw new IllegalArgumentException("Cookie origin may not be null"); 139 } 140 if (cookie.getVersion() > 0) { 141 getStrict().validate(cookie, origin); 142 } else { 143 getCompat().validate(cookie, origin); 144 } 145 } 146 147 public boolean match(final Cookie cookie, final CookieOrigin origin) { 148 if (cookie == null) { 149 throw new IllegalArgumentException("Cookie may not be null"); 150 } 151 if (origin == null) { 152 throw new IllegalArgumentException("Cookie origin may not be null"); 153 } 154 if (cookie.getVersion() > 0) { 155 return getStrict().match(cookie, origin); 156 } else { 157 return getCompat().match(cookie, origin); 158 } 159 } 160 161 public List<Header> formatCookies(final List<Cookie> cookies) { 162 if (cookies == null) { 163 throw new IllegalArgumentException("List of cookie may not be null"); 164 } 165 int version = Integer.MAX_VALUE; 166 for (Cookie cookie: cookies) { 167 if (cookie.getVersion() < version) { 168 version = cookie.getVersion(); 169 } 170 } 171 if (version > 0) { 172 return getStrict().formatCookies(cookies); 173 } else { 174 return getCompat().formatCookies(cookies); 175 } 176 } 177 178 public int getVersion() { 179 return getStrict().getVersion(); 180 } 181 182 public Header getVersionHeader() { 183 return getStrict().getVersionHeader(); 184 } 185 186}