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
34 import org.apache.http.annotation.NotThreadSafe;
35 import org.apache.http.cookie.CookieAttributeHandler;
36 import org.apache.http.cookie.CookieSpec;
37 import org.apache.http.util.Args;
38
39 /**
40 * Abstract cookie specification which can delegate the job of parsing,
41 * validation or matching cookie attributes to a number of arbitrary
42 * {@link CookieAttributeHandler}s.
43 *
44 *
45 * @since 4.0
46 */
47 @NotThreadSafe // HashMap is not thread-safe
48 public abstract class AbstractCookieSpec implements CookieSpec {
49
50 /**
51 * Stores attribute name -> attribute handler mappings
52 */
53 private final Map<String, CookieAttributeHandler> attribHandlerMap;
54
55 /**
56 * Default constructor
57 * */
58 public AbstractCookieSpec() {
59 super();
60 this.attribHandlerMap = new HashMap<String, CookieAttributeHandler>(10);
61 }
62
63 public void registerAttribHandler(
64 final String name, final CookieAttributeHandler handler) {
65 Args.notNull(name, "Attribute name");
66 Args.notNull(handler, "Attribute handler");
67 this.attribHandlerMap.put(name, handler);
68 }
69
70 /**
71 * Finds an attribute handler {@link CookieAttributeHandler} for the
72 * given attribute. Returns <tt>null</tt> if no attribute handler is
73 * found for the specified attribute.
74 *
75 * @param name attribute name. e.g. Domain, Path, etc.
76 * @return an attribute handler or <tt>null</tt>
77 */
78 protected CookieAttributeHandler findAttribHandler(final String name) {
79 return this.attribHandlerMap.get(name);
80 }
81
82 /**
83 * Gets attribute handler {@link CookieAttributeHandler} for the
84 * given attribute.
85 *
86 * @param name attribute name. e.g. Domain, Path, etc.
87 * @throws IllegalStateException if handler not found for the
88 * specified attribute.
89 */
90 protected CookieAttributeHandler getAttribHandler(final String name) {
91 final CookieAttributeHandler handler = findAttribHandler(name);
92 if (handler == null) {
93 throw new IllegalStateException("Handler not registered for " +
94 name + " attribute.");
95 } else {
96 return handler;
97 }
98 }
99
100 protected Collection<CookieAttributeHandler> getAttribHandlers() {
101 return this.attribHandlerMap.values();
102 }
103
104 }