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;
28 import javax.ws.rs.core.Response.Status;
29 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
30 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
31 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
32 import org.onap.policy.common.endpoints.http.client.HttpClient;
33 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
34 import org.onap.policy.common.utils.coder.Coder;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public abstract class AbstractHttpClient implements Closeable {
41 private static final String MSG_REQUEST_FAILED = "request to {} failed";
43 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
44 private final HttpClient httpclient;
45 public static final Coder CODER = new StandardCoder();
50 protected AbstractHttpClient(BusTopicParams restClientParameters) {
52 httpclient = HttpClientFactoryInstance.getClientFactory().build(restClientParameters);
53 } catch (final Exception e) {
54 throw new ControlLoopRuntimeException(Status.INTERNAL_SERVER_ERROR,
55 restClientParameters.getClientName() + " Client failed to start", e);
59 protected boolean executePut(String path, String jsonEntity, int statusCode) {
61 Response response = httpclient.put(path, Entity.json(jsonEntity), Collections.emptyMap());
62 return response.getStatus() == statusCode;
63 } catch (Exception e) {
64 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
69 protected boolean executePut(String path, int statusCode) {
71 Response response = httpclient.put(path, Entity.json(""), Collections.emptyMap());
72 return response.getStatus() == statusCode;
73 } catch (Exception e) {
74 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
79 protected Loop executePost(String path, int statusCode) {
81 Response response = httpclient.post(path, Entity.json(""), Collections.emptyMap());
82 if (response.getStatus() != statusCode) {
85 return response.readEntity(Loop.class);
86 } catch (Exception e) {
87 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
92 protected Loop executeGet(String path, int statusCode) {
94 Response response = httpclient.get(path);
96 if (response.getStatus() != statusCode) {
99 return response.readEntity(Loop.class);
100 } catch (Exception e) {
101 LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
107 public void close() throws IOException {
108 httpclient.shutdown();