2 r * ============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.aaf.v2_0;
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
27 import java.util.List;
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.Locator.Item;
33 import org.onap.aaf.cadi.LocatorException;
34 import org.onap.aaf.cadi.SecuritySetter;
35 import org.onap.aaf.cadi.client.AbsTransferSS;
36 import org.onap.aaf.cadi.client.Rcli;
37 import org.onap.aaf.cadi.client.Retryable;
38 import org.onap.aaf.cadi.config.Config;
39 import org.onap.aaf.cadi.config.SecurityInfoC;
40 import org.onap.aaf.cadi.http.HBasicAuthSS;
41 import org.onap.aaf.cadi.http.HMangr;
42 import org.onap.aaf.cadi.http.HRcli;
43 import org.onap.aaf.cadi.http.HTokenSS;
44 import org.onap.aaf.cadi.http.HTransferSS;
45 import org.onap.aaf.cadi.http.HX509SS;
46 import org.onap.aaf.cadi.principal.BasicPrincipal;
47 import org.onap.aaf.cadi.principal.TaggedPrincipal;
48 import org.onap.aaf.misc.env.APIException;
50 public class AAFConHttp extends AAFCon<HttpURLConnection> {
51 private final HMangr hman;
53 public AAFConHttp(Access access) throws CadiException, LocatorException {
54 super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
55 hman = new HMangr(access,Config.loadLocator(si, access.getProperty(Config.AAF_URL,null)));
58 protected SecuritySetter<HttpURLConnection> bestSS(SecurityInfoC<HttpURLConnection> si) {
62 public AAFConHttp(Access access, String tag) throws CadiException, LocatorException {
63 super(access,tag,SecurityInfoC.instance(access, HttpURLConnection.class));
64 hman = new HMangr(access,Config.loadLocator(si, access.getProperty(tag,tag/*try the content itself*/)));
67 public AAFConHttp(Access access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException {
68 super(access,urlTag,si);
69 hman = new HMangr(access,Config.loadLocator(si, access.getProperty(urlTag,null)));
72 public AAFConHttp(Access access, Locator<URI> locator) throws CadiException, LocatorException {
73 super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
74 hman = new HMangr(access,locator);
77 public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException, APIException {
78 super(access,Config.AAF_URL,si);
79 hman = new HMangr(access,locator);
82 public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException, LocatorException, APIException {
84 hman = new HMangr(access, locator);
87 private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) throws LocatorException {
90 hman = new HMangr(aafcon.access,Config.loadLocator(si, url));
94 public AAFCon<HttpURLConnection> clone(String url) throws LocatorException {
95 return new AAFConHttp(this,url);
99 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)
102 public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {
103 if (password.startsWith("enc:")) {
105 password = access.decrypt(password, true);
106 } catch (IOException e) {
107 throw new CadiException("Error decrypting password",e);
111 return new HBasicAuthSS(si,user,password);
112 } catch (IOException e) {
113 throw new CadiException("Error creating HBasicAuthSS",e);
117 public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws CadiException {
119 return set(new HX509SS(alias,si));
120 } catch (Exception e) {
121 throw new CadiException("Error creating X509SS",e);
126 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, org.onap.aaf.cadi.SecuritySetter)
129 protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {
130 if (hman.loc==null) {
131 throw new CadiException("No Locator set in AAFConHttp");
134 return new HRcli(hman, hman.loc.best() ,ss);
135 } catch (Exception e) {
136 throw new CadiException(e);
140 protected Rcli<HttpURLConnection> rclient(List<URI> ignoredURIs, SecuritySetter<HttpURLConnection> ss) throws CadiException {
141 if (hman.loc==null) {
142 throw new CadiException("No Locator set in AAFConHttp");
145 if (ignoredURIs.isEmpty()) {
146 return new HRcli(hman, hman.loc.best(), ss);
148 Item item = hman.loc.first();
149 HRcli currentClient = new HRcli(hman, item, ss);
151 item = hman.loc.next(item);
153 while (item != null) {
154 if (!ignoredURIs.contains(currentClient.getURI())) {
157 currentClient = new HRcli(hman, item, ss);
159 item = hman.loc.next(item);
161 return currentClient;
163 } catch (Exception e) {
164 throw new CadiException(e);
169 public Rcli<HttpURLConnection> rclient(Locator<URI> loc, SecuritySetter<HttpURLConnection> ss) throws CadiException {
171 HMangr newHMan = new HMangr(access, loc);
172 return new HRcli(newHMan,newHMan.loc.best(),ss);
173 } catch (Exception e) {
174 throw new CadiException(e);
178 public AbsTransferSS<HttpURLConnection> transferSS(TaggedPrincipal principal) {
179 return new HTransferSS(principal, app,si);
183 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)
186 public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {
188 return new HBasicAuthSS(principal,si);
189 } catch (IOException e) {
190 throw new CadiException("Error creating HBasicAuthSS",e);
195 public SecuritySetter<HttpURLConnection> tokenSS(final String client_id, final String accessToken) throws CadiException {
197 return new HTokenSS(si, client_id, accessToken);
198 } catch (IOException e) {
199 throw new CadiException(e);
203 public HMangr hman() {
208 public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
209 return hman.best(si.defSS, retryable);
213 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#bestForUser(org.onap.aaf.cadi.SecuritySetter, org.onap.aaf.cadi.client.Retryable)
216 public <RET> RET bestForUser(GetSetter getSetter, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
217 return hman.best(getSetter.get(this), retryable);
221 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#initURI()
224 protected URI initURI() {
226 Item item = hman.loc.best();
228 return hman.loc.get(item);
230 } catch (LocatorException e) {
231 access.log(e, "Error in AAFConHttp obtaining initial URI");
237 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)
240 protected void setInitURI(String uriString) {
241 // Using Locator, not URLString, which is mostly for DME2