1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
39
40
41
42
43
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
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 }