Replaced all tabs with spaces in java and pom.xml
[so.git] / cloudify-client / src / main / java / org / onap / so / cloudify / base / client / CloudifyClient.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.Properties;
24 import org.onap.so.cloudify.connector.http.HttpClientConnector;
25
26 public class CloudifyClient {
27
28     protected String managerEndpoint;
29     protected String tenant = "default_tenant"; // Note - only default_tenant supported in community edition
30
31     protected CloudifyTokenProvider tokenProvider;
32
33     protected static int AUTHENTICATION_RETRIES = 1;
34
35     protected CloudifyClientConnector connector;
36
37     protected Properties properties = new Properties();
38
39     public CloudifyClient(String managerEndpoint) {
40         this.managerEndpoint = managerEndpoint;
41         this.connector = new HttpClientConnector();
42     }
43
44     public CloudifyClient(String managerEndpoint, String tenant) {
45         this.managerEndpoint = managerEndpoint;
46         this.tenant = tenant;
47         this.connector = new HttpClientConnector();
48     }
49
50     public CloudifyClient(String managerEndpoint, CloudifyClientConnector connector) {
51         this.managerEndpoint = managerEndpoint;
52         this.connector = connector;
53     }
54
55     /**
56      * Execute a Cloudify request by making the REST API call. Return the complete CloudifyResponse structure, which
57      * includes the complete HTTP response.
58      * 
59      * @param request a CloudifyRequest object
60      * @return a CloudifyResponse object
61      */
62     public <T> CloudifyResponse request(CloudifyRequest<T> request) {
63         CloudifyResponseException authException = null;
64
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());
70
71             try {
72                 return connector.request(request);
73             } catch (CloudifyResponseException e) {
74                 if (e.getStatus() != CloudifyResponseStatus.NOT_AUTHORIZED || tokenProvider == null) {
75                     throw e;
76                 }
77                 authException = e;
78                 tokenProvider.expireToken();
79             }
80         }
81
82         if (authException != null) {
83             throw authException;
84         }
85
86         return null;
87     }
88
89     /**
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.
92      * 
93      * @param request
94      * @return a JSON POJO object specific to the request type
95      */
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())
100                 : null;
101     }
102
103     public void property(String property, String value) {
104         properties.put(property, value);
105     }
106
107     /**
108      * Set a Token Provider. This class should be able to produce an authentication token on-demand.
109      * 
110      * @param tokenProvider
111      */
112     public void setTokenProvider(CloudifyTokenProvider tokenProvider) {
113         this.tokenProvider = tokenProvider;
114     }
115
116     /**
117      * Manually set the authentication token to use for this client.
118      * 
119      * @param token
120      */
121     public void setToken(String token) {
122         setTokenProvider(new CloudifySimpleTokenProvider(token));
123     }
124
125     /**
126      * Perform a simple GET request with no request message body
127      * 
128      * @param path
129      * @param returnType
130      * @return An object of Class <R>
131      */
132     public <R> CloudifyRequest<R> get(String path, Class<R> returnType) {
133         return new CloudifyRequest<R>(this, HttpMethod.GET, path, null, returnType);
134     }
135
136 }