1 package org.openecomp.sdc.common.http.client.api;
3 import java.io.InputStream;
5 import java.util.Properties;
6 import java.util.concurrent.ConcurrentHashMap;
8 import org.apache.commons.io.IOUtils;
9 import org.apache.http.HttpEntity;
10 import org.apache.http.client.methods.CloseableHttpResponse;
11 import org.apache.http.util.EntityUtils;
12 import org.openecomp.sdc.common.api.Constants;
13 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.FunctionThrows;
14 import org.openecomp.sdc.common.http.config.HttpClientConfig;
16 public enum HttpRequestHandler {
18 private static final String HTTPS_PREFIX = "https://";
19 private static final String HTTP_PREFIX = "http://";
21 private Map<HttpClientConfigImmutable, HttpClient> clients = new ConcurrentHashMap<>();
22 private HttpClientFactory clientFactory;
24 private FunctionThrows<CloseableHttpResponse, HttpResponse<byte[]>, Exception> byteResponseBuilder = (CloseableHttpResponse httpResponse) -> {
25 HttpEntity entity = httpResponse.getEntity();
26 byte[] response = null;
28 InputStream content = entity.getContent();
29 if (content != null) {
30 response = IOUtils.toByteArray(content);
33 return new HttpResponse<>(response,
34 httpResponse.getStatusLine().getStatusCode(),
35 httpResponse.getStatusLine().getReasonPhrase());
38 private FunctionThrows<CloseableHttpResponse, HttpResponse<String>, Exception> stringResponseBuilder = (CloseableHttpResponse httpResponse) -> {
39 HttpEntity entity = httpResponse.getEntity();
40 String response = null;
42 response = EntityUtils.toString(entity);
44 return new HttpResponse<>(response,
45 httpResponse.getStatusLine().getStatusCode(),
46 httpResponse.getStatusLine().getReasonPhrase());
49 HttpRequestHandler() {
50 HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory();
51 clientFactory = new HttpClientFactory(connectionMngFactory);
54 static HttpRequestHandler get() {
58 public HttpResponse<String> get(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
59 HttpClient client = getOrCreateClient(url, config);
60 return client.<String>get(url, headers, stringResponseBuilder);
63 public HttpResponse<byte []> getAsByteArray(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
64 HttpClient client = getOrCreateClient(url, config);
65 return client.<byte[]>get(url, headers, byteResponseBuilder);
68 public HttpResponse<String> put(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
69 HttpClient client = getOrCreateClient(url, config);
70 return client.<String>put(url, headers, entity, stringResponseBuilder);
73 public HttpResponse<String> post(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
74 HttpClient client = getOrCreateClient(url, config);
75 return client.<String>post(url, headers, entity, stringResponseBuilder);
78 public HttpResponse<String> patch(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
79 HttpClient client = getOrCreateClient(url, config);
80 return client.<String>patch(url, headers, entity, stringResponseBuilder);
83 public HttpResponse<String> delete(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
84 HttpClient client = getOrCreateClient(url, config != null ? config : HttpRequest.defaultConfig);
85 return client.<String>delete(url, headers, stringResponseBuilder);
88 public void destroy() {
89 clients.forEach((k, v) -> v.close());
93 private HttpClient getOrCreateClient(String url, HttpClientConfig config) throws HttpExecuteException {
94 String protocol = getProtocol(url);
95 HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(config);
96 return clients.computeIfAbsent(httpClientConfigImmutable, k -> createClient(protocol, httpClientConfigImmutable));
99 private HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
100 return clientFactory.createClient(protocol, config);
103 private String getProtocol(String url) throws HttpExecuteException {
104 if (url.startsWith(HTTPS_PREFIX)) {
105 return Constants.HTTPS;
107 else if (url.startsWith(HTTP_PREFIX)) {
108 return Constants.HTTP;
111 throw new HttpExecuteException(String.format("Failed to create http client. Requested protocol is not supported \"%s\"", url));