2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.policy.client;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.Collections;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.Status;
32 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public abstract class AbstractHttpClient implements Closeable {
41 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
42 private final HttpClient httpclient;
47 * @param policyParticipantParameters the parameters for the policy participant
48 * @throws ControlLoopRuntimeException on client start errors
50 protected AbstractHttpClient(BusTopicParams policyParticipantParameters) {
52 httpclient = HttpClientFactoryInstance.getClientFactory().build(policyParticipantParameters);
53 } catch (final Exception e) {
54 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR, " Client failed to start", e);
58 protected Response executePost(String path, final Entity<?> entity) {
59 var response = httpclient.post(path, entity, Map.of(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON,
60 HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
61 if (response.getStatus() / 100 != 2) {
62 LOGGER.error("Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}",
63 path, entity, response.getStatus(), response.getStatusInfo());
68 protected Response executeDelete(String path) {
69 return httpclient.delete(path, Collections.emptyMap());
73 public void close() throws IOException {
74 httpclient.shutdown();