0f9ad2da39080009b51ca95a6062ed17238eab0d
[so.git] / cloudify-client / src / main / java / org / onap / so / cloudify / base / client / CloudifyRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
21 package org.onap.so.cloudify.base.client;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.ArrayList;
28 import java.util.Map;
29
30 public class CloudifyRequest<R> {
31
32     private CloudifyClient client;
33
34     public CloudifyRequest() {
35
36     }
37
38     public CloudifyRequest(CloudifyClient client, HttpMethod method, CharSequence path, Entity<?> entity,
39             Class<R> returnType) {
40         this.client = client;
41         this.method = method;
42         this.path = new StringBuilder(path);
43         this.entity = entity;
44         this.returnType = returnType;
45         header("Accept", "application/json");
46     }
47
48     private String endpoint;
49
50     private HttpMethod method;
51
52     private StringBuilder path = new StringBuilder();
53
54     private Map<String, List<Object>> headers = new HashMap<String, List<Object>>();
55
56     private Entity<?> entity;
57
58     private Class<R> returnType;
59
60     private boolean basicAuth = false;
61     private String user = null;
62     private String password = null;
63
64     public CloudifyRequest<R> endpoint(String endpoint) {
65         this.endpoint = endpoint;
66         return this;
67     }
68
69     public String endpoint() {
70         return endpoint;
71     }
72
73     public CloudifyRequest<R> method(HttpMethod method) {
74         this.method = method;
75         return this;
76     }
77
78     public HttpMethod method() {
79         return method;
80     }
81
82     public CloudifyRequest<R> path(String path) {
83         this.path.append(path);
84         return this;
85     }
86
87     public String path() {
88         return path.toString();
89     }
90
91     public CloudifyRequest<R> header(String name, Object value) {
92         if (value != null) {
93             headers.put(name, Arrays.asList(value));
94         }
95         return this;
96     }
97
98     public Map<String, List<Object>> headers() {
99         return headers;
100     }
101
102     public <T> Entity<T> entity(T entity, String contentType) {
103         return new Entity<T>(entity, contentType);
104     }
105
106     public Entity<?> entity() {
107         return entity;
108     }
109
110     public <T> Entity<T> json(T entity) {
111         return entity(entity, "application/json");
112     }
113
114     public void returnType(Class<R> returnType) {
115         this.returnType = returnType;
116     }
117
118     public Class<R> returnType() {
119         return returnType;
120     }
121
122     /*
123      * Use Basic Authentication for this request. If not set, the client will use Token authentication if a token
124      * provider is defined. Otherwise, no authentication will be applied.
125      */
126     public void setBasicAuthentication(String user, String password) {
127         this.basicAuth = true;
128         this.user = user;
129         this.password = password;
130     }
131
132     public boolean isBasicAuth() {
133         return this.basicAuth;
134     }
135
136     public String getUser() {
137         return user;
138     }
139
140     public String getPassword() {
141         return password;
142     }
143
144     public R execute() {
145         return client.execute(this);
146     }
147
148     public CloudifyResponse request() {
149         return client.request(this);
150     }
151
152     /*
153      * (non-Javadoc)
154      * 
155      * @see java.lang.Object#toString()
156      */
157     @Override
158     public String toString() {
159         return "CloudifyRequest [endpoint=" + endpoint + ", method=" + method + ", path=" + path + ", headers="
160                 + headers + ", entity=" + entity + ", returnType=" + returnType + "]";
161     }
162
163     private Map<String, List<Object>> queryParams = new LinkedHashMap<String, List<Object>>();
164
165     public Map<String, List<Object>> queryParams() {
166         return queryParams;
167     }
168
169     public CloudifyRequest<R> queryParam(String key, Object value) {
170         if (queryParams.containsKey(key)) {
171             List<Object> values = queryParams.get(key);
172             values.add(value);
173         } else {
174             List<Object> values = new ArrayList<Object>();
175             values.add(value);
176             queryParams.put(key, values);
177         }
178
179         return this;
180     }
181
182     protected static String buildPath(String... elements) {
183         StringBuilder stringBuilder = new StringBuilder();
184         for (String element : elements) {
185             stringBuilder.append(element);
186         }
187
188         return stringBuilder.toString();
189     }
190 }