c07332d1a4c00cdabc9acd28c7e2512684fec48a
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / TzHClient.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.cadi.oauth;
23
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.Locator;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.SecuritySetter;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
35 import org.onap.aaf.cadi.client.Retryable;
36 import org.onap.aaf.cadi.config.SecurityInfoC;
37 import org.onap.aaf.cadi.http.HMangr;
38 import org.onap.aaf.cadi.http.HTokenSS;
39 import org.onap.aaf.misc.env.APIException;
40
41 /**
42  * Tokenized HClient
43  * 
44  * @author Jonathan
45  *
46  */
47 public class TzHClient extends TzClient {
48     private HMangr hman;
49     public SecurityInfoC<HttpURLConnection> si;
50     private TimedToken token;
51     private SecuritySetter<HttpURLConnection> tokenSS;
52
53     public TzHClient(Access access, String tagOrURL) throws CadiException, LocatorException {
54         try {
55             si = SecurityInfoC.instance(access, HttpURLConnection.class);
56             hman = new HMangr(access, new AAFLocator(si,new URI(access.getProperty(tagOrURL, tagOrURL))));
57         } catch (URISyntaxException e) {
58             throw new CadiException(e);
59         }
60     }
61     public TzHClient(Access access, SecurityInfoC<HttpURLConnection> hsi, Locator<URI> loc) throws LocatorException {
62         si = hsi;
63         hman = new HMangr(access, loc);
64     }
65     
66     public void setToken(final String client_id, TimedToken token) throws IOException {
67         this.token = token;
68         tokenSS = new HTokenSS(si, client_id, token.getAccessToken());
69     }
70
71     public <RET> RET best (Retryable<RET> retryable) throws CadiException, LocatorException, APIException {
72         if (token == null || tokenSS==null) {
73             throw new CadiException("OAuth2 Token has not been set");
74         }
75         if (token.expired()) {
76             //TODO Refresh?
77             throw new CadiException("Expired Token");
78         } else {
79             return hman.best(tokenSS, retryable);
80         }
81     }
82 }