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.hc.client5.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.hc.client5.http.cookie.CommonCookieAttributeHandler;
36 import org.apache.hc.client5.http.cookie.CookieAttributeHandler;
37 import org.apache.hc.client5.http.cookie.CookieSpec;
38 import org.apache.hc.core5.annotation.Contract;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.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 * @since 4.0
48 */
49 @Contract(threading = ThreadingBehavior.SAFE)
50 public abstract class AbstractCookieSpec implements CookieSpec {
51
52 /**
53 * Stores attribute name -> attribute handler mappings
54 */
55 private final Map<String, CookieAttributeHandler> attribHandlerMap;
56
57 /**
58 * Default constructor
59 * */
60 public AbstractCookieSpec() {
61 super();
62 this.attribHandlerMap = new ConcurrentHashMap<>(10);
63 }
64
65 /**
66 * @since 4.4
67 */
68 protected AbstractCookieSpec(final HashMap<String, CookieAttributeHandler> map) {
69 super();
70 Asserts.notNull(map, "Attribute handler map");
71 this.attribHandlerMap = new ConcurrentHashMap<>(map);
72 }
73
74 /**
75 * @since 4.4
76 */
77 protected AbstractCookieSpec(final CommonCookieAttributeHandler... handlers) {
78 super();
79 this.attribHandlerMap = new ConcurrentHashMap<>(handlers.length);
80 for (final CommonCookieAttributeHandler handler: handlers) {
81 this.attribHandlerMap.put(handler.getAttributeName(), handler);
82 }
83 }
84
85 /**
86 * Finds an attribute handler {@link CookieAttributeHandler} for the
87 * given attribute. Returns {@code null} if no attribute handler is
88 * found for the specified attribute.
89 *
90 * @param name attribute name. e.g. Domain, Path, etc.
91 * @return an attribute handler or {@code null}
92 */
93 protected CookieAttributeHandler findAttribHandler(final String name) {
94 return this.attribHandlerMap.get(name);
95 }
96
97 /**
98 * Gets attribute handler {@link CookieAttributeHandler} for the
99 * given attribute.
100 *
101 * @param name attribute name. e.g. Domain, Path, etc.
102 * @throws IllegalStateException if handler not found for the
103 * specified attribute.
104 */
105 protected CookieAttributeHandler getAttribHandler(final String name) {
106 final CookieAttributeHandler handler = findAttribHandler(name);
107 Asserts.check(handler != null, "Handler not registered for " +
108 name + " attribute");
109 return handler;
110 }
111
112 protected Collection<CookieAttributeHandler> getAttribHandlers() {
113 return this.attribHandlerMap.values();
114 }
115
116 }