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.HashMap;
31 import java.util.Map;
32
33 import javax.net.ssl.SSLSession;
34
35 import org.apache.hc.core5.http.EndpointDetails;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.HttpVersion;
39 import org.apache.hc.core5.http.ProtocolVersion;
40 import org.apache.hc.core5.util.Args;
41
42
43
44
45
46
47
48
49
50 public class HttpCoreContext implements HttpContext {
51
52
53
54
55 @Deprecated
56 public static final String CONNECTION_ENDPOINT = HttpContext.RESERVED_PREFIX + "connection-endpoint";
57
58
59
60
61 @Deprecated
62 public static final String SSL_SESSION = HttpContext.RESERVED_PREFIX + "ssl-session";
63
64
65
66
67 @Deprecated
68 public static final String HTTP_REQUEST = HttpContext.RESERVED_PREFIX + "request";
69
70
71
72
73 @Deprecated
74 public static final String HTTP_RESPONSE = HttpContext.RESERVED_PREFIX + "response";
75
76 public static HttpCoreContext create() {
77 return new HttpCoreContext();
78 }
79
80
81
82
83 @Deprecated
84 public static HttpCoreContext adapt(final HttpContext context) {
85 if (context == null) {
86 return new HttpCoreContext();
87 }
88 if (context instanceof HttpCoreContext) {
89 return (HttpCoreContext) context;
90 }
91 return new HttpCoreContext(context);
92 }
93
94
95
96
97
98
99
100
101 public static HttpCoreContext cast(final HttpContext context) {
102 if (context instanceof HttpCoreContext) {
103 return (HttpCoreContext) context;
104 }
105 return new HttpCoreContext(context);
106 }
107
108 private final HttpContext parentContext;
109 private Map<String, Object> map;
110 private ProtocolVersion version;
111 private HttpRequest request;
112 private HttpResponse response;
113 private EndpointDetails endpointDetails;
114 private SSLSession sslSession;
115
116 public HttpCoreContext(final HttpContext parentContext) {
117 super();
118 this.parentContext = parentContext;
119 }
120
121 public HttpCoreContext() {
122 super();
123 this.parentContext = null;
124 }
125
126
127
128
129
130
131
132
133
134 @Override
135 public ProtocolVersion getProtocolVersion() {
136 return this.version != null ? this.version : HttpVersion.HTTP_1_1;
137 }
138
139
140
141
142 @Override
143 public void setProtocolVersion(final ProtocolVersion version) {
144 this.version = version;
145 }
146
147 @Override
148 public Object getAttribute(final String id) {
149 Object o = map != null ? map.get(id) : null;
150 if (o == null && parentContext != null) {
151 o = parentContext.getAttribute(id);
152 }
153 return o;
154 }
155
156 @Override
157 public Object setAttribute(final String id, final Object obj) {
158 if (map == null) {
159 map = new HashMap<>();
160 }
161 return map.put(id, obj);
162 }
163
164 @Override
165 public Object removeAttribute(final String id) {
166 if (map != null) {
167 return map.remove(id);
168 } else {
169 return null;
170 }
171 }
172
173 public <T> T getAttribute(final String id, final Class<T> clazz) {
174 Args.notNull(clazz, "Attribute class");
175 final Object obj = getAttribute(id);
176 if (obj == null) {
177 return null;
178 }
179 return clazz.cast(obj);
180 }
181
182
183
184
185
186
187
188 public HttpRequest getRequest() {
189 return request;
190 }
191
192
193
194
195 public void setRequest(final HttpRequest request) {
196 this.request = request;
197 }
198
199
200
201
202
203
204
205 public HttpResponse getResponse() {
206 return response;
207 }
208
209
210
211
212 public void setResponse(final HttpResponse response) {
213 this.response = response;
214 }
215
216
217
218
219
220
221
222
223 public EndpointDetails getEndpointDetails() {
224 return endpointDetails;
225 }
226
227
228
229
230 public void setEndpointDetails(final EndpointDetails endpointDetails) {
231 this.endpointDetails = endpointDetails;
232 }
233
234
235
236
237
238
239
240
241 public SSLSession getSSLSession() {
242 return sslSession;
243 }
244
245
246
247
248 public void setSSLSession(final SSLSession sslSession) {
249 this.sslSession = sslSession;
250 }
251
252 @Override
253 public String toString() {
254 return "HttpCoreContext{" +
255 "version=" + version +
256 ", request=" + request +
257 ", response=" + response +
258 ", endpointDetails=" + endpointDetails +
259 ", sslSession=" + sslSession +
260 '}';
261 }
262
263 }