Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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 */
28 package ch.boye.httpclientandroidlib.cookie;
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;
36 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
38 import ch.boye.httpclientandroidlib.params.HttpParams;
40 /**
41 * Cookie specification registry that can be used to obtain the corresponding
42 * cookie specification implementation for a given type of type or version of
43 * cookie.
44 *
45 *
46 * @since 4.0
47 */
48 @ThreadSafe
49 public final class CookieSpecRegistry {
51 private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs;
53 public CookieSpecRegistry() {
54 super();
55 this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>();
56 }
58 /**
59 * Registers a {@link CookieSpecFactory} with the given identifier.
60 * If a specification with the given name already exists it will be overridden.
61 * This nameis the same one used to retrieve the {@link CookieSpecFactory}
62 * from {@link #getCookieSpec(String)}.
63 *
64 * @param name the identifier for this specification
65 * @param factory the {@link CookieSpecFactory} class to register
66 *
67 * @see #getCookieSpec(String)
68 */
69 public void register(final String name, final CookieSpecFactory factory) {
70 if (name == null) {
71 throw new IllegalArgumentException("Name may not be null");
72 }
73 if (factory == null) {
74 throw new IllegalArgumentException("Cookie spec factory may not be null");
75 }
76 registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
77 }
79 /**
80 * Unregisters the {@link CookieSpecFactory} with the given ID.
81 *
82 * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
83 */
84 public void unregister(final String id) {
85 if (id == null) {
86 throw new IllegalArgumentException("Id may not be null");
87 }
88 registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
89 }
91 /**
92 * Gets the {@link CookieSpec cookie specification} with the given ID.
93 *
94 * @param name the {@link CookieSpec cookie specification} identifier
95 * @param params the {@link HttpParams HTTP parameters} for the cookie
96 * specification.
97 *
98 * @return {@link CookieSpec cookie specification}
99 *
100 * @throws IllegalStateException if a policy with the given name cannot be found
101 */
102 public CookieSpec getCookieSpec(final String name, final HttpParams params)
103 throws IllegalStateException {
105 if (name == null) {
106 throw new IllegalArgumentException("Name may not be null");
107 }
108 CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
109 if (factory != null) {
110 return factory.newInstance(params);
111 } else {
112 throw new IllegalStateException("Unsupported cookie spec: " + name);
113 }
114 }
116 /**
117 * Gets the {@link CookieSpec cookie specification} with the given name.
118 *
119 * @param name the {@link CookieSpec cookie specification} identifier
120 *
121 * @return {@link CookieSpec cookie specification}
122 *
123 * @throws IllegalStateException if a policy with the given name cannot be found
124 */
125 public CookieSpec getCookieSpec(final String name)
126 throws IllegalStateException {
127 return getCookieSpec(name, null);
128 }
130 /**
131 * Obtains a list containing the names of all registered {@link CookieSpec cookie
132 * specs}.
133 *
134 * Note that the DEFAULT policy (if present) is likely to be the same
135 * as one of the other policies, but does not have to be.
136 *
137 * @return list of registered cookie spec names
138 */
139 public List<String> getSpecNames(){
140 return new ArrayList<String>(registeredSpecs.keySet());
141 }
143 /**
144 * Populates the internal collection of registered {@link CookieSpec cookie
145 * specs} with the content of the map passed as a parameter.
146 *
147 * @param map cookie specs
148 */
149 public void setItems(final Map<String, CookieSpecFactory> map) {
150 if (map == null) {
151 return;
152 }
153 registeredSpecs.clear();
154 registeredSpecs.putAll(map);
155 }
157 }