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