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.openecomp.mso.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 public CloudifyRequest() {
38 public CloudifyRequest(CloudifyClient client, HttpMethod method, CharSequence path, Entity<?> entity, Class<R> returnType) {
41 this.path = new StringBuilder(path);
43 this.returnType = returnType;
44 header("Accept", "application/json");
47 private String endpoint;
49 private HttpMethod method;
51 private StringBuilder path = new StringBuilder();
53 private Map<String, List<Object>> headers = new HashMap<String, List<Object>>();
55 private Entity<?> entity;
57 private Class<R> returnType;
59 private boolean basicAuth = false;
60 private String user = null;
61 private String password = null;
63 public CloudifyRequest<R> endpoint(String endpoint) {
64 this.endpoint = endpoint;
68 public String endpoint() {
72 public CloudifyRequest<R> method(HttpMethod method) {
77 public HttpMethod method() {
81 public CloudifyRequest<R> path(String path) {
82 this.path.append(path);
86 public String path() {
87 return path.toString();
90 public CloudifyRequest<R> header(String name, Object value) {
92 headers.put(name, Arrays.asList(value));
97 public Map<String, List<Object>> headers() {
101 public <T> Entity<T> entity(T entity, String contentType) {
102 return new Entity<T>(entity, contentType);
105 public Entity<?> entity() {
109 public <T> Entity<T> json(T entity) {
110 return entity(entity, "application/json");
113 public void returnType(Class<R> returnType) {
114 this.returnType = returnType;
117 public Class<R> returnType() {
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.
125 public void setBasicAuthentication (String user, String password) {
126 this.basicAuth = true;
128 this.password= password;
131 public boolean isBasicAuth () {
132 return this.basicAuth;
135 public String getUser() {
139 public String getPassword() {
144 return client.execute(this);
147 public CloudifyResponse request() {
148 return client.request(this);
152 * @see java.lang.Object#toString()
155 public String toString() {
156 return "CloudifyRequest [endpoint=" + endpoint + ", method=" + method
157 + ", path=" + path + ", headers=" + headers + ", entity="
158 + entity + ", returnType=" + returnType + "]";
161 private Map<String, List<Object> > queryParams = new LinkedHashMap<String, List<Object> >();
163 public Map<String, List<Object> > queryParams() {
167 public CloudifyRequest<R> queryParam(String key, Object value) {
168 if (queryParams.containsKey(key)) {
169 List<Object> values = queryParams.get(key);
172 List<Object> values = new ArrayList<Object>();
174 queryParams.put(key, values);
180 protected static String buildPath(String ... elements) {
181 StringBuilder stringBuilder = new StringBuilder();
182 for (String element : elements) {
183 stringBuilder.append(element);
186 return stringBuilder.toString();