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