2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.so.cloudify.base.client;
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;
30 public class CloudifyRequest<R> {
32 private CloudifyClient client;
34 private String endpoint;
36 private HttpMethod method;
38 private StringBuilder path = new StringBuilder();
40 private Map<String, List<Object>> headers = new HashMap<>();
42 private Entity<?> entity;
44 private Class<R> returnType;
46 private boolean basicAuth = false;
47 private String user = null;
48 private String password = null;
50 public CloudifyRequest() {
54 public CloudifyRequest(CloudifyClient client, HttpMethod method, CharSequence path, Entity<?> entity,
55 Class<R> returnType) {
58 this.path = new StringBuilder(path);
60 this.returnType = returnType;
61 header("Accept", "application/json");
64 public CloudifyRequest<R> endpoint(String endpoint) {
65 this.endpoint = endpoint;
69 public String endpoint() {
73 public CloudifyRequest<R> method(HttpMethod method) {
78 public HttpMethod method() {
82 public CloudifyRequest<R> path(String path) {
83 this.path.append(path);
87 public String path() {
88 return path.toString();
91 public CloudifyRequest<R> header(String name, Object value) {
93 headers.put(name, Arrays.asList(value));
98 public Map<String, List<Object>> headers() {
102 public <T> Entity<T> entity(T entity, String contentType) {
103 return new Entity<>(entity, contentType);
106 public Entity<?> entity() {
110 public <T> Entity<T> json(T entity) {
111 return entity(entity, "application/json");
114 public void returnType(Class<R> returnType) {
115 this.returnType = returnType;
118 public Class<R> returnType() {
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.
126 public void setBasicAuthentication(String user, String password) {
127 this.basicAuth = true;
129 this.password = password;
132 public boolean isBasicAuth() {
133 return this.basicAuth;
136 public String getUser() {
140 public String getPassword() {
145 return client.execute(this);
148 public CloudifyResponse request() {
149 return client.request(this);
155 * @see java.lang.Object#toString()
158 public String toString() {
159 return "CloudifyRequest [endpoint=" + endpoint + ", method=" + method + ", path=" + path + ", headers="
160 + headers + ", entity=" + entity + ", returnType=" + returnType + "]";
163 private Map<String, List<Object>> queryParams = new LinkedHashMap<>();
165 public Map<String, List<Object>> queryParams() {
169 public CloudifyRequest<R> queryParam(String key, Object value) {
170 if (queryParams.containsKey(key)) {
171 List<Object> values = queryParams.get(key);
174 List<Object> values = new ArrayList<>();
176 queryParams.put(key, values);
182 protected static String buildPath(String... elements) {
183 StringBuilder stringBuilder = new StringBuilder();
184 for (String element : elements) {
185 stringBuilder.append(element);
188 return stringBuilder.toString();