summaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java
blob: 5722fc9c4e8b21da9102ec6f2ffa7ea363b9cf0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package org.openecomp.sdc.common.http.client.api;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.FunctionThrows;
import org.openecomp.sdc.common.http.config.HttpClientConfig;

public enum HttpRequestHandler {
    HANDLER;
    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;
        if (entity != null) {
            InputStream content = entity.getContent();
            if (content != null) {
                response = IOUtils.toByteArray(content);
            }
        }
        return new HttpResponse<>(response, 
                httpResponse.getStatusLine().getStatusCode(), 
                httpResponse.getStatusLine().getReasonPhrase());
    };

    private FunctionThrows<CloseableHttpResponse, HttpResponse<String>, Exception> stringResponseBuilder = (CloseableHttpResponse httpResponse) -> {
        HttpEntity entity = httpResponse.getEntity();
        String response = null;
        if (entity != null) {
            response = EntityUtils.toString(entity);
        }
        return new HttpResponse<>(response, 
                httpResponse.getStatusLine().getStatusCode(),
                httpResponse.getStatusLine().getReasonPhrase());
    };

    HttpRequestHandler() {
        HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory();
        clientFactory = new HttpClientFactory(connectionMngFactory);
    }
    
    static HttpRequestHandler get() {
        return HANDLER;
    }

    public HttpResponse<String> get(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
        HttpClient client = getOrCreateClient(url, config);
        return client.<String>get(url, headers, stringResponseBuilder);
    }

    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> put(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
        HttpClient client = getOrCreateClient(url, config);
        return client.<String>put(url, headers, entity, stringResponseBuilder);
    }

    public HttpResponse<String> post(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
        HttpClient client = getOrCreateClient(url, config);
        return client.<String>post(url, headers, entity, stringResponseBuilder);
    }

    public HttpResponse<String> patch(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException {
        HttpClient client = getOrCreateClient(url, config);
        return client.<String>patch(url, headers, entity, stringResponseBuilder);
    }

    public HttpResponse<String> delete(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException {
        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);
        return clients.computeIfAbsent(httpClientConfigImmutable, k -> createClient(protocol, httpClientConfigImmutable));
    }

    private HttpClient createClient(String protocol, HttpClientConfigImmutable config) {
        return clientFactory.createClient(protocol, config);
    }

    private String getProtocol(String url) throws HttpExecuteException { 
        if (url.startsWith(HTTPS_PREFIX)) {
            return Constants.HTTPS;
        }
        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));
        }        
    }
}