Sync Integ to Master
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / client / api / HttpRequestHandler.java
1 package org.openecomp.sdc.common.http.client.api;
2
3 import java.io.InputStream;
4 import java.util.Map;
5 import java.util.Properties;
6 import java.util.concurrent.ConcurrentHashMap;
7
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;
15
16 public enum HttpRequestHandler {
17     HANDLER;
18     private static final String HTTPS_PREFIX = "https://";
19     private static final String HTTP_PREFIX = "http://";
20     
21     private Map<HttpClientConfigImmutable, HttpClient> clients = new ConcurrentHashMap<>();
22     private HttpClientFactory clientFactory;
23     
24     private FunctionThrows<CloseableHttpResponse, HttpResponse<byte[]>, Exception> byteResponseBuilder = (CloseableHttpResponse httpResponse) -> {
25         HttpEntity entity = httpResponse.getEntity();
26         byte[] response = null;
27         if (entity != null) {
28             InputStream content = entity.getContent();
29             if (content != null) {
30                 response = IOUtils.toByteArray(content);
31             }
32         }
33         return new HttpResponse<>(response, 
34                 httpResponse.getStatusLine().getStatusCode(), 
35                 httpResponse.getStatusLine().getReasonPhrase());
36     };
37
38     private FunctionThrows<CloseableHttpResponse, HttpResponse<String>, Exception> stringResponseBuilder = (CloseableHttpResponse httpResponse) -> {
39         HttpEntity entity = httpResponse.getEntity();
40         String response = null;
41         if (entity != null) {
42             response = EntityUtils.toString(entity);
43         }
44         return new HttpResponse<>(response, 
45                 httpResponse.getStatusLine().getStatusCode(),
46                 httpResponse.getStatusLine().getReasonPhrase());
47     };
48
49     HttpRequestHandler() {
50         HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory();
51         clientFactory = new HttpClientFactory(connectionMngFactory);
52     }
53     
54     static HttpRequestHandler get() {
55         return HANDLER;
56     }
57
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);
61     }
62
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);
66     }
67
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);
71     }
72
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);
76     }
77
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);
81     }
82
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);
86     }
87     
88     public void destroy() {
89         clients.forEach((k, v) -> v.close());
90         clients.clear();
91     }
92     
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));
97     }
98
99     private HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
100         return clientFactory.createClient(protocol, config);
101     }
102
103     private String getProtocol(String url) throws HttpExecuteException { 
104         if (url.startsWith(HTTPS_PREFIX)) {
105             return Constants.HTTPS;
106         }
107         else if (url.startsWith(HTTP_PREFIX)) {
108             return Constants.HTTP;
109         }
110         else {
111             throw new HttpExecuteException(String.format("Failed to create http client. Requested protocol is not supported \"%s\"", url));
112         }        
113     }
114 }