1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.http.cookie; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Locale; 33 import java.util.Map; 34 import java.util.concurrent.ConcurrentHashMap; 35 36 import org.apache.http.HttpRequest; 37 import org.apache.http.annotation.Contract; 38 import org.apache.http.annotation.ThreadingBehavior; 39 import org.apache.http.config.Lookup; 40 import org.apache.http.params.HttpParams; 41 import org.apache.http.protocol.ExecutionContext; 42 import org.apache.http.protocol.HttpContext; 43 import org.apache.http.util.Args; 44 45 /** 46 * Cookie specification registry that can be used to obtain the corresponding 47 * cookie specification implementation for a given type of type or version of 48 * cookie. 49 * 50 * @since 4.0 51 * 52 * @deprecated (4.3) use {@link org.apache.http.config.Registry}. 53 */ 54 @Contract(threading = ThreadingBehavior.SAFE) 55 @Deprecated 56 public final class CookieSpecRegistry implements Lookup<CookieSpecProvider> { 57 58 private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs; 59 60 public CookieSpecRegistry() { 61 super(); 62 this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>(); 63 } 64 65 /** 66 * Registers a {@link CookieSpecFactory} with the given identifier. 67 * If a specification with the given name already exists it will be overridden. 68 * This nameis the same one used to retrieve the {@link CookieSpecFactory} 69 * from {@link #getCookieSpec(String)}. 70 * 71 * @param name the identifier for this specification 72 * @param factory the {@link CookieSpecFactory} class to register 73 * 74 * @see #getCookieSpec(String) 75 */ 76 public void register(final String name, final CookieSpecFactory factory) { 77 Args.notNull(name, "Name"); 78 Args.notNull(factory, "Cookie spec factory"); 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 */ 87 public void unregister(final String id) { 88 Args.notNull(id, "Id"); 89 registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH)); 90 } 91 92 /** 93 * Gets the {@link CookieSpec cookie specification} with the given ID. 94 * 95 * @param name the {@link CookieSpec cookie specification} identifier 96 * @param params the {@link HttpParams HTTP parameters} for the cookie 97 * specification. 98 * 99 * @return {@link CookieSpec cookie specification} 100 * 101 * @throws IllegalStateException if a policy with the given name cannot be found 102 */ 103 public CookieSpec getCookieSpec(final String name, final HttpParams params) 104 throws IllegalStateException { 105 106 Args.notNull(name, "Name"); 107 final CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH)); 108 if (factory != null) { 109 return factory.newInstance(params); 110 } 111 throw new IllegalStateException("Unsupported cookie spec: " + name); 112 } 113 114 /** 115 * Gets the {@link CookieSpec cookie specification} with the given name. 116 * 117 * @param name the {@link CookieSpec cookie specification} identifier 118 * 119 * @return {@link CookieSpec cookie specification} 120 * 121 * @throws IllegalStateException if a policy with the given name cannot be found 122 */ 123 public CookieSpec getCookieSpec(final String name) 124 throws IllegalStateException { 125 return getCookieSpec(name, null); 126 } 127 128 /** 129 * Obtains a list containing the names of all registered {@link CookieSpec cookie 130 * specs}. 131 * 132 * Note that the DEFAULT policy (if present) is likely to be the same 133 * as one of the other policies, but does not have to be. 134 * 135 * @return list of registered cookie spec names 136 */ 137 public List<String> getSpecNames(){ 138 return new ArrayList<String>(registeredSpecs.keySet()); 139 } 140 141 /** 142 * Populates the internal collection of registered {@link CookieSpec cookie 143 * specs} with the content of the map passed as a parameter. 144 * 145 * @param map cookie specs 146 */ 147 public void setItems(final Map<String, CookieSpecFactory> map) { 148 if (map == null) { 149 return; 150 } 151 registeredSpecs.clear(); 152 registeredSpecs.putAll(map); 153 } 154 155 @Override 156 public CookieSpecProvider lookup(final String name) { 157 return new CookieSpecProvider() { 158 159 @Override 160 public CookieSpec create(final HttpContext context) { 161 final HttpRequest request = (HttpRequest) context.getAttribute( 162 ExecutionContext.HTTP_REQUEST); 163 return getCookieSpec(name, request.getParams()); 164 } 165 166 }; 167 } 168 169 }