code cleanup in http clieant

pull/533/head
Michael Peter Christen 2 years ago
parent 62d177bf59
commit 5ddc794bb9

@ -186,7 +186,7 @@ public class HTTPClient implements Closeable {
super();
this.timeout = agent.clientTimeout;
clientBuilder.setUserAgent(agent.userAgent);
reqConfBuilder = RequestConfig.copy(DFLTREQUESTCONFIG);
this.reqConfBuilder = RequestConfig.copy(DFLTREQUESTCONFIG);
setTimout(agent.clientTimeout);
}
@ -194,7 +194,7 @@ public class HTTPClient implements Closeable {
super();
this.timeout = timeout;
clientBuilder.setUserAgent(agent.userAgent);
reqConfBuilder = RequestConfig.copy(DFLTREQUESTCONFIG);
this.reqConfBuilder = RequestConfig.copy(DFLTREQUESTCONFIG);
setTimout(timeout);
}
@ -284,7 +284,7 @@ public class HTTPClient implements Closeable {
* @throws IllegalArgumentException
* when pool is null or when maxConnections is lower than 1
*/
public static void initPoolMaxConnections(final PoolingHttpClientConnectionManager pool, int maxConnections) {
public static void initPoolMaxConnections(final PoolingHttpClientConnectionManager pool, final int maxConnections) {
if (pool == null) {
throw new IllegalArgumentException("pool parameter must not be null");
}
@ -340,9 +340,9 @@ public class HTTPClient implements Closeable {
* @param timeout in milliseconds
*/
public void setTimout(final int timeout) {
reqConfBuilder.setSocketTimeout(timeout);
reqConfBuilder.setConnectTimeout(timeout);
reqConfBuilder.setConnectionRequestTimeout(timeout);
this.reqConfBuilder.setSocketTimeout(timeout);
this.reqConfBuilder.setConnectTimeout(timeout);
this.reqConfBuilder.setConnectionRequestTimeout(timeout);
}
/**
@ -369,8 +369,8 @@ public class HTTPClient implements Closeable {
* @param redirecting
*/
public void setRedirecting(final boolean redirecting) {
reqConfBuilder.setRedirectsEnabled(redirecting);
reqConfBuilder.setRelativeRedirectsAllowed(redirecting);
this.reqConfBuilder.setRedirectsEnabled(redirecting);
this.reqConfBuilder.setRelativeRedirectsAllowed(redirecting);
}
/**
@ -438,7 +438,7 @@ public class HTTPClient implements Closeable {
try {
this.currentRequest = new HttpGet(urix);
} catch (IllegalArgumentException e) {
} catch (final IllegalArgumentException e) {
throw new IOException(e.getMessage()); // can be caused at java.net.URI.create()
}
if (!localhost) setHost(url.getHost()); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
@ -446,7 +446,7 @@ public class HTTPClient implements Closeable {
return getContentBytes(maxBytes, concurrent);
}
CredentialsProvider credsProvider = new BasicCredentialsProvider();
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", url.getPort()),
new UsernamePasswordCredentials(username, pass));
@ -454,7 +454,7 @@ public class HTTPClient implements Closeable {
try (final CloseableHttpClient httpclient = clientBuilder.setDefaultCredentialsProvider(credsProvider)
.setDefaultAuthSchemeRegistry(AUTHSCHEMEREGISTRY).build()) {
this.httpResponse = httpclient.execute(this.currentRequest);
HttpEntity httpEntity = this.httpResponse.getEntity();
final HttpEntity httpEntity = this.httpResponse.getEntity();
if (httpEntity != null) {
if (getStatusCode() == HttpStatus.SC_OK) {
if (maxBytes >= 0 && httpEntity.getContentLength() > maxBytes) {
@ -498,7 +498,7 @@ public class HTTPClient implements Closeable {
try {
this.currentRequest = new HttpGet(urix);
} catch (IllegalArgumentException e) {
} catch (final IllegalArgumentException e) {
throw new IOException(e.getMessage()); // can be caused at java.net.URI.create()
}
setHost(url.getHost()); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
@ -642,7 +642,7 @@ public class HTTPClient implements Closeable {
try (final CloseableHttpClient httpclient = clientBuilder.setDefaultCredentialsProvider(credsProvider)
.setDefaultAuthSchemeRegistry(AUTHSCHEMEREGISTRY).build()) {
this.httpResponse = httpclient.execute(this.currentRequest);
HttpEntity httpEntity = this.httpResponse.getEntity();
final HttpEntity httpEntity = this.httpResponse.getEntity();
if (httpEntity != null) {
if (getStatusCode() == HttpStatus.SC_OK) {
return getByteArray(httpEntity, Integer.MAX_VALUE);
@ -703,7 +703,7 @@ public class HTTPClient implements Closeable {
String mimeType = null;
if (this.httpResponse != null) {
Header contentType = this.httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);
final Header contentType = this.httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);
if (contentType != null) {
@ -731,11 +731,11 @@ public class HTTPClient implements Closeable {
String charsetName = null;
if (this.httpResponse != null) {
Header contentTypeHeader = this.httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);
final Header contentTypeHeader = this.httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);
if (contentTypeHeader != null) {
String contentType = contentTypeHeader.getValue();
final String contentType = contentTypeHeader.getValue();
if (contentType != null) {
@ -818,7 +818,7 @@ public class HTTPClient implements Closeable {
this.httpResponse.close();
}
if (this.client != null) {
client.close();
this.client.close();
}
} finally {
if (this.currentRequest != null) {
@ -854,7 +854,7 @@ public class HTTPClient implements Closeable {
private void execute(final boolean concurrent) throws IOException {
final HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(reqConfBuilder.build());
context.setRequestConfig(this.reqConfBuilder.build());
if (this.host != null)
context.setTargetHost(new HttpHost(this.host));
@ -879,25 +879,25 @@ public class HTTPClient implements Closeable {
this.client = clientBuilder.build();
if (concurrent) {
FutureTask<CloseableHttpResponse> t = new FutureTask<CloseableHttpResponse>(new Callable<CloseableHttpResponse>() {
final FutureTask<CloseableHttpResponse> t = new FutureTask<CloseableHttpResponse>(new Callable<CloseableHttpResponse>() {
@Override
public CloseableHttpResponse call() throws ClientProtocolException, IOException {
CloseableHttpResponse response = client.execute(currentRequest, context);
final CloseableHttpResponse response = HTTPClient.this.client.execute(HTTPClient.this.currentRequest, context);
return response;
}
});
executor.execute(t);
try {
this.httpResponse = t.get(this.timeout, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
} catch (final ExecutionException e) {
throw e.getCause();
} catch (Throwable e) {}
try {t.cancel(true);} catch (Throwable e) {}
} catch (final Throwable e) {}
try {t.cancel(true);} catch (final Throwable e) {}
if (this.httpResponse == null) {
throw new IOException("timout to client after " + this.timeout + "ms" + " for url " + uri);
}
} else {
this.httpResponse = client.execute(this.currentRequest, context);
this.httpResponse = this.client.execute(this.currentRequest, context);
}
this.httpResponse.setHeader(HeaderFramework.RESPONSE_TIME_MILLIS, Long.toString(System.currentTimeMillis() - time));
} catch (final Throwable e) {
@ -923,7 +923,7 @@ public class HTTPClient implements Closeable {
if (instream == null) {
return null;
}
long contentLength = entity.getContentLength();
final long contentLength = entity.getContentLength();
/*
* When no maxBytes is specified, the default limit is
* Integer.MAX_VALUE as a byte array size can not be over
@ -944,7 +944,7 @@ public class HTTPClient implements Closeable {
initialSize = 4096;
}
final ByteArrayBuffer buffer = new ByteArrayBuffer(initialSize);
byte[] tmp = new byte[4096];
final byte[] tmp = new byte[4096];
int l = 0;
/* Sum is a long to enable check against Integer.MAX_VALUE */
long sum = 0;
@ -1025,7 +1025,7 @@ public class HTTPClient implements Closeable {
new NoopHostnameVerifier()) {
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
protected void prepareSocket(final SSLSocket socket) throws IOException {
if(!ENABLE_SNI_EXTENSION.get()) {
/* Set the SSLParameters server names to empty so we don't use SNI extension.
* See https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html#ClientSNIExamples */
@ -1047,8 +1047,8 @@ public class HTTPClient implements Closeable {
private static ConnectionKeepAliveStrategy customKeepAliveStrategy() {
return new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
final long keepAlive = super.getKeepAliveDuration(response, context);
return Math.min(Math.max(keepAlive, 5000), 25000);
}
};

Loading…
Cancel
Save