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.Collection; 031import java.util.HashMap; 032import java.util.Map; 033import java.util.concurrent.ConcurrentHashMap; 034 035import org.apache.http.annotation.Contract; 036import org.apache.http.annotation.ThreadingBehavior; 037import org.apache.http.cookie.CommonCookieAttributeHandler; 038import org.apache.http.cookie.CookieAttributeHandler; 039import org.apache.http.cookie.CookieSpec; 040import org.apache.http.util.Args; 041import org.apache.http.util.Asserts; 042 043/** 044 * Abstract cookie specification which can delegate the job of parsing, 045 * validation or matching cookie attributes to a number of arbitrary 046 * {@link CookieAttributeHandler}s. 047 * 048 * 049 * @since 4.0 050 */ 051@Contract(threading = ThreadingBehavior.SAFE) 052public abstract class AbstractCookieSpec implements CookieSpec { 053 054 /** 055 * Stores attribute name -> attribute handler mappings 056 */ 057 private final Map<String, CookieAttributeHandler> attribHandlerMap; 058 059 /** 060 * Default constructor 061 * */ 062 public AbstractCookieSpec() { 063 super(); 064 this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(10); 065 } 066 067 /** 068 * @since 4.4 069 */ 070 protected AbstractCookieSpec(final HashMap<String, CookieAttributeHandler> map) { 071 super(); 072 Asserts.notNull(map, "Attribute handler map"); 073 this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(map); 074 } 075 076 /** 077 * @since 4.4 078 */ 079 protected AbstractCookieSpec(final CommonCookieAttributeHandler... handlers) { 080 super(); 081 this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(handlers.length); 082 for (final CommonCookieAttributeHandler handler: handlers) { 083 this.attribHandlerMap.put(handler.getAttributeName(), handler); 084 } 085 } 086 087 /** 088 * @deprecated (4.4) use {@link #AbstractCookieSpec(java.util.HashMap)} or 089 * {@link #AbstractCookieSpec(org.apache.http.cookie.CommonCookieAttributeHandler...)} 090 * constructors instead. 091 */ 092 @Deprecated 093 public void registerAttribHandler( 094 final String name, final CookieAttributeHandler handler) { 095 Args.notNull(name, "Attribute name"); 096 Args.notNull(handler, "Attribute handler"); 097 this.attribHandlerMap.put(name, handler); 098 } 099 100 /** 101 * Finds an attribute handler {@link CookieAttributeHandler} for the 102 * given attribute. Returns {@code null} if no attribute handler is 103 * found for the specified attribute. 104 * 105 * @param name attribute name. e.g. Domain, Path, etc. 106 * @return an attribute handler or {@code null} 107 */ 108 protected CookieAttributeHandler findAttribHandler(final String name) { 109 return this.attribHandlerMap.get(name); 110 } 111 112 /** 113 * Gets attribute handler {@link CookieAttributeHandler} for the 114 * given attribute. 115 * 116 * @param name attribute name. e.g. Domain, Path, etc. 117 * @throws IllegalStateException if handler not found for the 118 * specified attribute. 119 */ 120 protected CookieAttributeHandler getAttribHandler(final String name) { 121 final CookieAttributeHandler handler = findAttribHandler(name); 122 Asserts.check(handler != null, "Handler not registered for " + 123 name + " attribute"); 124 return handler; 125 } 126 127 protected Collection<CookieAttributeHandler> getAttribHandlers() { 128 return this.attribHandlerMap.values(); 129 } 130 131}