* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
private static HttpRequestHandler handlerInstance = new HttpRequestHandler();
private static final String HTTPS_PREFIX = "https://";
private static final String HTTP_PREFIX = "http://";
-
+
private Map<HttpClientConfigImmutable, HttpClient> clients = new ConcurrentHashMap<>();
private HttpClientFactory clientFactory;
-
+
private FunctionThrows<CloseableHttpResponse, HttpResponse<byte[]>, Exception> byteResponseBuilder = (CloseableHttpResponse httpResponse) -> {
HttpEntity entity = httpResponse.getEntity();
byte[] response = null;
response = IOUtils.toByteArray(content);
}
}
- return new HttpResponse<>(response,
- httpResponse.getStatusLine().getStatusCode(),
+ return new HttpResponse<>(response,
+ httpResponse.getStatusLine().getStatusCode(),
httpResponse.getStatusLine().getReasonPhrase());
};
if (entity != null) {
response = EntityUtils.toString(entity);
}
- return new HttpResponse<>(response,
+ return new HttpResponse<>(response,
httpResponse.getStatusLine().getStatusCode(),
httpResponse.getStatusLine().getReasonPhrase());
};
HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory();
clientFactory = new HttpClientFactory(connectionMngFactory);
}
-
+
public static HttpRequestHandler get() {
return handlerInstance;
}
return client.<String>get(url, headers, stringResponseBuilder);
}
- public HttpResponse<byte[]> getAsByteArray(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
+ public HttpResponse<byte []> getAsByteArray(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
HttpClient client = getOrCreateClient(url, config);
return client.<byte[]>get(url, headers, byteResponseBuilder);
}
}
public HttpResponse<String> delete(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
- HttpClient client = getOrCreateClient(url, config != null ? config : HttpRequest.getDefaultConfig());
+ HttpClient client = getOrCreateClient(url, config != null ? config : HttpRequest.defaultConfig);
return client.<String>delete(url, headers, stringResponseBuilder);
}
-
+
public void destroy() {
clients.forEach((k, v) -> v.close());
clients.clear();
}
-
+
private HttpClient getOrCreateClient(String url, HttpClientConfig config) throws HttpExecuteException {
String protocol = getProtocol(url);
HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(config);
private String getProtocol(String url) throws HttpExecuteException {
if (url.startsWith(HTTPS_PREFIX)) {
return Constants.HTTPS;
- } else if (url.startsWith(HTTP_PREFIX)) {
+ }
+ else if (url.startsWith(HTTP_PREFIX)) {
return Constants.HTTP;
- } else {
- throw new HttpExecuteException(String.format("Failed to create http client. Requested protocol is not supported \"%s\"", url));
}
+ else {
+ throw new HttpExecuteException(String.format("Failed to create http client. Requested protocol is not supported \"%s\"", url));
+ }
}
}