View Javadoc

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.ThreadSafe;
38  import org.apache.http.config.Lookup;
39  import org.apache.http.params.HttpParams;
40  import org.apache.http.protocol.ExecutionContext;
41  import org.apache.http.protocol.HttpContext;
42  import org.apache.http.util.Args;
43  
44  /**
45   * Cookie specification registry that can be used to obtain the corresponding
46   * cookie specification implementation for a given type of type or version of
47   * cookie.
48   *
49   * @since 4.0
50   *
51   * @deprecated (4.3) use {@link org.apache.http.config.Registry}.
52   */
53  @ThreadSafe
54  @Deprecated
55  public final class CookieSpecRegistry implements Lookup<CookieSpecProvider> {
56  
57      private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs;
58  
59      public CookieSpecRegistry() {
60          super();
61          this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>();
62      }
63  
64      /**
65       * Registers a {@link CookieSpecFactory} with the given identifier.
66       * If a specification with the given name already exists it will be overridden.
67       * This nameis the same one used to retrieve the {@link CookieSpecFactory}
68       * from {@link #getCookieSpec(String)}.
69       *
70       * @param name the identifier for this specification
71       * @param factory the {@link CookieSpecFactory} class to register
72       *
73       * @see #getCookieSpec(String)
74       */
75      public void register(final String name, final CookieSpecFactory factory) {
76           Args.notNull(name, "Name");
77          Args.notNull(factory, "Cookie spec factory");
78          registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
79      }
80  
81      /**
82       * Unregisters the {@link CookieSpecFactory} with the given ID.
83       *
84       * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
85       */
86      public void unregister(final String id) {
87           Args.notNull(id, "Id");
88           registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
89      }
90  
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 {
104 
105         Args.notNull(name, "Name");
106         final CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
107         if (factory != null) {
108             return factory.newInstance(params);
109         } else {
110             throw new IllegalStateException("Unsupported cookie spec: " + name);
111         }
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     public CookieSpecProvider lookup(final String name) {
156         return new CookieSpecProvider() {
157 
158             public CookieSpec create(final HttpContext context) {
159                 final HttpRequest request = (HttpRequest) context.getAttribute(
160                         ExecutionContext.HTTP_REQUEST);
161                 return getCookieSpec(name, request.getParams());
162             }
163 
164         };
165     }
166 
167 }