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