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