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