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