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