aboutsummaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClient.java
blob: 66a70508211f92294c5b02127c29b7f91919572a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.common.http.client.api;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.FunctionThrows;
import org.openecomp.sdc.common.log.wrappers.Logger;

import java.io.IOException;
import java.net.URI;
import java.util.Properties;

public class HttpClient {
    private static final Logger logger = Logger.getLogger(HttpClient.class.getName());
    
    private final CloseableHttpClient client;
    private final HttpClientConfigImmutable configImmutable;
    
    public HttpClient(CloseableHttpClient client, HttpClientConfigImmutable configImmutable) {
        this.client = client;
        this.configImmutable = configImmutable; 
    }
    
    public <T> HttpResponse<T> get(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        HttpGet httpGet = new HttpGet(url);
        return execute(httpGet, headers, responseBuilder);
    }

    <T> HttpResponse<T> put(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(entity);
        return execute(httpPut, headers, responseBuilder);
    }

    <T> HttpResponse<T> post(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(entity);
        return execute(httpPost, headers, responseBuilder);
    }

    <T> HttpResponse<T> patch(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        HttpPatch httpPatch = new HttpPatch(url);
        httpPatch.setEntity(entity);
        return execute(httpPatch, headers, responseBuilder);
    }

    <T> HttpResponse<T> delete(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        HttpDelete httpDelete = new HttpDelete(url);
        return execute(httpDelete, headers, responseBuilder);
    }
    
    void close() {
        try {
            client.close();
        }
        catch (IOException e) {
            logger.debug("Close http client failed with exception ", e);
        }
    }
    
    private <T> HttpResponse<T> execute(HttpRequestBase request, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException {
        if(configImmutable.getHeaders() != null) {
            configImmutable.getHeaders().forEach(request::addHeader);
        }

        if (headers != null) {
            headers.forEach((k, v) -> request.addHeader(k.toString(), v.toString()));
        }

        HttpClientContext httpClientContext = null;
        if(request.getHeaders(HttpHeaders.AUTHORIZATION).length == 0) {
            httpClientContext = createHttpContext(request.getURI());
        }

        logger.debug("Execute request {}", request.getRequestLine());
        try (CloseableHttpResponse response = client.execute(request, httpClientContext)) {
            return responseBuilder.apply(response);
        }
        catch (Exception e) {
            String description = String.format("Execute request %s failed with exception: %s", request.getRequestLine(), e.getMessage()); 
            BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description, ErrorSeverity.ERROR);
            logger.debug("{}: ",description, e);

            throw new HttpExecuteException(description, e);
        } 
    }

    private HttpClientContext createHttpContext(URI uri) {
        if(StringUtils.isEmpty(configImmutable.getBasicAuthUserName()) || StringUtils.isEmpty(configImmutable.getBasicAuthPassword())) {
            return null;
        }

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        int port = getPort(uri);
        credentialsProvider.setCredentials(new AuthScope(uri.getHost(), port), 
                new UsernamePasswordCredentials(configImmutable.getBasicAuthUserName(), configImmutable.getBasicAuthPassword()));

        HttpClientContext localContext = HttpClientContext.create();
        localContext.setCredentialsProvider(credentialsProvider);

        AuthCache authCache = new BasicAuthCache();
        HttpHost target = new HttpHost(uri.getHost(), port, "https");
        authCache.put(target, (AuthScheme) new BasicScheme());
        localContext.setAuthCache(authCache);

        return localContext;
    }

    private int getPort(URI uri) {
        int port = uri.getPort(); 
        if(port < 0) {
            if(Constants.HTTPS.equals(uri.getScheme())) {
                port = 443;
            }
            else
            if (Constants.HTTP.equals(uri.getScheme())) {
                port = 80;
            }
            else {
                port = AuthScope.ANY_PORT;
                logger.debug("Protocol \"{}\" is not supported, set port to {}", uri.getScheme(), port);
            }
        }
        return port;
    }
}