1 /* 2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/cookie/CookieSpecRegistry.java $ 3 * $Revision: 652950 $ 4 * $Date: 2008-05-02 16:49:48 -0700 (Fri, 02 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.cookie; 33 34 import java.util.ArrayList; 35 import java.util.LinkedHashMap; 36 import java.util.List; 37 import java.util.Locale; 38 import java.util.Map; 39 40 import org.apache.http.params.HttpParams; 41 42 /** 43 * Cookie specification registry that can be used to obtain the corresponding 44 * cookie specification implementation for a given type of type or version of 45 * cookie. 46 * 47 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 49 * 50 * @since 4.0 51 */ 52 public final class CookieSpecRegistry { 53 54 private final Map<String,CookieSpecFactory> registeredSpecs; 55 CookieSpecRegistry()56 public CookieSpecRegistry() { 57 super(); 58 this.registeredSpecs = new LinkedHashMap<String,CookieSpecFactory>(); 59 } 60 61 /** 62 * Registers a {@link CookieSpecFactory} with the given identifier. 63 * If a specification with the given name already exists it will be overridden. 64 * This nameis the same one used to retrieve the {@link CookieSpecFactory} 65 * from {@link #getCookieSpec(String)}. 66 * 67 * @param name the identifier for this specification 68 * @param factory the {@link CookieSpecFactory} class to register 69 * 70 * @see #getCookieSpec(String) 71 */ register(final String name, final CookieSpecFactory factory)72 public synchronized void register(final String name, final CookieSpecFactory factory) { 73 if (name == null) { 74 throw new IllegalArgumentException("Name may not be null"); 75 } 76 if (factory == null) { 77 throw new IllegalArgumentException("Cookie spec factory may not be null"); 78 } 79 registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory); 80 } 81 82 /** 83 * Unregisters the {@link CookieSpecFactory} with the given ID. 84 * 85 * @param id the identifier of the {@link CookieSpec cookie specification} to unregister 86 */ unregister(final String id)87 public synchronized void unregister(final String id) { 88 if (id == null) { 89 throw new IllegalArgumentException("Id may not be null"); 90 } 91 registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH)); 92 } 93 94 /** 95 * Gets the {@link CookieSpec cookie specification} with the given ID. 96 * 97 * @param name the {@link CookieSpec cookie specification} identifier 98 * @param params the {@link HttpParams HTTP parameters} for the cookie 99 * specification. 100 * 101 * @return {@link CookieSpec cookie specification} 102 * 103 * @throws IllegalStateException if a policy with the given name cannot be found 104 */ getCookieSpec(final String name, final HttpParams params)105 public synchronized CookieSpec getCookieSpec(final String name, final HttpParams params) 106 throws IllegalStateException { 107 108 if (name == null) { 109 throw new IllegalArgumentException("Name may not be null"); 110 } 111 CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH)); 112 if (factory != null) { 113 return factory.newInstance(params); 114 } else { 115 throw new IllegalStateException("Unsupported cookie spec: " + name); 116 } 117 } 118 119 /** 120 * Gets the {@link CookieSpec cookie specification} with the given name. 121 * 122 * @param name the {@link CookieSpec cookie specification} identifier 123 * 124 * @return {@link CookieSpec cookie specification} 125 * 126 * @throws IllegalStateException if a policy with the given name cannot be found 127 */ getCookieSpec(final String name)128 public synchronized CookieSpec getCookieSpec(final String name) 129 throws IllegalStateException { 130 return getCookieSpec(name, null); 131 } 132 133 /** 134 * Obtains a list containing names of all registered {@link CookieSpec cookie 135 * specs} in their default order. 136 * 137 * Note that the DEFAULT policy (if present) is likely to be the same 138 * as one of the other policies, but does not have to be. 139 * 140 * @return list of registered cookie spec names 141 */ getSpecNames()142 public synchronized List<String> getSpecNames(){ 143 return new ArrayList<String>(registeredSpecs.keySet()); 144 } 145 146 /** 147 * Populates the internal collection of registered {@link CookieSpec cookie 148 * specs} with the content of the map passed as a parameter. 149 * 150 * @param map cookie specs 151 */ setItems(final Map<String, CookieSpecFactory> map)152 public synchronized void setItems(final Map<String, CookieSpecFactory> map) { 153 if (map == null) { 154 return; 155 } 156 registeredSpecs.clear(); 157 registeredSpecs.putAll(map); 158 } 159 160 } 161