Update AAF Version 1.0.0
[aaf/cadi.git] / aaf / src / main / java / com / att / 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 com.att.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 com.att.cadi.CadiException;\r
32 import com.att.cadi.Locator;\r
33 import com.att.cadi.Locator.Item;\r
34 import com.att.cadi.LocatorException;\r
35 import com.att.cadi.PropAccess;\r
36 import com.att.cadi.SecuritySetter;\r
37 import com.att.cadi.client.AbsTransferSS;\r
38 import com.att.cadi.client.Rcli;\r
39 import com.att.cadi.client.Retryable;\r
40 import com.att.cadi.config.Config;\r
41 import com.att.cadi.config.SecurityInfoC;\r
42 import com.att.cadi.http.HBasicAuthSS;\r
43 import com.att.cadi.http.HMangr;\r
44 import com.att.cadi.http.HRcli;\r
45 import com.att.cadi.http.HTransferSS;\r
46 import com.att.cadi.http.HX509SS;\r
47 import com.att.cadi.principal.BasicPrincipal;\r
48 import com.att.inno.env.APIException;\r
49 \r
50 public class AAFConHttp extends AAFCon<HttpURLConnection> {\r
51         private final HMangr hman;\r
52 \r
53         public AAFConHttp(PropAccess access) throws CadiException, GeneralSecurityException, IOException {\r
54                 super(access,Config.AAF_URL,new SecurityInfoC<HttpURLConnection>(access));\r
55                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(Config.AAF_URL,null)));\r
56         }\r
57 \r
58         public AAFConHttp(PropAccess access, String tag) throws CadiException, GeneralSecurityException, IOException {\r
59                 super(access,tag,new SecurityInfoC<HttpURLConnection>(access));\r
60                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(tag,null)));\r
61         }\r
62 \r
63         public AAFConHttp(PropAccess access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException {\r
64                 super(access,urlTag,si);\r
65                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(urlTag,null)));\r
66         }\r
67 \r
68         public AAFConHttp(PropAccess access, Locator<URI> locator) throws CadiException, GeneralSecurityException, IOException {\r
69                 super(access,Config.AAF_URL,new SecurityInfoC<HttpURLConnection>(access));\r
70                 hman = new HMangr(access,locator);\r
71         }\r
72 \r
73         public AAFConHttp(PropAccess access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException {\r
74                 super(access,Config.AAF_URL,si);\r
75                 hman = new HMangr(access,locator);\r
76         }\r
77 \r
78         public AAFConHttp(PropAccess access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException {\r
79                 super(access,tag,si);\r
80                 hman = new HMangr(access, locator);\r
81         }\r
82         \r
83         private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) {\r
84                 super(aafcon);\r
85                 hman = new HMangr(aafcon.access,Config.loadLocator(access, url));\r
86         }\r
87 \r
88         @Override\r
89         public AAFCon<HttpURLConnection> clone(String url) {\r
90                 return new AAFConHttp(this,url);\r
91         }\r
92 \r
93         /* (non-Javadoc)\r
94          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
95          */\r
96         @Override\r
97         public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {\r
98                 if(password.startsWith("enc:???")) {\r
99                         try {\r
100                                 password = access.decrypt(password, true);\r
101                         } catch (IOException e) {\r
102                                 throw new CadiException("Error decrypting password",e);\r
103                         }\r
104                 }\r
105                 try {\r
106                         return new HBasicAuthSS(user,password,si);\r
107                 } catch (IOException e) {\r
108                         throw new CadiException("Error creating HBasicAuthSS",e);\r
109                 }\r
110         }\r
111 \r
112         public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws APIException, CadiException {\r
113                 try {\r
114                         return set(new HX509SS(alias,si));\r
115                 } catch (Exception e) {\r
116                         throw new CadiException("Error creating X509SS",e);\r
117                 }\r
118         }\r
119 \r
120         /* (non-Javadoc)\r
121          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
122          */\r
123         @Override\r
124         protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {\r
125                 if(hman.loc==null) {\r
126                         throw new CadiException("No Locator set in AAFConHttp"); \r
127                 }\r
128                 try {\r
129                         return new HRcli(hman, hman.loc.best() ,ss);\r
130                 } catch (Exception e) {\r
131                         throw new CadiException(e);\r
132                 }\r
133         }\r
134 \r
135         @Override\r
136         public AbsTransferSS<HttpURLConnection> transferSS(Principal principal) throws CadiException {\r
137                 return new HTransferSS(principal, app,si);\r
138         }\r
139         \r
140         /* (non-Javadoc)\r
141          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)\r
142          */\r
143         @Override\r
144         public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
145                 try {\r
146                         return new HBasicAuthSS(principal,si);\r
147                 } catch (IOException e) {\r
148                         throw new CadiException("Error creating HBasicAuthSS",e);\r
149                 }\r
150         }\r
151 \r
152         public HMangr hman() {\r
153                 return hman;\r
154         }\r
155 \r
156         @Override\r
157         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
158                 return hman.best(ss, (Retryable<RET>)retryable);\r
159         }\r
160 \r
161         /* (non-Javadoc)\r
162          * @see com.att.cadi.aaf.v2_0.AAFCon#initURI()\r
163          */\r
164         @Override\r
165         protected URI initURI() {\r
166                 try {\r
167                         Item item = hman.loc.best();\r
168                         if(item!=null) {\r
169                                 return hman.loc.get(item);\r
170                         }\r
171                 } catch (LocatorException e) {\r
172                         access.log(e, "Error in AAFConHttp obtaining initial URI");\r
173                 }\r
174                 return null;\r
175         }\r
176 \r
177         /* (non-Javadoc)\r
178          * @see com.att.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)\r
179          */\r
180         @Override\r
181         protected void setInitURI(String uriString) throws CadiException {\r
182                 // TODO Auto-generated method stub\r
183                 \r
184         }\r
185         \r
186 }\r