2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.chef.chefclient.impl;
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;
38 public class ChefApiClientImpl implements ChefApiClient {
40 private final HttpClient httpClient;
41 private final String endpoint;
42 private final HttpHeaderFactory httpHeaderFactory;
44 public ChefApiClientImpl(HttpClient httpClient, String endpoint, HttpHeaderFactory httpHeaderFactory) {
45 this.httpClient = httpClient;
46 this.endpoint = endpoint;
47 this.httpHeaderFactory = httpHeaderFactory;
51 public ChefResponse get(String path) {
52 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
55 .withHeaders(httpHeaderFactory.create("GET", path, ""));
56 return execute(requestBuilder);
60 public ChefResponse delete(String path) {
61 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
64 .withHeaders(httpHeaderFactory.create("DELETE", path, ""));
65 return execute(requestBuilder);
69 public ChefResponse post(String path, String body) {
70 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
73 .withHeaders(httpHeaderFactory.create("POST", path, body));
74 return execute(requestBuilder);
78 public ChefResponse put(String path, String body) {
79 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
82 .withHeaders(httpHeaderFactory.create("PUT", path, body));
83 return execute(requestBuilder);
86 private ChefResponse execute(OngoingRequestBuilder chefRequest) {
88 HttpResponse response = httpClient.execute(chefRequest.build());
89 int statusCode = response.getStatusLine().getStatusCode();
90 HttpEntity httpEntity = response.getEntity();
91 String responseBody = EntityUtils.toString(httpEntity);
92 return ChefResponse.create(statusCode, responseBody);
93 } catch (IOException ex) {
94 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
95 } catch (URISyntaxException ex) {
96 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());