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.aaf.v2_0;
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
28 import org.onap.aaf.cadi.Access;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.Locator;
31 import org.onap.aaf.cadi.Locator.Item;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.SecuritySetter;
34 import org.onap.aaf.cadi.client.AbsTransferSS;
35 import org.onap.aaf.cadi.client.Rcli;
36 import org.onap.aaf.cadi.client.Retryable;
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.config.SecurityInfoC;
39 import org.onap.aaf.cadi.http.HBasicAuthSS;
40 import org.onap.aaf.cadi.http.HMangr;
41 import org.onap.aaf.cadi.http.HRcli;
42 import org.onap.aaf.cadi.http.HTokenSS;
43 import org.onap.aaf.cadi.http.HTransferSS;
44 import org.onap.aaf.cadi.http.HX509SS;
45 import org.onap.aaf.cadi.principal.BasicPrincipal;
46 import org.onap.aaf.cadi.principal.TaggedPrincipal;
47 import org.onap.aaf.misc.env.APIException;
49 public class AAFConHttp extends AAFCon<HttpURLConnection> {
50 private final HMangr hman;
52 public AAFConHttp(Access access) throws APIException, CadiException, LocatorException {
53 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 public static SecuritySetter<HttpURLConnection> bestSS(SecurityInfoC<HttpURLConnection> si) throws APIException, CadiException {
59 Access access = si.access;
61 if((s = access.getProperty(Config.CADI_ALIAS, null))!=null) {
62 return new HX509SS(s,si,true);
63 } else if((s = access.getProperty(Config.AAF_APPID, null))!=null){
65 return new HBasicAuthSS(si,true);
66 } catch (IOException /*| GeneralSecurityException*/ e) {
67 throw new CadiException(e);
73 public AAFConHttp(Access access, String tag) throws APIException, CadiException, LocatorException {
74 super(access,tag,SecurityInfoC.instance(access, HttpURLConnection.class));
76 hman = new HMangr(access,Config.loadLocator(si, access.getProperty(tag,tag/*try the content itself*/)));
79 public AAFConHttp(Access access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException, APIException, LocatorException {
80 super(access,urlTag,si);
82 hman = new HMangr(access,Config.loadLocator(si, access.getProperty(urlTag,null)));
85 public AAFConHttp(Access access, Locator<URI> locator) throws CadiException, LocatorException, APIException {
86 super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
88 hman = new HMangr(access,locator);
91 public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException {
92 super(access,Config.AAF_URL,si);
93 hman = new HMangr(access,locator);
96 public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException, LocatorException {
98 hman = new HMangr(access, locator);
101 private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) throws LocatorException {
103 hman = new HMangr(aafcon.access,Config.loadLocator(si, url));
107 public AAFCon<HttpURLConnection> clone(String url) throws LocatorException {
108 return new AAFConHttp(this,url);
112 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)
115 public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {
116 if(password.startsWith("enc:")) {
118 password = access.decrypt(password, true);
119 } catch (IOException e) {
120 throw new CadiException("Error decrypting password",e);
124 return new HBasicAuthSS(si,user,password);
125 } catch (IOException e) {
126 throw new CadiException("Error creating HBasicAuthSS",e);
130 public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws APIException, CadiException {
132 return set(new HX509SS(alias,si));
133 } catch (Exception e) {
134 throw new CadiException("Error creating X509SS",e);
139 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, org.onap.aaf.cadi.SecuritySetter)
142 protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {
144 throw new CadiException("No Locator set in AAFConHttp");
147 return new HRcli(hman, hman.loc.best() ,ss);
148 } catch (Exception e) {
149 throw new CadiException(e);
154 public Rcli<HttpURLConnection> rclient(Locator<URI> loc, SecuritySetter<HttpURLConnection> ss) throws CadiException {
156 HMangr newHMan = new HMangr(access, loc);
157 return new HRcli(newHMan,newHMan.loc.best(),ss);
158 } catch (Exception e) {
159 throw new CadiException(e);
163 public AbsTransferSS<HttpURLConnection> transferSS(TaggedPrincipal principal) throws CadiException {
164 return new HTransferSS(principal, app,si);
168 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)
171 public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {
173 return new HBasicAuthSS(principal,si);
174 } catch (IOException e) {
175 throw new CadiException("Error creating HBasicAuthSS",e);
180 public SecuritySetter<HttpURLConnection> tokenSS(final String client_id, final String accessToken) throws CadiException {
182 return new HTokenSS(si, client_id, accessToken);
183 } catch (IOException e) {
184 throw new CadiException(e);
188 public HMangr hman() {
193 public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
194 return hman.best(ss, (Retryable<RET>)retryable);
198 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#bestForUser(org.onap.aaf.cadi.SecuritySetter, org.onap.aaf.cadi.client.Retryable)
201 public <RET> RET bestForUser(GetSetter getSetter, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
202 return hman.best(getSetter.get(this), (Retryable<RET>)retryable);
206 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#initURI()
209 protected URI initURI() {
211 Item item = hman.loc.best();
213 return hman.loc.get(item);
215 } catch (LocatorException e) {
216 access.log(e, "Error in AAFConHttp obtaining initial URI");
222 * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)
225 protected void setInitURI(String uriString) throws CadiException {
226 // Using Locator, not URLString, which is mostly for DME2