2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 * =============================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.chef.chefclient.impl;
23 import com.google.common.collect.ImmutableMap;
25 import java.net.URISyntaxException;
26 import java.util.Map.Entry;
27 import org.apache.http.client.methods.HttpDelete;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.client.methods.HttpPut;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.client.utils.URIBuilder;
33 import org.apache.http.entity.StringEntity;
35 final class ChefRequestBuilder {
37 private ChefRequestBuilder() {}
39 static OngoingRequestBuilder newRequestTo(String endPoint) {
40 return new OngoingRequestBuilder(endPoint);
43 static class OngoingRequestBuilder {
45 private HttpRequestBase httpRequestBase;
46 private String endPoint;
48 private ImmutableMap<String, String> headers;
50 private OngoingRequestBuilder(String endPoint) {
51 this.endPoint = endPoint;
54 public OngoingRequestBuilder withPath(String path) {
59 public OngoingRequestBuilder httpGet() {
60 httpRequestBase = new HttpGet();
64 public OngoingRequestBuilder httpDelete() {
65 httpRequestBase = new HttpDelete();
69 public OngoingRequestBuilder httpPost(String body) {
70 HttpPost httpPost = new HttpPost();
71 httpPost.setEntity(toEntity(body));
72 httpRequestBase = httpPost;
76 public OngoingRequestBuilder httpPut(String body) {
77 HttpPut httpPut = new HttpPut();
78 httpPut.setEntity(toEntity(body));
79 httpRequestBase = httpPut;
83 private StringEntity toEntity(String body) {
84 StringEntity stringEntity = new StringEntity(body, "UTF-8");
85 stringEntity.setContentType("application/json");
89 public OngoingRequestBuilder withHeaders(ImmutableMap<String, String> headers) {
90 this.headers = headers;
94 public HttpRequestBase build() throws URISyntaxException {
97 return httpRequestBase;
100 private void setRequestUri() throws URISyntaxException {
101 URI fullPath = new URIBuilder(endPoint).setPath(path).build();
102 httpRequestBase.setURI(fullPath);
105 private void setRequestHeaders() {
106 for (Entry<String, String> entry : headers.entrySet()) {
107 httpRequestBase.addHeader(entry.getKey(), entry.getValue());