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 Object setAttribute(final String id, final Object obj) {
77          Args.notNull(id, "Id");
78          if (obj != null) {
79              return this.map.put(id, obj);
80          }
81          return this.map.remove(id);
82      }
83  
84      @Override
85      public Object removeAttribute(final String id) {
86          Args.notNull(id, "Id");
87          return this.map.remove(id);
88      }
89  
90      /**
91       * @since 5.0
92       */
93      @Override
94      public ProtocolVersion getProtocolVersion() {
95          return this.version != null ? this.version : HttpVersion.DEFAULT;
96      }
97  
98      /**
99       * @since 5.0
100      */
101     @Override
102     public void setProtocolVersion(final ProtocolVersion version) {
103         this.version = version;
104     }
105 
106     /**
107      * @since 4.2
108      */
109     public void clear() {
110         this.map.clear();
111     }
112 
113     @Override
114     public String toString() {
115         return this.map.toString();
116     }
117 
118 }