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.Properties;
24 import org.onap.so.cloudify.connector.http.HttpClientConnector;
26 public class CloudifyClient {
28 protected String managerEndpoint;
29 protected String tenant = "default_tenant"; // Note - only default_tenant supported in community edition
31 protected CloudifyTokenProvider tokenProvider;
33 protected static int AUTHENTICATION_RETRIES = 1;
35 protected CloudifyClientConnector connector;
37 protected Properties properties = new Properties();
39 public CloudifyClient(String managerEndpoint) {
40 this.managerEndpoint = managerEndpoint;
41 this.connector = new HttpClientConnector();
44 public CloudifyClient(String managerEndpoint, String tenant) {
45 this.managerEndpoint = managerEndpoint;
47 this.connector = new HttpClientConnector();
50 public CloudifyClient(String managerEndpoint, CloudifyClientConnector connector) {
51 this.managerEndpoint = managerEndpoint;
52 this.connector = connector;
56 * Execute a Cloudify request by making the REST API call. Return the complete CloudifyResponse structure, which
57 * includes the complete HTTP response.
59 * @param request a CloudifyRequest object
60 * @return a CloudifyResponse object
62 public <T> CloudifyResponse request(CloudifyRequest<T> request) {
63 CloudifyResponseException authException = null;
65 for (int i = 0; i <= AUTHENTICATION_RETRIES; i++) {
66 request.endpoint(managerEndpoint);
67 request.header("Tenant", tenant);
68 if (tokenProvider != null)
69 request.header("Authentication-Token", tokenProvider.getToken());
72 return connector.request(request);
73 } catch (CloudifyResponseException e) {
74 if (e.getStatus() != CloudifyResponseStatus.NOT_AUTHORIZED || tokenProvider == null) {
78 tokenProvider.expireToken();
82 if (authException != null) {
90 * Execute a CloudifyRequest by sending the REST API call to the Cloudify Manager endpoint. The return type is a
91 * JSON POJO object containing the response body entity.
94 * @return a JSON POJO object specific to the request type
96 public <T> T execute(CloudifyRequest<T> request) {
97 CloudifyResponse response = request(request);
98 return (request.returnType() != null && request.returnType() != Void.class)
99 ? response.getEntity(request.returnType())
103 public void property(String property, String value) {
104 properties.put(property, value);
108 * Set a Token Provider. This class should be able to produce an authentication token on-demand.
110 * @param tokenProvider
112 public void setTokenProvider(CloudifyTokenProvider tokenProvider) {
113 this.tokenProvider = tokenProvider;
117 * Manually set the authentication token to use for this client.
121 public void setToken(String token) {
122 setTokenProvider(new CloudifySimpleTokenProvider(token));
126 * Perform a simple GET request with no request message body
130 * @return An object of Class <R>
132 public <R> CloudifyRequest<R> get(String path, Class<R> returnType) {
133 return new CloudifyRequest<R>(this, HttpMethod.GET, path, null, returnType);