2 * ============LICENSE_START====================================================
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
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====================================================
22 package org.onap.aaf.cadi.oauth;
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
27 import java.net.URISyntaxException;
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;
47 public class TzHClient extends TzClient {
49 public SecurityInfoC<HttpURLConnection> si;
50 private TimedToken token;
51 private SecuritySetter<HttpURLConnection> tokenSS;
53 public TzHClient(Access access, String tagOrURL) throws CadiException, LocatorException {
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);
61 public TzHClient(Access access, SecurityInfoC<HttpURLConnection> hsi, Locator<URI> loc) throws LocatorException {
63 hman = new HMangr(access, loc);
66 public void setToken(final String client_id, TimedToken token) throws IOException {
68 tokenSS = new HTokenSS(si, client_id, token.getAccessToken());
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");
75 if (token.expired()) {
77 throw new CadiException("Expired Token");
79 return hman.best(tokenSS, retryable);