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.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         } else {
111             throw new IllegalStateException("Unsupported cookie spec: " + name);
112         }
113     }
114 
115     /**
116      * Gets the {@link CookieSpec cookie specification} with the given name.
117      *
118      * @param name the {@link CookieSpec cookie specification} identifier
119      *
120      * @return {@link CookieSpec cookie specification}
121      *
122      * @throws IllegalStateException if a policy with the given name cannot be found
123      */
124     public CookieSpec getCookieSpec(final String name)
125         throws IllegalStateException {
126         return getCookieSpec(name, null);
127     }
128 
129     /**
130      * Obtains a list containing the names of all registered {@link CookieSpec cookie
131      * specs}.
132      *
133      * Note that the DEFAULT policy (if present) is likely to be the same
134      * as one of the other policies, but does not have to be.
135      *
136      * @return list of registered cookie spec names
137      */
138     public List<String> getSpecNames(){
139         return new ArrayList<String>(registeredSpecs.keySet());
140     }
141 
142     /**
143      * Populates the internal collection of registered {@link CookieSpec cookie
144      * specs} with the content of the map passed as a parameter.
145      *
146      * @param map cookie specs
147      */
148     public void setItems(final Map<String, CookieSpecFactory> map) {
149         if (map == null) {
150             return;
151         }
152         registeredSpecs.clear();
153         registeredSpecs.putAll(map);
154     }
155 
156     @Override
157     public CookieSpecProvider lookup(final String name) {
158         return new CookieSpecProvider() {
159 
160             @Override
161             public CookieSpec create(final HttpContext context) {
162                 final HttpRequest request = (HttpRequest) context.getAttribute(
163                         ExecutionContext.HTTP_REQUEST);
164                 return getCookieSpec(name, request.getParams());
165             }
166 
167         };
168     }
169 
170 }