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 org.apache.http.HttpConnection;
31 import org.apache.http.HttpHost;
32 import org.apache.http.HttpRequest;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.util.Args;
35
36
37
38
39
40
41
42 public class HttpCoreContext implements HttpContext {
43
44
45
46
47
48 public static final String HTTP_CONNECTION = "http.connection";
49
50
51
52
53
54 public static final String HTTP_REQUEST = "http.request";
55
56
57
58
59
60 public static final String HTTP_RESPONSE = "http.response";
61
62
63
64
65
66 public static final String HTTP_TARGET_HOST = "http.target_host";
67
68
69
70
71
72
73 public static final String HTTP_REQ_SENT = "http.request_sent";
74
75 public static HttpCoreContext create() {
76 return new HttpCoreContext(new BasicHttpContext());
77 }
78
79 public static HttpCoreContext adapt(final HttpContext context) {
80 Args.notNull(context, "HTTP context");
81 return context instanceof HttpCoreContext
82 ? (HttpCoreContext) context
83 : new HttpCoreContext(context);
84 }
85
86 private final HttpContext context;
87
88 public HttpCoreContext(final HttpContext context) {
89 super();
90 this.context = context;
91 }
92
93 public HttpCoreContext() {
94 super();
95 this.context = new BasicHttpContext();
96 }
97
98 @Override
99 public Object getAttribute(final String id) {
100 return context.getAttribute(id);
101 }
102
103 @Override
104 public void setAttribute(final String id, final Object obj) {
105 context.setAttribute(id, obj);
106 }
107
108 @Override
109 public Object removeAttribute(final String id) {
110 return context.removeAttribute(id);
111 }
112
113 public <T> T getAttribute(final String attribname, final Class<T> clazz) {
114 Args.notNull(clazz, "Attribute class");
115 final Object obj = getAttribute(attribname);
116 if (obj == null) {
117 return null;
118 }
119 return clazz.cast(obj);
120 }
121
122 public <T extends HttpConnection> T getConnection(final Class<T> clazz) {
123 return getAttribute(HTTP_CONNECTION, clazz);
124 }
125
126 public HttpConnection getConnection() {
127 return getAttribute(HTTP_CONNECTION, HttpConnection.class);
128 }
129
130 public HttpRequest getRequest() {
131 return getAttribute(HTTP_REQUEST, HttpRequest.class);
132 }
133
134 public boolean isRequestSent() {
135 final Boolean b = getAttribute(HTTP_REQ_SENT, Boolean.class);
136 return b != null && b.booleanValue();
137 }
138
139 public HttpResponse getResponse() {
140 return getAttribute(HTTP_RESPONSE, HttpResponse.class);
141 }
142
143 public void setTargetHost(final HttpHost host) {
144 setAttribute(HTTP_TARGET_HOST, host);
145 }
146
147 public HttpHost getTargetHost() {
148 return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
149 }
150
151 }