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