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.client.methods;
29
30 import java.net.URI;
31
32 import org.apache.http.Header;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpEntityEnclosingRequest;
35 import org.apache.http.HttpHost;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.ProtocolVersion;
38 import org.apache.http.RequestLine;
39 import org.apache.http.message.AbstractHttpMessage;
40 import org.apache.http.message.BasicRequestLine;
41 import org.apache.http.params.HttpParams;
42 import org.apache.http.protocol.HTTP;
43 import org.apache.http.util.Args;
44
45
46
47
48
49
50
51 @SuppressWarnings("deprecation")
52 public class HttpRequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
53
54 private final HttpRequest original;
55 private final HttpHost target;
56 private final String method;
57 private RequestLine requestLine;
58 private ProtocolVersion version;
59 private URI uri;
60
61 private HttpRequestWrapper(final HttpRequest request, final HttpHost target) {
62 super();
63 this.original = Args.notNull(request, "HTTP request");
64 this.target = target;
65 this.version = this.original.getRequestLine().getProtocolVersion();
66 this.method = this.original.getRequestLine().getMethod();
67 if (request instanceof HttpUriRequest) {
68 this.uri = ((HttpUriRequest) request).getURI();
69 } else {
70 this.uri = null;
71 }
72 setHeaders(request.getAllHeaders());
73 }
74
75 @Override
76 public ProtocolVersion getProtocolVersion() {
77 return this.version != null ? this.version : this.original.getProtocolVersion();
78 }
79
80 public void setProtocolVersion(final ProtocolVersion version) {
81 this.version = version;
82 this.requestLine = null;
83 }
84
85 @Override
86 public URI getURI() {
87 return this.uri;
88 }
89
90 public void setURI(final URI uri) {
91 this.uri = uri;
92 this.requestLine = null;
93 }
94
95 @Override
96 public String getMethod() {
97 return method;
98 }
99
100 @Override
101 public void abort() throws UnsupportedOperationException {
102 throw new UnsupportedOperationException();
103 }
104
105 @Override
106 public boolean isAborted() {
107 return false;
108 }
109
110 @Override
111 public RequestLine getRequestLine() {
112 if (this.requestLine == null) {
113 String requestUri;
114 if (this.uri != null) {
115 requestUri = this.uri.toASCIIString();
116 } else {
117 requestUri = this.original.getRequestLine().getUri();
118 }
119 if (requestUri == null || requestUri.isEmpty()) {
120 requestUri = "/";
121 }
122 this.requestLine = new BasicRequestLine(this.method, requestUri, getProtocolVersion());
123 }
124 return this.requestLine;
125 }
126
127 public HttpRequest getOriginal() {
128 return this.original;
129 }
130
131
132
133
134 public HttpHost getTarget() {
135 return target;
136 }
137
138 @Override
139 public String toString() {
140 return getRequestLine() + " " + this.headergroup;
141 }
142
143 static class HttpEntityEnclosingRequestWrapper extends HttpRequestWrapper
144 implements HttpEntityEnclosingRequest {
145
146 private HttpEntity entity;
147
148 HttpEntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request, final HttpHost target) {
149 super(request, target);
150 this.entity = request.getEntity();
151 }
152
153 @Override
154 public HttpEntity getEntity() {
155 return this.entity;
156 }
157
158 @Override
159 public void setEntity(final HttpEntity entity) {
160 this.entity = entity;
161 }
162
163 @Override
164 public boolean expectContinue() {
165 final Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
166 return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
167 }
168
169 }
170
171
172
173
174
175
176
177 public static HttpRequestWrapper wrap(final HttpRequest request) {
178 return wrap(request, null);
179 }
180
181
182
183
184
185
186
187
188
189
190 public static HttpRequestWrapper wrap(final HttpRequest request, final HttpHost target) {
191 Args.notNull(request, "HTTP request");
192 return request instanceof HttpEntityEnclosingRequest
193 ? new HttpEntityEnclosingRequestWrapper(
194 (HttpEntityEnclosingRequest) request, target)
195 : new HttpRequestWrapper(request, target);
196 }
197
198
199
200
201
202 @Override
203 @Deprecated
204 public HttpParams getParams() {
205 if (this.params == null) {
206 this.params = original.getParams().copy();
207 }
208 return this.params;
209 }
210
211 }