aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-dcae/src/main/java/org/onap/policy/clamp/controlloop/participant/dcae/httpclient/AbstractHttpClient.java
blob: b2d0b61d07bdeca68803b5b857c140237c9aa311 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.participant.dcae.httpclient;

import java.io.Closeable;
import java.io.IOException;
import javax.ws.rs.core.Response.Status;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.ParseException;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
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.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractHttpClient implements Closeable {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
    private final HttpClientContext localContext;
    private final CloseableHttpClient httpclient;
    private final HttpHost target;
    public static final Coder CODER = new StandardCoder();

    /**
     * Constructor.
     */
    protected AbstractHttpClient(RestServerParameters restServerParameters) {
        try {
            final String scheme = restServerParameters.isHttps() ? "https" : "http";
            target = new HttpHost(restServerParameters.getHost(), restServerParameters.getPort(), scheme);

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                    new UsernamePasswordCredentials(restServerParameters.getUserName(),
                            restServerParameters.getPassword()));

            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(target, basicAuth);
            localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);

            HttpClientBuilder builder = HttpClients.custom().setDefaultCredentialsProvider(credsProvider);
            if (restServerParameters.isHttps()) {
                final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(new SSLContextBuilder()
                        .loadTrustMaterial(null, new TrustSelfSignedStrategy()).setProtocol("TLSv1.2").build(),
                        new NoopHostnameVerifier());
                builder.setSSLSocketFactory(sslsf);
            }
            httpclient = builder.build();

        } catch (final Exception e) {
            throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
                    restServerParameters.getName() + " Client failed to start", e);
        }
    }

    CloseableHttpResponse execute(HttpRequest request) throws IOException {
        return httpclient.execute(target, request, localContext);
    }

    protected boolean executePut(String path, int statusCode) {
        try (CloseableHttpResponse response = execute(new HttpPut(path))) {
            return response.getStatusLine().getStatusCode() == statusCode;
        } catch (Exception e) {
            return false;
        }
    }

    protected Loop executePost(String path, int statusCode) {
        try (CloseableHttpResponse response = execute(new HttpPost(path))) {
            if (response.getStatusLine().getStatusCode() != statusCode) {
                return null;
            }
            return entityToMap(response.getEntity());
        } catch (Exception e) {
            return null;
        }
    }

    protected Loop executeGet(String path, int statusCode) {
        try (CloseableHttpResponse response = execute(new HttpGet(path))) {
            if (response.getStatusLine().getStatusCode() != statusCode) {
                return null;
            }
            return entityToMap(response.getEntity());
        } catch (Exception e) {
            return null;
        }
    }

    private Loop entityToMap(HttpEntity httpEntity) {
        if (httpEntity == null) {
            return new Loop();
        }
        try {
            return CODER.convert(EntityUtils.toString(httpEntity), Loop.class);
        } catch (ParseException | IOException e) {
            LOGGER.error("error reading Entity", e);
            return new Loop();
        } catch (CoderException e) {
            LOGGER.error("cannot convert to Loop Object", e);
            return new Loop();
        }
    }

    @Override
    public void close() throws IOException {
        httpclient.close();
    }
}