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 / impl / ChefApiClientImpl.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.impl;
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.api.ChefApiClient;
35 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
36 import org.onap.appc.adapter.chef.chefclient.impl.ChefRequestBuilder.OngoingRequestBuilder;
37
38 public class ChefApiClientImpl implements ChefApiClient {
39
40     private final HttpClient httpClient;
41     private final ChefApiHeaderFactory chefApiHeaderFactory;
42     private String endpoint;
43     private String userId;
44     private String pemPath;
45     private String organizations;
46
47     public ChefApiClientImpl(HttpClient httpClient, ChefApiHeaderFactory chefApiHeaderFactory,
48         String endpoint, String organizations, String userId, String pemPath) {
49         this.httpClient = httpClient;
50         this.chefApiHeaderFactory = chefApiHeaderFactory;
51         this.endpoint = endpoint;
52         this.organizations = organizations;
53         this.userId = userId;
54         this.pemPath = pemPath;
55     }
56
57     @Override
58     public ChefResponse get(String path) {
59         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
60             .httpGet()
61             .withPath(path)
62             .withHeaders(chefApiHeaderFactory.create("GET", path, "", userId, organizations, pemPath));
63         return execute(requestBuilder);
64     }
65
66     @Override
67     public ChefResponse delete(String path) {
68         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
69             .httpDelete()
70             .withPath(path)
71             .withHeaders(chefApiHeaderFactory.create("DELETE", path, "", userId, organizations, pemPath));
72         return execute(requestBuilder);
73     }
74
75     @Override
76     public ChefResponse post(String path, String body) {
77         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
78             .httpPost(body)
79             .withPath(path)
80             .withHeaders(chefApiHeaderFactory.create("POST", path, body, userId, organizations, pemPath));
81         return execute(requestBuilder);
82     }
83
84     @Override
85     public ChefResponse put(String path, String body) {
86         OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
87             .httpPut(body)
88             .withPath(path)
89             .withHeaders(chefApiHeaderFactory.create("PUT", path, body, userId, organizations, pemPath));
90         return execute(requestBuilder);
91     }
92
93     private ChefResponse execute(OngoingRequestBuilder chefRequest) {
94         try {
95             HttpResponse response = httpClient.execute(chefRequest.build());
96             int statusCode = response.getStatusLine().getStatusCode();
97             HttpEntity httpEntity = response.getEntity();
98             String responseBody = EntityUtils.toString(httpEntity);
99             return ChefResponse.create(statusCode, responseBody);
100         } catch (IOException ex) {
101             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
102         } catch (URISyntaxException ex) {
103             return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
104         }
105     }
106 }