package org.apache.http.client.methods;
import java.net.URI;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicRequestLine;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.Args;
@SuppressWarnings("deprecation")
public class HttpRequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
private final HttpRequest original;
private final HttpHost target;
private final String method;
private RequestLine requestLine;
private ProtocolVersion version;
private URI uri;
private HttpRequestWrapper(final HttpRequest request, final HttpHost target) {
super();
this.original = Args.notNull(request, "HTTP request");
this.target = target;
this.version = this.original.getRequestLine().getProtocolVersion();
this.method = this.original.getRequestLine().getMethod();
if (request instanceof HttpUriRequest) {
this.uri = ((HttpUriRequest) request).getURI();
} else {
this.uri = null;
}
setHeaders(request.getAllHeaders());
}
@Override
public ProtocolVersion getProtocolVersion() {
return this.version != null ? this.version : this.original.getProtocolVersion();
}
public void setProtocolVersion(final ProtocolVersion version) {
this.version = version;
this.requestLine = null;
}
@Override
public URI getURI() {
return this.uri;
}
public void setURI(final URI uri) {
this.uri = uri;
this.requestLine = null;
}
@Override
public String getMethod() {
return method;
}
@Override
public void abort() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
@Override
public boolean isAborted() {
return false;
}
@Override
public RequestLine getRequestLine() {
if (this.requestLine == null) {
String requestUri;
if (this.uri != null) {
requestUri = this.uri.toASCIIString();
} else {
requestUri = this.original.getRequestLine().getUri();
}
if (requestUri == null || requestUri.isEmpty()) {
requestUri = "/";
}
this.requestLine = new BasicRequestLine(this.method, requestUri, getProtocolVersion());
}
return this.requestLine;
}
public HttpRequest getOriginal() {
return this.original;
}
public HttpHost getTarget() {
return target;
}
@Override
public String toString() {
return getRequestLine() + " " + this.headergroup;
}
static class HttpEntityEnclosingRequestWrapper extends HttpRequestWrapper
implements HttpEntityEnclosingRequest {
private HttpEntity entity;
HttpEntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request, final HttpHost target) {
super(request, target);
this.entity = request.getEntity();
}
@Override
public HttpEntity getEntity() {
return this.entity;
}
@Override
public void setEntity(final HttpEntity entity) {
this.entity = entity;
}
@Override
public boolean expectContinue() {
final Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
}
}
public static HttpRequestWrapper wrap(final HttpRequest request) {
return wrap(request, null);
}
public static HttpRequestWrapper wrap(final HttpRequest request, final HttpHost target) {
Args.notNull(request, "HTTP request");
return request instanceof HttpEntityEnclosingRequest
? new HttpEntityEnclosingRequestWrapper(
(HttpEntityEnclosingRequest) request, target)
: new HttpRequestWrapper(request, target);
}
@Override
@Deprecated
public HttpParams getParams() {
if (this.params == null) {
this.params = original.getParams().copy();
}
return this.params;
}
}