2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * =============================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.appc.adapter.chef.chefclient.impl;
22 import com.google.common.collect.ImmutableMap;
24 import java.net.URISyntaxException;
25 import java.util.Map.Entry;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpGet;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.client.methods.HttpPut;
30 import org.apache.http.client.methods.HttpRequestBase;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.apache.http.entity.StringEntity;
34 final class ChefRequestBuilder {
36 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();
72 httpRequestBase = httpPost;
76 public OngoingRequestBuilder httpPut(String body) {
77 HttpPut httpPut = new HttpPut();
79 httpRequestBase = httpPut;
83 private void toEntity(String body) {
84 StringEntity stringEntity = new StringEntity(body, "UTF-8");
85 stringEntity.setContentType("application/json");
88 public OngoingRequestBuilder withHeaders(ImmutableMap<String, String> headers) {
89 this.headers = headers;
93 public HttpRequestBase build() throws URISyntaxException {
97 return httpRequestBase;
100 private void setRequestUri() throws URISyntaxException {
101 URI fullPath = new URIBuilder(endPoint)
102 .setPath(path).build();
103 httpRequestBase.setURI(fullPath);
106 private void setRequestHeaders() {
107 for (Entry<String, String> entry : headers.entrySet()) {
108 httpRequestBase.addHeader(entry.getKey(), entry.getValue());