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.hc.core5.http.protocol;
29
30 import java.util.Map;
31 import java.util.concurrent.ConcurrentHashMap;
32
33 import org.apache.hc.core5.annotation.Contract;
34 import org.apache.hc.core5.annotation.ThreadingBehavior;
35 import org.apache.hc.core5.http.HttpVersion;
36 import org.apache.hc.core5.http.ProtocolVersion;
37 import org.apache.hc.core5.util.Args;
38
39
40
41
42
43
44
45
46
47 @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
48 public class BasicHttpContext implements HttpContext {
49
50 private final HttpContext parentContext;
51 private final Map<String, Object> map;
52
53 private ProtocolVersion version;
54
55 public BasicHttpContext() {
56 this(null);
57 }
58
59 public BasicHttpContext(final HttpContext parentContext) {
60 super();
61 this.map = new ConcurrentHashMap<>();
62 this.parentContext = parentContext;
63 }
64
65 @Override
66 public Object getAttribute(final String id) {
67 Args.notNull(id, "Id");
68 Object obj = this.map.get(id);
69 if (obj == null && this.parentContext != null) {
70 obj = this.parentContext.getAttribute(id);
71 }
72 return obj;
73 }
74
75 @Override
76 public Object setAttribute(final String id, final Object obj) {
77 Args.notNull(id, "Id");
78 if (obj != null) {
79 return this.map.put(id, obj);
80 }
81 return this.map.remove(id);
82 }
83
84 @Override
85 public Object removeAttribute(final String id) {
86 Args.notNull(id, "Id");
87 return this.map.remove(id);
88 }
89
90
91
92
93 @Override
94 public ProtocolVersion getProtocolVersion() {
95 return this.version != null ? this.version : HttpVersion.DEFAULT;
96 }
97
98
99
100
101 @Override
102 public void setProtocolVersion(final ProtocolVersion version) {
103 this.version = version;
104 }
105
106
107
108
109 public void clear() {
110 this.map.clear();
111 }
112
113 @Override
114 public String toString() {
115 return this.map.toString();
116 }
117
118 }