Add Notice of aaf/cadi source moving to aaf/authz
[aaf/cadi.git] / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AAFConHttp.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.aaf.v2_0;\r
24 \r
25 import java.io.IOException;\r
26 import java.net.HttpURLConnection;\r
27 import java.net.URI;\r
28 import java.security.GeneralSecurityException;\r
29 import java.security.Principal;\r
30 \r
31 import org.onap.aaf.cadi.CadiException;\r
32 import org.onap.aaf.cadi.Locator;\r
33 import org.onap.aaf.cadi.LocatorException;\r
34 import org.onap.aaf.cadi.PropAccess;\r
35 import org.onap.aaf.cadi.SecuritySetter;\r
36 import org.onap.aaf.cadi.Locator.Item;\r
37 import org.onap.aaf.cadi.client.AbsTransferSS;\r
38 import org.onap.aaf.cadi.client.Rcli;\r
39 import org.onap.aaf.cadi.client.Retryable;\r
40 import org.onap.aaf.cadi.config.Config;\r
41 import org.onap.aaf.cadi.config.SecurityInfoC;\r
42 import org.onap.aaf.cadi.http.HBasicAuthSS;\r
43 import org.onap.aaf.cadi.http.HMangr;\r
44 import org.onap.aaf.cadi.http.HRcli;\r
45 import org.onap.aaf.cadi.http.HTransferSS;\r
46 import org.onap.aaf.cadi.http.HX509SS;\r
47 import org.onap.aaf.cadi.principal.BasicPrincipal;\r
48 \r
49 import org.onap.aaf.inno.env.APIException;\r
50 \r
51 public class AAFConHttp extends AAFCon<HttpURLConnection> {\r
52         private final HMangr hman;\r
53 \r
54         public AAFConHttp(PropAccess access) throws CadiException, GeneralSecurityException, IOException {\r
55                 super(access,Config.AAF_URL,new SecurityInfoC<HttpURLConnection>(access));\r
56                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(Config.AAF_URL,null)));\r
57         }\r
58 \r
59         public AAFConHttp(PropAccess access, String tag) throws CadiException, GeneralSecurityException, IOException {\r
60                 super(access,tag,new SecurityInfoC<HttpURLConnection>(access));\r
61                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(tag,null)));\r
62         }\r
63 \r
64         public AAFConHttp(PropAccess access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException {\r
65                 super(access,urlTag,si);\r
66                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(urlTag,null)));\r
67         }\r
68 \r
69         public AAFConHttp(PropAccess access, Locator<URI> locator) throws CadiException, GeneralSecurityException, IOException {\r
70                 super(access,Config.AAF_URL,new SecurityInfoC<HttpURLConnection>(access));\r
71                 hman = new HMangr(access,locator);\r
72         }\r
73 \r
74         public AAFConHttp(PropAccess access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException {\r
75                 super(access,Config.AAF_URL,si);\r
76                 hman = new HMangr(access,locator);\r
77         }\r
78 \r
79         public AAFConHttp(PropAccess access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException {\r
80                 super(access,tag,si);\r
81                 hman = new HMangr(access, locator);\r
82         }\r
83         \r
84         private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) {\r
85                 super(aafcon);\r
86                 hman = new HMangr(aafcon.access,Config.loadLocator(access, url));\r
87         }\r
88 \r
89         @Override\r
90         public AAFCon<HttpURLConnection> clone(String url) {\r
91                 return new AAFConHttp(this,url);\r
92         }\r
93 \r
94         /* (non-Javadoc)\r
95          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
96          */\r
97         @Override\r
98         public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {\r
99                 if(password.startsWith("enc:???")) {\r
100                         try {\r
101                                 password = access.decrypt(password, true);\r
102                         } catch (IOException e) {\r
103                                 throw new CadiException("Error decrypting password",e);\r
104                         }\r
105                 }\r
106                 try {\r
107                         return new HBasicAuthSS(user,password,si);\r
108                 } catch (IOException e) {\r
109                         throw new CadiException("Error creating HBasicAuthSS",e);\r
110                 }\r
111         }\r
112 \r
113         public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws APIException, CadiException {\r
114                 try {\r
115                         return set(new HX509SS(alias,si));\r
116                 } catch (Exception e) {\r
117                         throw new CadiException("Error creating X509SS",e);\r
118                 }\r
119         }\r
120 \r
121         /* (non-Javadoc)\r
122          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
123          */\r
124         @Override\r
125         protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {\r
126                 if(hman.loc==null) {\r
127                         throw new CadiException("No Locator set in AAFConHttp"); \r
128                 }\r
129                 try {\r
130                         return new HRcli(hman, hman.loc.best() ,ss);\r
131                 } catch (Exception e) {\r
132                         throw new CadiException(e);\r
133                 }\r
134         }\r
135 \r
136         @Override\r
137         public AbsTransferSS<HttpURLConnection> transferSS(Principal principal) throws CadiException {\r
138                 return new HTransferSS(principal, app,si);\r
139         }\r
140         \r
141         /* (non-Javadoc)\r
142          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)\r
143          */\r
144         @Override\r
145         public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
146                 try {\r
147                         return new HBasicAuthSS(principal,si);\r
148                 } catch (IOException e) {\r
149                         throw new CadiException("Error creating HBasicAuthSS",e);\r
150                 }\r
151         }\r
152 \r
153         public HMangr hman() {\r
154                 return hman;\r
155         }\r
156 \r
157         @Override\r
158         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
159                 return hman.best(ss, (Retryable<RET>)retryable);\r
160         }\r
161 \r
162         /* (non-Javadoc)\r
163          * @see com.att.cadi.aaf.v2_0.AAFCon#initURI()\r
164          */\r
165         @Override\r
166         protected URI initURI() {\r
167                 try {\r
168                         Item item = hman.loc.best();\r
169                         if(item!=null) {\r
170                                 return hman.loc.get(item);\r
171                         }\r
172                 } catch (LocatorException e) {\r
173                         access.log(e, "Error in AAFConHttp obtaining initial URI");\r
174                 }\r
175                 return null;\r
176         }\r
177 \r
178         /* (non-Javadoc)\r
179          * @see com.att.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)\r
180          */\r
181         @Override\r
182         protected void setInitURI(String uriString) throws CadiException {\r
183                 // TODO Auto-generated method stub\r
184                 \r
185         }\r
186         \r
187 }\r