6429d062e4e8905be4c493da1c805a62c569f4ac
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021,2023 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.policy.client;
22
23 import jakarta.ws.rs.client.Entity;
24 import jakarta.ws.rs.core.HttpHeaders;
25 import jakarta.ws.rs.core.MediaType;
26 import jakarta.ws.rs.core.Response;
27 import jakarta.ws.rs.core.Response.Status;
28 import java.io.IOException;
29 import java.util.Collections;
30 import java.util.Map;
31 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
32 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
33 import org.onap.policy.common.endpoints.http.client.HttpClient;
34 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public abstract class AbstractHttpClient implements AutoCloseable {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
41     private final HttpClient httpclient;
42
43     /**
44      * Constructor.
45      *
46      * @param policyParticipantParameters the parameters for the policy participant
47      * @throws AutomationCompositionRuntimeException on client start errors
48      */
49     protected AbstractHttpClient(BusTopicParams policyParticipantParameters) {
50         try {
51             httpclient = HttpClientFactoryInstance.getClientFactory().build(policyParticipantParameters);
52         } catch (final Exception e) {
53             throw new AutomationCompositionRuntimeException(Status.INTERNAL_SERVER_ERROR, " Client failed to start", e);
54         }
55     }
56
57     protected Response executePost(String path, final Entity<?> entity) {
58         var response = httpclient.post(path, entity, Map.of(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON,
59             HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
60         if (response.getStatus() / 100 != 2) {
61             LOGGER.error("Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}",
62                 path, entity, response.getStatus(), response.getStatusInfo());
63         }
64         return response;
65     }
66
67     protected Response executeDelete(String path) {
68         return httpclient.delete(path, Collections.emptyMap());
69     }
70
71     @Override
72     public void close() throws IOException {
73         httpclient.shutdown();
74     }
75 }