diff options
Diffstat (limited to 'common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java')
-rw-r--r-- | common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java index e4747f4308..73f0f15354 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java @@ -7,9 +7,9 @@ * 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. @@ -38,10 +38,10 @@ public class HttpRequestHandler { 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; @@ -51,8 +51,8 @@ public class HttpRequestHandler { response = IOUtils.toByteArray(content); } } - return new HttpResponse<>(response, - httpResponse.getStatusLine().getStatusCode(), + return new HttpResponse<>(response, + httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); }; @@ -62,7 +62,7 @@ public class HttpRequestHandler { if (entity != null) { response = EntityUtils.toString(entity); } - return new HttpResponse<>(response, + return new HttpResponse<>(response, httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); }; @@ -71,7 +71,7 @@ public class HttpRequestHandler { HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory(); clientFactory = new HttpClientFactory(connectionMngFactory); } - + public static HttpRequestHandler get() { return handlerInstance; } @@ -81,7 +81,7 @@ public class HttpRequestHandler { 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); } @@ -102,15 +102,15 @@ public class HttpRequestHandler { } 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); @@ -129,10 +129,12 @@ public class HttpRequestHandler { 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)); + } } } |