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.dcae.httpclient;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.Collections;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
30 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
31 import org.onap.policy.common.endpoints.http.client.HttpClient;
32 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
33 import org.onap.policy.common.utils.coder.Coder;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public abstract class AbstractHttpClient implements Closeable {
40 private static final String MSG_REQUEST_FAILED = "request to {} failed";
42 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
43 private final HttpClient httpclient;
44 public static final Coder CODER = new StandardCoder();
49 * @param restClientParameters the REST client parameters
50 * @throws ControlLoopRuntimeException on errors
52 protected AbstractHttpClient(BusTopicParams restClientParameters) {
54 httpclient = HttpClientFactoryInstance.getClientFactory().build(restClientParameters);
55 } catch (final Exception e) {
56 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
57 restClientParameters.getClientName() + " Client failed to start", e);
61 protected boolean executePut(String path, String jsonEntity, int statusCode) {
63 var response = httpclient.put(path, Entity.json(jsonEntity), Collections.emptyMap());
64 return response.getStatus() == statusCode;
65 } catch (Exception e) {
66 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
71 protected boolean executePut(String path, int statusCode) {
73 var response = httpclient.put(path, Entity.json(""), Collections.emptyMap());
74 return response.getStatus() == statusCode;
75 } catch (Exception e) {
76 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
81 protected Loop executePost(String path, int statusCode) {
83 var response = httpclient.post(path, Entity.json(""), Collections.emptyMap());
84 if (response.getStatus() != statusCode) {
87 return response.readEntity(Loop.class);
88 } catch (Exception e) {
89 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
94 protected Loop executeGet(String path, int statusCode) {
96 var response = httpclient.get(path);
98 if (response.getStatus() != statusCode) {
101 return response.readEntity(Loop.class);
102 } catch (Exception e) {
103 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
109 public void close() throws IOException {
110 httpclient.shutdown();