2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.adapter.chef.chefclient.impl;
25 import java.io.IOException;
26 import java.net.URISyntaxException;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.HttpStatus;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.util.EntityUtils;
35 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
36 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
37 import org.onap.appc.adapter.chef.chefclient.impl.ChefRequestBuilder.OngoingRequestBuilder;
39 public class ChefApiClientImpl implements ChefApiClient {
41 private final HttpClient httpClient;
42 private final String endpoint;
43 private final String organization;
44 private final HttpHeaderFactory httpHeaderFactory;
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefApiClientImpl.class);
46 public ChefApiClientImpl(HttpClient httpClient, String endpoint, String organization,
47 HttpHeaderFactory httpHeaderFactory) {
48 this.httpClient = httpClient;
49 this.endpoint = endpoint;
50 this.organization = organization;
51 this.httpHeaderFactory = httpHeaderFactory;
55 public ChefResponse get(String path) {
56 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
58 .withPath(getPath(path))
59 .withHeaders(httpHeaderFactory.create("GET", path, ""));
60 return execute(requestBuilder);
64 public ChefResponse delete(String path) {
65 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
67 .withPath(getPath(path))
68 .withHeaders(httpHeaderFactory.create("DELETE", path, ""));
69 return execute(requestBuilder);
73 public ChefResponse post(String path, String body) {
74 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
76 .withPath(getPath(path))
77 .withHeaders(httpHeaderFactory.create("POST", path, body));
78 return execute(requestBuilder);
82 public ChefResponse put(String path, String body) {
83 OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint)
85 .withPath(getPath(path))
86 .withHeaders(httpHeaderFactory.create("PUT", path, body));
87 logger.info("request: PATH: "+path+ " body: "+body);
88 return execute(requestBuilder);
91 private ChefResponse execute(OngoingRequestBuilder chefRequest) {
93 if (httpClient == null) {
94 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Could not create http client for chef");
96 HttpResponse response = httpClient.execute(chefRequest.build());
97 int statusCode = response.getStatusLine().getStatusCode();
98 HttpEntity httpEntity = response.getEntity();
99 String responseBody = EntityUtils.toString(httpEntity);
100 return ChefResponse.create(statusCode, responseBody);
101 } catch (IOException ex) {
102 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
103 } catch (URISyntaxException ex) {
104 return ChefResponse.create(HttpStatus.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
108 private String getPath(String path) {
109 return "/organizations/" + organization + path;