e23aaaf23a2c53c5c79e7aad7d50c25636f45bf2
[policy/clamp.git] /
1 /*-
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
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.controlloop.participant.dcae.httpclient;
22
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;
38
39 public abstract class AbstractHttpClient implements Closeable {
40
41     private static final String MSG_REQUEST_FAILED = "request to {} failed";
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractHttpClient.class);
44     private final HttpClient httpclient;
45     public static final Coder CODER = new StandardCoder();
46
47     /**
48      * Constructor.
49      */
50     protected AbstractHttpClient(BusTopicParams restClientParameters) {
51         try {
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);
56         }
57     }
58
59     protected boolean executePut(String path, String jsonEntity, int statusCode) {
60         try {
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);
65             return false;
66         }
67     }
68
69     protected boolean executePut(String path, int statusCode) {
70         try {
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);
75             return false;
76         }
77     }
78
79     protected Loop executePost(String path, int statusCode) {
80         try {
81             Response response = httpclient.post(path, Entity.json(""), Collections.emptyMap());
82             if (response.getStatus() != statusCode) {
83                 return null;
84             }
85             return response.readEntity(Loop.class);
86         } catch (Exception e) {
87             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
88             return null;
89         }
90     }
91
92     protected Loop executeGet(String path, int statusCode) {
93         try {
94             Response response = httpclient.get(path);
95
96             if (response.getStatus() != statusCode) {
97                 return null;
98             }
99             return response.readEntity(Loop.class);
100         } catch (Exception e) {
101             LOGGER.error(MSG_REQUEST_FAILED, httpclient.getName(), e);
102             return null;
103         }
104     }
105
106     @Override
107     public void close() throws IOException {
108         httpclient.shutdown();
109     }
110 }