001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.impl.cookie; 029 030import java.util.ArrayList; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Locale; 034 035import org.apache.http.HeaderElement; 036import org.apache.http.NameValuePair; 037import org.apache.http.annotation.Contract; 038import org.apache.http.annotation.ThreadingBehavior; 039import org.apache.http.cookie.CommonCookieAttributeHandler; 040import org.apache.http.cookie.Cookie; 041import org.apache.http.cookie.CookieAttributeHandler; 042import org.apache.http.cookie.CookieOrigin; 043import org.apache.http.cookie.MalformedCookieException; 044import org.apache.http.util.Args; 045 046/** 047 * Cookie management functions shared by all specification. 048 * 049 * @since 4.0 050 */ 051@Contract(threading = ThreadingBehavior.SAFE) 052public abstract class CookieSpecBase extends AbstractCookieSpec { 053 054 public CookieSpecBase() { 055 super(); 056 } 057 058 /** 059 * @since 4.4 060 */ 061 protected CookieSpecBase(final HashMap<String, CookieAttributeHandler> map) { 062 super(map); 063 } 064 065 /** 066 * @since 4.4 067 */ 068 protected CookieSpecBase(final CommonCookieAttributeHandler... handlers) { 069 super(handlers); 070 } 071 072 protected static String getDefaultPath(final CookieOrigin origin) { 073 String defaultPath = origin.getPath(); 074 int lastSlashIndex = defaultPath.lastIndexOf('/'); 075 if (lastSlashIndex >= 0) { 076 if (lastSlashIndex == 0) { 077 //Do not remove the very first slash 078 lastSlashIndex = 1; 079 } 080 defaultPath = defaultPath.substring(0, lastSlashIndex); 081 } 082 return defaultPath; 083 } 084 085 protected static String getDefaultDomain(final CookieOrigin origin) { 086 return origin.getHost(); 087 } 088 089 protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) 090 throws MalformedCookieException { 091 final List<Cookie> cookies = new ArrayList<Cookie>(elems.length); 092 for (final HeaderElement headerelement : elems) { 093 final String name = headerelement.getName(); 094 final String value = headerelement.getValue(); 095 if (name == null || name.isEmpty()) { 096 continue; 097 } 098 099 final BasicClientCookie cookie = new BasicClientCookie(name, value); 100 cookie.setPath(getDefaultPath(origin)); 101 cookie.setDomain(getDefaultDomain(origin)); 102 103 // cycle through the parameters 104 final NameValuePair[] attribs = headerelement.getParameters(); 105 for (int j = attribs.length - 1; j >= 0; j--) { 106 final NameValuePair attrib = attribs[j]; 107 final String s = attrib.getName().toLowerCase(Locale.ROOT); 108 109 cookie.setAttribute(s, attrib.getValue()); 110 111 final CookieAttributeHandler handler = findAttribHandler(s); 112 if (handler != null) { 113 handler.parse(cookie, attrib.getValue()); 114 } 115 } 116 cookies.add(cookie); 117 } 118 return cookies; 119 } 120 121 @Override 122 public void validate(final Cookie cookie, final CookieOrigin origin) 123 throws MalformedCookieException { 124 Args.notNull(cookie, "Cookie"); 125 Args.notNull(origin, "Cookie origin"); 126 for (final CookieAttributeHandler handler: getAttribHandlers()) { 127 handler.validate(cookie, origin); 128 } 129 } 130 131 @Override 132 public boolean match(final Cookie cookie, final CookieOrigin origin) { 133 Args.notNull(cookie, "Cookie"); 134 Args.notNull(origin, "Cookie origin"); 135 for (final CookieAttributeHandler handler: getAttribHandlers()) { 136 if (!handler.match(cookie, origin)) { 137 return false; 138 } 139 } 140 return true; 141 } 142 143}