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