modify listeners to only start when props present
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / ChefApiClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.chef.chefclient;
26
27 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import org.apache.http.HttpEntity;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.util.EntityUtils;
34 import org.onap.appc.adapter.chef.chefclient.ChefRequestBuilder.OngoingRequestBuilder;
35
36 public class ChefApiClient {
37
38     private final HttpClient httpClient;
39     private final ChefApiHeaderFactory chefApiHeaderFactory;
40     private String endpoint;
41     private String userId;
42     private String pemPath;
43     private String organizations;
44
45     ChefApiClient(HttpClient httpClient, ChefApiHeaderFactory chefApiHeaderFactory,
46         String endpoint, String organizations, String userId, String pemPath) {
47         this.httpClient = httpClient;
48         this.chefApiHeaderFactory = chefApiHeaderFactory;
49         this.endpoint = endpoint;
50         this.organizations = organizations;
51         this.userId = userId;
52         this.pemPath = pemPath;
53     }
54
55     public ChefResponse get(String path) {
56         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
57             .httpGet()
58             .withPath(path)
59             .withHeaders(chefApiHeaderFactory.create("GET", path, "", userId, organizations, pemPath));
60         return execute(requestBuilder);
61     }
62
63     public ChefResponse delete(String path) {
64         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
65             .httpDelete()
66             .withPath(path)
67             .withHeaders(chefApiHeaderFactory.create("DELETE", path, "", userId, organizations, pemPath));
68         return execute(requestBuilder);
69     }
70
71     public ChefResponse post(String path, String body) {
72         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
73             .httpPost(body)
74             .withPath(path)
75             .withHeaders(chefApiHeaderFactory.create("POST", path, body, userId, organizations, pemPath));
76         return execute(requestBuilder);
77     }
78
79     public ChefResponse put(String path, String body) {
80         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
81             .httpPut(body)
82             .withPath(path)
83             .withHeaders(chefApiHeaderFactory.create("PUT", path, body, userId, organizations, pemPath));
84         return execute(requestBuilder);
85     }
86
87     private ChefResponse execute(OngoingRequestBuilder chefRequest) {
88         try {
89             HttpResponse response = httpClient.execute(chefRequest.build());
90             int statusCode = response.getStatusLine().getStatusCode();
91             HttpEntity httpEntity = response.getEntity();
92             String responseBody = EntityUtils.toString(httpEntity);
93             return ChefResponse.create(statusCode, responseBody);
94         } catch (IOException ex) {
95             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
96         } catch (URISyntaxException ex) {
97             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
98         }
99     }
100 }