aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpSdcClient.java
blob: 8b6ee0a7d27c8b09c4ae2502758744666cb1157f (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*-
 * ============LICENSE_START=======================================================
 * sdc-distribution-client
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Modifications copyright (C) 2020 Nokia. 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.onap.sdc.http;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.onap.sdc.api.consumer.IConfiguration;
import org.onap.sdc.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class HttpSdcClient implements IHttpSdcClient {

    private static final Logger log = LoggerFactory.getLogger(HttpSdcClient.class.getName());
    private static final boolean ALWAYS_CLOSE_THE_REQUEST_CONNECTION = true;
    private final CloseableHttpClient httpClient;
    private final String httpSchema;
    private final String serverFqdn;
    private final HttpRequestFactory httpRequestFactory;

    /**
     * Constructor
     *
     * @deprecated
     * This constructor will be removed in the future.
     *
     * @param configuration Sdc client configuration
     */
    @Deprecated
    public HttpSdcClient(IConfiguration configuration) {
        this(configuration.getSdcAddress(),
                new HttpClientFactory(configuration),
                new HttpRequestFactory(configuration.getUser(), configuration.getPassword())
        );
    }

    public HttpSdcClient(String sdcAddress, HttpClientFactory httpClientFactory, HttpRequestFactory httpRequestFactory) {
        this.serverFqdn = sdcAddress;
        this.httpRequestFactory = httpRequestFactory;

        Pair<String, CloseableHttpClient> httpClientPair = httpClientFactory.createInstance();
        this.httpSchema = httpClientPair.getFirst();
        this.httpClient = httpClientPair.getSecond();
    }

    public HttpSdcResponse postRequest(String requestUrl, HttpEntity entity, Map<String, String> headersMap) {
        return postRequest(requestUrl, entity, headersMap, ALWAYS_CLOSE_THE_REQUEST_CONNECTION).getFirst();
    }

    public Pair<HttpSdcResponse, CloseableHttpResponse> postRequest(String requestUrl, HttpEntity entity, Map<String, String> headersMap, boolean closeTheRequest) {
        Pair<HttpSdcResponse, CloseableHttpResponse> ret;
        final String url = resolveUrl(requestUrl);
        log.debug("url to send {}", url);
        HttpPost httpPost = httpRequestFactory.createHttpPostRequest(url, headersMap, entity);

        CloseableHttpResponse httpResponse = null;
        HttpSdcResponse response = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            response = new HttpSdcResponse(httpResponse.getStatusLine().getStatusCode(), httpResponse.getEntity());
        } catch (IOException e) {
            log.error("failed to send request to url: {}", requestUrl);
            response = createHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "failed to send request");
        } finally {
            ret = finalizeHttpRequest(closeTheRequest, httpResponse, response);
        }

        return ret;
    }

    public HttpSdcResponse getRequest(String requestUrl, Map<String, String> headersMap) {
        return getRequest(requestUrl, headersMap, ALWAYS_CLOSE_THE_REQUEST_CONNECTION).getFirst();
    }

    public Pair<HttpSdcResponse, CloseableHttpResponse> getRequest(String requestUrl, Map<String, String> headersMap, boolean closeTheRequest) {
        Pair<HttpSdcResponse, CloseableHttpResponse> ret;

        final String url = resolveUrl(requestUrl);
        log.debug("url to send {}", url);
        HttpGet httpGet = httpRequestFactory.createHttpGetRequest(url, headersMap);

        CloseableHttpResponse httpResponse = null;
        HttpSdcResponse response = null;
        try {
            httpResponse = httpClient.execute(httpGet);

            log.debug("GET Response Status {}", httpResponse.getStatusLine().getStatusCode());
            Header[] headersRes = httpResponse.getAllHeaders();
            Map<String, String> headersResMap = new HashMap<>();
            for (Header header : headersRes) {
                headersResMap.put(header.getName(), header.getValue());
            }
            response = new HttpSdcResponse(httpResponse.getStatusLine().getStatusCode(), httpResponse.getEntity(), headersResMap);

        } catch (UnknownHostException | ConnectException e) {
            log.error("failed to connect to url: {}", requestUrl, e);
            response = createHttpResponse(HttpStatus.SC_BAD_GATEWAY, "failed to connect");
        } catch (IOException e) {
            log.error("failed to send request to url: {} error {}", requestUrl, e.getMessage());
            response = createHttpResponse(HttpStatus.SC_BAD_GATEWAY, "failed to send request " + e.getMessage());
        } finally {
            ret = finalizeHttpRequest(closeTheRequest, httpResponse, response);
        }

        return ret;
    }

    String getHttpSchema(){
        return this.httpSchema;
    }

    private String resolveUrl(String requestUrl) {
        return this.httpSchema + serverFqdn + requestUrl;
    }

    private Pair<HttpSdcResponse, CloseableHttpResponse> finalizeHttpRequest(boolean closeTheRequest, CloseableHttpResponse httpResponse, HttpSdcResponse response) {
        Pair<HttpSdcResponse, CloseableHttpResponse> ret;
        if (closeTheRequest) {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    log.error("failed to close http response");
                }
            }
            ret = new Pair<>(response, null);
        } else {
            ret = new Pair<>(response, httpResponse);
        }

        return ret;
    }

    static HttpSdcResponse createHttpResponse(int httpStatusCode, String httpMessage) {
        return new HttpSdcResponse(httpStatusCode, new StringEntity(httpMessage, StandardCharsets.UTF_8));
    }

    public void closeHttpClient() {
        try {
            httpClient.close();
        } catch (IOException e) {
            log.error("failed to close http client");
        }
    }
}