a2248438a2654ae1de0d13a0f3aafc0307612d86
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefclient / impl / ChefRequestBuilder.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.appc.adapter.chef.chefclient.impl;
21
22 import com.google.common.collect.ImmutableMap;
23 import java.net.URI;
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;
33
34 final class ChefRequestBuilder {
35
36     private ChefRequestBuilder() {
37     }
38
39     static OngoingRequestBuilder newRequestTo(String endPoint) {
40         return new OngoingRequestBuilder(endPoint);
41     }
42
43     static class OngoingRequestBuilder {
44
45         private HttpRequestBase httpRequestBase;
46         private String endPoint;
47         private String path;
48         private ImmutableMap<String, String> headers;
49
50         private OngoingRequestBuilder(String endPoint) {
51             this.endPoint = endPoint;
52         }
53
54         public OngoingRequestBuilder withPath(String path) {
55             this.path = path;
56             return this;
57         }
58
59         public OngoingRequestBuilder httpGet() {
60             httpRequestBase = new HttpGet();
61             return this;
62         }
63
64         public OngoingRequestBuilder httpDelete() {
65             httpRequestBase = new HttpDelete();
66             return this;
67         }
68
69         public OngoingRequestBuilder httpPost(String body) {
70             HttpPost httpPost = new HttpPost();
71             toEntity(body);
72             httpRequestBase = httpPost;
73             return this;
74         }
75
76         public OngoingRequestBuilder httpPut(String body) {
77             HttpPut httpPut = new HttpPut();
78             toEntity(body);
79             httpRequestBase = httpPut;
80             return this;
81         }
82
83         private void toEntity(String body) {
84             StringEntity stringEntity = new StringEntity(body, "UTF-8");
85             stringEntity.setContentType("application/json");
86         }
87
88         public OngoingRequestBuilder withHeaders(ImmutableMap<String, String> headers) {
89             this.headers = headers;
90             return this;
91         }
92
93         public HttpRequestBase build() throws URISyntaxException {
94             setRequestUri();
95             setRequestHeaders();
96
97             return httpRequestBase;
98         }
99
100         private void setRequestUri() throws URISyntaxException {
101             URI fullPath = new URIBuilder(endPoint)
102                 .setPath(path).build();
103             httpRequestBase.setURI(fullPath);
104         }
105
106         private void setRequestHeaders() {
107             for (Entry<String, String> entry : headers.entrySet()) {
108                 httpRequestBase.addHeader(entry.getKey(), entry.getValue());
109             }
110         }
111     }
112 }