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.hc.core5.http.protocol;
29  
30  import java.util.Map;
31  import java.util.concurrent.ConcurrentHashMap;
32  
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.ThreadingBehavior;
35  import org.apache.hc.core5.http.HttpVersion;
36  import org.apache.hc.core5.http.ProtocolVersion;
37  import org.apache.hc.core5.util.Args;
38  
39  /**
40   * Default implementation of {@link HttpContext}.
41   * <p>
42   * Please note instances of this class can be thread unsafe if the
43   * parent context is not thread safe.
44   *
45   * @since 4.0
46   */
47  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
48  public class BasicHttpContext implements HttpContext {
49  
50      private final HttpContext parentContext;
51      private final Map<String, Object> map;
52  
53      private ProtocolVersion version;
54  
55      public BasicHttpContext() {
56          this(null);
57      }
58  
59      public BasicHttpContext(final HttpContext parentContext) {
60          super();
61          this.map = new ConcurrentHashMap<>();
62          this.parentContext = parentContext;
63      }
64  
65      @Override
66      public Object getAttribute(final String id) {
67          Args.notNull(id, "Id");
68          Object obj = this.map.get(id);
69          if (obj == null && this.parentContext != null) {
70              obj = this.parentContext.getAttribute(id);
71          }
72          return obj;
73      }
74  
75      @Override
76      public void setAttribute(final String id, final Object obj) {
77          Args.notNull(id, "Id");
78          if (obj != null) {
79              this.map.put(id, obj);
80          } else {
81              this.map.remove(id);
82          }
83      }
84  
85      @Override
86      public Object removeAttribute(final String id) {
87          Args.notNull(id, "Id");
88          return this.map.remove(id);
89      }
90  
91      /**
92       * @since 5.0
93       */
94      @Override
95      public ProtocolVersion getProtocolVersion() {
96          return this.version != null ? this.version : HttpVersion.DEFAULT;
97      }
98  
99      /**
100      * @since 5.0
101      */
102     @Override
103     public void setProtocolVersion(final ProtocolVersion version) {
104         this.version = version;
105     }
106 
107     /**
108      * @since 4.2
109      */
110     public void clear() {
111         this.map.clear();
112     }
113 
114     @Override
115     public String toString() {
116         return this.map.toString();
117     }
118 
119 }