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      public Object getAttribute(final String id) {
61          Args.notNull(id, "Id");
62          Object obj = this.map.get(id);
63          if (obj == null && this.parentContext != null) {
64              obj = this.parentContext.getAttribute(id);
65          }
66          return obj;
67      }
68  
69      public void setAttribute(final String id, final Object obj) {
70          Args.notNull(id, "Id");
71          if (obj != null) {
72              this.map.put(id, obj);
73          } else {
74              this.map.remove(id);
75          }
76      }
77  
78      public Object removeAttribute(final String id) {
79          Args.notNull(id, "Id");
80          return this.map.remove(id);
81      }
82  
83      /**
84       * @since 4.2
85       */
86      public void clear() {
87          this.map.clear();
88      }
89  
90      @Override
91      public String toString() {
92          return this.map.toString();
93      }
94  
95  }