Replaced all tabs with spaces in java and pom.xml
[so.git] / cloudify-client / src / main / java / org / onap / so / cloudify / base / client / CloudifyClientTokenProvider.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.Date;
24 import org.apache.commons.lang.time.DateUtils;
25 import org.onap.so.cloudify.v3.client.Cloudify;
26 import org.onap.so.cloudify.v3.client.TokensResource.GetToken;
27 import org.onap.so.cloudify.v3.model.Token;
28
29 /**
30  * Cloudify Token Provider that uses the Cloudify client API itself to obtain a token
31  * 
32  * @author JC1348
33  *
34  */
35 public class CloudifyClientTokenProvider implements CloudifyTokenProvider {
36
37     String user;
38     String password;
39     String token;
40     Date expiration;
41     Cloudify cloudify = null;
42
43     public CloudifyClientTokenProvider(String cloudifyEndpoint, String user, String password) {
44         this.user = user;
45         this.password = password;
46
47         cloudify = new Cloudify(cloudifyEndpoint);
48     }
49
50     @Override
51     public String getToken() {
52         Date now = new Date();
53         if (token != null && expiration != null && expiration.after(now)) {
54             return token;
55         }
56
57         // Create a "Get Token" request. Force basic authentication to acquire the token itself.
58         GetToken tokenRequest = cloudify.tokens().token();
59         tokenRequest.setBasicAuthentication(user, password);
60         Token newToken = tokenRequest.execute();
61
62         token = newToken.getValue();
63
64         if (expiration == null) {
65             expiration = new Date();
66         }
67         // TODO: Make this property driven (or see if it comes back somehow in response)
68         expiration = DateUtils.addMinutes(expiration, 10);
69
70         return token;
71     }
72
73     @Override
74     /**
75      * This doesn't actually expire the token in Cloudify. It just prevents this token provider from using it.
76      */
77     public void expireToken() {
78         expiration = null;
79         token = null;
80     }
81
82 }