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 public abstract class CookieSpecBase extends AbstractCookieSpec { 53 54 protected static String getDefaultPath(final CookieOrigin origin) { 55 String defaultPath = origin.getPath(); 56 int lastSlashIndex = defaultPath.lastIndexOf('/'); 57 if (lastSlashIndex >= 0) { 58 if (lastSlashIndex == 0) { 59 //Do not remove the very first slash 60 lastSlashIndex = 1; 61 } 62 defaultPath = defaultPath.substring(0, lastSlashIndex); 63 } 64 return defaultPath; 65 } 66 67 protected static String getDefaultDomain(final CookieOrigin origin) { 68 return origin.getHost(); 69 } 70 71 protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) 72 throws MalformedCookieException { 73 List<Cookie> cookies = new ArrayList<Cookie>(elems.length); 74 for (HeaderElement headerelement : elems) { 75 String name = headerelement.getName(); 76 String value = headerelement.getValue(); 77 if (name == null || name.length() == 0) { 78 throw new MalformedCookieException("Cookie name may not be empty"); 79 } 80 81 BasicClientCookie cookie = new BasicClientCookie(name, value); 82 cookie.setPath(getDefaultPath(origin)); 83 cookie.setDomain(getDefaultDomain(origin)); 84 85 // cycle through the parameters 86 NameValuePair[] attribs = headerelement.getParameters(); 87 for (int j = attribs.length - 1; j >= 0; j--) { 88 NameValuePair attrib = attribs[j]; 89 String s = attrib.getName().toLowerCase(Locale.ENGLISH); 90 91 cookie.setAttribute(s, attrib.getValue()); 92 93 CookieAttributeHandler handler = findAttribHandler(s); 94 if (handler != null) { 95 handler.parse(cookie, attrib.getValue()); 96 } 97 } 98 cookies.add(cookie); 99 } 100 return cookies; 101 } 102 103 public void validate(final Cookie cookie, final CookieOrigin origin) 104 throws MalformedCookieException { 105 if (cookie == null) { 106 throw new IllegalArgumentException("Cookie may not be null"); 107 } 108 if (origin == null) { 109 throw new IllegalArgumentException("Cookie origin may not be null"); 110 } 111 for (CookieAttributeHandler handler: getAttribHandlers()) { 112 handler.validate(cookie, origin); 113 } 114 } 115 116 public boolean match(final Cookie cookie, final CookieOrigin origin) { 117 if (cookie == null) { 118 throw new IllegalArgumentException("Cookie may not be null"); 119 } 120 if (origin == null) { 121 throw new IllegalArgumentException("Cookie origin may not be null"); 122 } 123 for (CookieAttributeHandler handler: getAttribHandlers()) { 124 if (!handler.match(cookie, origin)) { 125 return false; 126 } 127 } 128 return true; 129 } 130 131 } 132