Skip to content

Commit

Permalink
Restore the ApacheHttpRequest implementation. (#641)
Browse files Browse the repository at this point in the history
The RequestConfig class is an API from the newer Apache HttpClient 4.5.x
clients.
  • Loading branch information
chingor13 authored May 20, 2019
1 parent 249ddec commit 1954575
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@
import java.io.IOException;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

/** @author Yaniv Inbar */
/**
* @author Yaniv Inbar
*/
final class ApacheHttpRequest extends LowLevelHttpRequest {
private final HttpClient httpClient;

private final HttpRequestBase request;

private RequestConfig.Builder requestConfig;

ApacheHttpRequest(HttpClient httpClient, HttpRequestBase request) {
this.httpClient = httpClient;
this.request = request;
this.requestConfig = RequestConfig.custom().setRedirectsEnabled(false);
}

@Override
Expand All @@ -44,22 +45,23 @@ public void addHeader(String name, String value) {

@Override
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
requestConfig.setConnectionRequestTimeout(connectTimeout).setSocketTimeout(readTimeout);
HttpParams params = request.getParams();
ConnManagerParams.setTimeout(params, connectTimeout);
HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
HttpConnectionParams.setSoTimeout(params, readTimeout);
}

@Override
public LowLevelHttpResponse execute() throws IOException {
if (getStreamingContent() != null) {
Preconditions.checkArgument(
request instanceof HttpEntityEnclosingRequest,
Preconditions.checkArgument(request instanceof HttpEntityEnclosingRequest,
"Apache HTTP client does not support %s requests with content.",
request.getRequestLine().getMethod());
ContentEntity entity = new ContentEntity(getContentLength(), getStreamingContent());
entity.setContentEncoding(getContentEncoding());
entity.setContentType(getContentType());
((HttpEntityEnclosingRequest) request).setEntity(entity);
}
request.setConfig(requestConfig.build());
return new ApacheHttpResponse(request, httpClient.execute(request));
}
}

0 comments on commit 1954575

Please sign in to comment.