|
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 ch.boye.httpclientandroidlib.impl.cookie; |
|
29 |
|
30 import java.util.Collection; |
|
31 import java.util.HashMap; |
|
32 import java.util.Map; |
|
33 |
|
34 import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
|
35 |
|
36 import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler; |
|
37 import ch.boye.httpclientandroidlib.cookie.CookieSpec; |
|
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 if (name == null) { |
|
66 throw new IllegalArgumentException("Attribute name may not be null"); |
|
67 } |
|
68 if (handler == null) { |
|
69 throw new IllegalArgumentException("Attribute handler may not be null"); |
|
70 } |
|
71 this.attribHandlerMap.put(name, handler); |
|
72 } |
|
73 |
|
74 /** |
|
75 * Finds an attribute handler {@link CookieAttributeHandler} for the |
|
76 * given attribute. Returns <tt>null</tt> if no attribute handler is |
|
77 * found for the specified attribute. |
|
78 * |
|
79 * @param name attribute name. e.g. Domain, Path, etc. |
|
80 * @return an attribute handler or <tt>null</tt> |
|
81 */ |
|
82 protected CookieAttributeHandler findAttribHandler(final String name) { |
|
83 return this.attribHandlerMap.get(name); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Gets attribute handler {@link CookieAttributeHandler} for the |
|
88 * given attribute. |
|
89 * |
|
90 * @param name attribute name. e.g. Domain, Path, etc. |
|
91 * @throws IllegalStateException if handler not found for the |
|
92 * specified attribute. |
|
93 */ |
|
94 protected CookieAttributeHandler getAttribHandler(final String name) { |
|
95 CookieAttributeHandler handler = findAttribHandler(name); |
|
96 if (handler == null) { |
|
97 throw new IllegalStateException("Handler not registered for " + |
|
98 name + " attribute."); |
|
99 } else { |
|
100 return handler; |
|
101 } |
|
102 } |
|
103 |
|
104 protected Collection<CookieAttributeHandler> getAttribHandlers() { |
|
105 return this.attribHandlerMap.values(); |
|
106 } |
|
107 |
|
108 } |