changed to unmaintained
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AAFConHttp.java
1 /**
2 r * ============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.aaf.v2_0;
23
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
26 import java.net.URI;
27 import java.util.List;
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.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;
49
50 public class AAFConHttp extends AAFCon<HttpURLConnection> {
51     private final HMangr hman;
52
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)));
56     }
57
58     protected SecuritySetter<HttpURLConnection> bestSS(SecurityInfoC<HttpURLConnection> si) {
59         return si.defSS;
60     }
61
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*/)));
65     }
66
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)));
70     }
71
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);
75     }
76
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);
80     }
81
82     public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException, LocatorException, APIException {
83         super(access,tag,si);
84         hman = new HMangr(access, locator);
85     }
86
87     private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) throws LocatorException {
88         super(aafcon);
89         si=aafcon.si;
90         hman = new HMangr(aafcon.access,Config.loadLocator(si, url));
91     }
92
93     @Override
94     public AAFCon<HttpURLConnection> clone(String url) throws LocatorException {
95         return new AAFConHttp(this,url);
96     }
97
98     /* (non-Javadoc)
99      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)
100      */
101     @Override
102     public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {
103         if (password.startsWith("enc:")) {
104             try {
105                 password = access.decrypt(password, true);
106             } catch (IOException e) {
107                 throw new CadiException("Error decrypting password",e);
108             }
109         }
110         try {
111             return new HBasicAuthSS(si,user,password);
112         } catch (IOException e) {
113             throw new CadiException("Error creating HBasicAuthSS",e);
114         }
115     }
116
117     public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws CadiException {
118         try {
119             return set(new HX509SS(alias,si));
120         } catch (Exception e) {
121             throw new CadiException("Error creating X509SS",e);
122         }
123     }
124
125     /* (non-Javadoc)
126      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, org.onap.aaf.cadi.SecuritySetter)
127      */
128     @Override
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");
132         }
133         try {
134             return new HRcli(hman, hman.loc.best() ,ss);
135         } catch (Exception e) {
136             throw new CadiException(e);
137         }
138     }
139
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");
143         }
144         try {
145             if (ignoredURIs.isEmpty()) {
146                 return new HRcli(hman, hman.loc.best(), ss);
147             } else {
148                 Item item = hman.loc.first();
149                 HRcli currentClient = new HRcli(hman, item, ss);
150
151                 item = hman.loc.next(item);
152
153                 while (item != null) {
154                     if (!ignoredURIs.contains(currentClient.getURI())) {
155                         break;
156                     } else {
157                         currentClient = new HRcli(hman, item, ss);
158                     }
159                     item = hman.loc.next(item);
160                 }
161                 return currentClient;
162             }
163         } catch (Exception e) {
164             throw new CadiException(e);
165         }
166     }
167
168     @Override
169     public Rcli<HttpURLConnection> rclient(Locator<URI> loc, SecuritySetter<HttpURLConnection> ss) throws CadiException {
170         try {
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);
175         }
176     }
177     @Override
178     public AbsTransferSS<HttpURLConnection> transferSS(TaggedPrincipal principal) {
179         return new HTransferSS(principal, app,si);
180     }
181
182     /* (non-Javadoc)
183      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)
184      */
185     @Override
186     public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {
187         try {
188             return new HBasicAuthSS(principal,si);
189         } catch (IOException e) {
190             throw new CadiException("Error creating HBasicAuthSS",e);
191         }
192     }
193
194     @Override
195     public SecuritySetter<HttpURLConnection> tokenSS(final String client_id, final String accessToken) throws CadiException {
196         try {
197             return new HTokenSS(si, client_id, accessToken);
198         } catch (IOException e) {
199             throw new CadiException(e);
200         }
201     }
202
203     public HMangr hman() {
204         return hman;
205     }
206
207     @Override
208     public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
209         return hman.best(si.defSS, retryable);
210     }
211
212     /* (non-Javadoc)
213      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#bestForUser(org.onap.aaf.cadi.SecuritySetter, org.onap.aaf.cadi.client.Retryable)
214      */
215     @Override
216     public <RET> RET bestForUser(GetSetter getSetter, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
217         return hman.best(getSetter.get(this), retryable);
218     }
219
220     /* (non-Javadoc)
221      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#initURI()
222      */
223     @Override
224     protected URI initURI() {
225         try {
226             Item item = hman.loc.best();
227             if (item!=null) {
228                 return hman.loc.get(item);
229             }
230         } catch (LocatorException e) {
231             access.log(e, "Error in AAFConHttp obtaining initial URI");
232         }
233         return null;
234     }
235
236     /* (non-Javadoc)
237      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)
238      */
239     @Override
240     protected void setInitURI(String uriString) {
241         // Using Locator, not URLString, which is mostly for DME2
242     }
243
244 }