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.impl.cookie;
29  
30  import java.util.Collection;
31  import java.util.HashMap;
32  import java.util.Map;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import org.apache.http.annotation.ThreadSafe;
36  import org.apache.http.cookie.CommonCookieAttributeHandler;
37  import org.apache.http.cookie.CookieAttributeHandler;
38  import org.apache.http.cookie.CookieSpec;
39  import org.apache.http.util.Args;
40  import org.apache.http.util.Asserts;
41  
42  /**
43   * Abstract cookie specification which can delegate the job of parsing,
44   * validation or matching cookie attributes to a number of arbitrary
45   * {@link CookieAttributeHandler}s.
46   *
47   *
48   * @since 4.0
49   */
50  @ThreadSafe
51  public abstract class AbstractCookieSpec implements CookieSpec {
52  
53      /**
54      * Stores attribute name -> attribute handler mappings
55      */
56      private final Map<String, CookieAttributeHandler> attribHandlerMap;
57  
58      /**
59       * Default constructor
60       * */
61      public AbstractCookieSpec() {
62          super();
63          this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(10);
64      }
65  
66      /**
67       * @since 4.4
68       */
69      protected AbstractCookieSpec(final HashMap<String, CookieAttributeHandler> map) {
70          super();
71          Asserts.notNull(map, "Attribute handler map");
72          this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(map);
73      }
74  
75      /**
76       * @since 4.4
77       */
78      protected AbstractCookieSpec(final CommonCookieAttributeHandler... handlers) {
79          super();
80          this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(handlers.length);
81          for (CommonCookieAttributeHandler handler: handlers) {
82              this.attribHandlerMap.put(handler.getAttributeName(), handler);
83          }
84      }
85  
86      /**
87       * @deprecated (4.4) use {@link #AbstractCookieSpec(java.util.HashMap)} or
88       *  {@link #AbstractCookieSpec(org.apache.http.cookie.CommonCookieAttributeHandler...)}
89       *  constructors instead.
90       */
91      @Deprecated
92      public void registerAttribHandler(
93              final String name, final CookieAttributeHandler handler) {
94          Args.notNull(name, "Attribute name");
95          Args.notNull(handler, "Attribute handler");
96          this.attribHandlerMap.put(name, handler);
97      }
98  
99      /**
100      * Finds an attribute handler {@link CookieAttributeHandler} for the
101      * given attribute. Returns {@code null} if no attribute handler is
102      * found for the specified attribute.
103      *
104      * @param name attribute name. e.g. Domain, Path, etc.
105      * @return an attribute handler or {@code null}
106      */
107     protected CookieAttributeHandler findAttribHandler(final String name) {
108         return this.attribHandlerMap.get(name);
109     }
110 
111     /**
112      * Gets attribute handler {@link CookieAttributeHandler} for the
113      * given attribute.
114      *
115      * @param name attribute name. e.g. Domain, Path, etc.
116      * @throws IllegalStateException if handler not found for the
117      *          specified attribute.
118      */
119     protected CookieAttributeHandler getAttribHandler(final String name) {
120         final CookieAttributeHandler handler = findAttribHandler(name);
121         Asserts.check(handler != null, "Handler not registered for " +
122                 name + " attribute");
123         return handler;
124     }
125 
126     protected Collection<CookieAttributeHandler> getAttribHandlers() {
127         return this.attribHandlerMap.values();
128     }
129 
130 }