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   * @deprecated Do not use.
46   */
47  @Deprecated
48  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
49  public class BasicHttpContext implements HttpContext {
50  
51      private final HttpContext parentContext;
52      private final Map<String, Object> map;
53  
54      private ProtocolVersion version;
55  
56      public BasicHttpContext() {
57          this(null);
58      }
59  
60      public BasicHttpContext(final HttpContext parentContext) {
61          super();
62          this.map = new ConcurrentHashMap<>();
63          this.parentContext = parentContext;
64      }
65  
66      @Override
67      public Object getAttribute(final String id) {
68          Args.notNull(id, "Id");
69          Object obj = this.map.get(id);
70          if (obj == null && this.parentContext != null) {
71              obj = this.parentContext.getAttribute(id);
72          }
73          return obj;
74      }
75  
76      @Override
77      public Object setAttribute(final String id, final Object obj) {
78          Args.notNull(id, "Id");
79          if (obj != null) {
80              return this.map.put(id, obj);
81          }
82          return this.map.remove(id);
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 }