[AAF-21] Initial code import
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / aaf / v2_0 / AAFConDME2.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.aaf.v2_0;\r
25 \r
26 import java.io.IOException;\r
27 import java.net.ConnectException;\r
28 import java.net.URI;\r
29 import java.net.URISyntaxException;\r
30 import java.security.GeneralSecurityException;\r
31 import java.security.Principal;\r
32 import java.util.Properties;\r
33 \r
34 import com.att.aft.dme2.api.DME2Client;\r
35 import com.att.aft.dme2.api.DME2Exception;\r
36 import com.att.aft.dme2.api.DME2Manager;\r
37 import com.att.cadi.CadiException;\r
38 import com.att.cadi.LocatorException;\r
39 import com.att.cadi.PropAccess;\r
40 import com.att.cadi.SecuritySetter;\r
41 import com.att.cadi.client.Rcli;\r
42 import com.att.cadi.client.Retryable;\r
43 import com.att.cadi.config.Config;\r
44 import com.att.cadi.config.SecurityInfoC;\r
45 import com.att.cadi.dme2.DME2BasicAuth;\r
46 import com.att.cadi.dme2.DME2TransferSS;\r
47 import com.att.cadi.dme2.DME2x509SS;\r
48 import com.att.cadi.dme2.DRcli;\r
49 import com.att.cadi.principal.BasicPrincipal;\r
50 import com.att.inno.env.APIException;\r
51 \r
52 public class AAFConDME2 extends AAFCon<DME2Client>{\r
53         private DME2Manager manager;\r
54         private boolean isProxy;\r
55         private URI initURI;\r
56 \r
57         public AAFConDME2(PropAccess access) throws CadiException, GeneralSecurityException, IOException{\r
58                 super(access,Config.AAF_URL,new SecurityInfoC<DME2Client> (access));\r
59                 manager = newManager(access);\r
60                 setIsProxy();\r
61         }\r
62         \r
63         public AAFConDME2(PropAccess access, String url) throws CadiException, GeneralSecurityException, IOException{\r
64                 super(access,url,new SecurityInfoC<DME2Client> (access));\r
65                 manager = newManager(access);\r
66                 setIsProxy();\r
67         }\r
68 \r
69         public AAFConDME2(PropAccess access, SecurityInfoC<DME2Client> si) throws CadiException {\r
70                 super(access,Config.AAF_URL,si);\r
71                 manager = newManager(access);\r
72                 setIsProxy();\r
73         }\r
74 \r
75         public AAFConDME2(PropAccess access, String url, SecurityInfoC<DME2Client> si) throws CadiException {\r
76                 super(access,url,si);\r
77                 manager = newManager(access);\r
78                 setIsProxy();\r
79         }\r
80 \r
81         /**\r
82         *  Construct a Connector based on the AAF one.  This is for remote access to OTHER than AAF,\r
83         *  but using Credentials, etc\r
84         */ \r
85         private AAFConDME2(AAFCon<DME2Client> aafcon, String url) throws CadiException {\r
86                 super(aafcon);\r
87                 try {\r
88                         initURI = new URI(url);\r
89                 } catch (URISyntaxException e) {\r
90                         throw new CadiException(e);\r
91                 }\r
92                 manager = newManager(access);\r
93         }\r
94         \r
95         /**\r
96         *  Create a Connector based on the AAF one.  This is for remote access to OTHER than AAF,\r
97         *  but using Credentials, etc\r
98         */ \r
99         public AAFCon<DME2Client> clone(String url) throws CadiException {\r
100                 return new AAFConDME2(this,url);\r
101         }\r
102         \r
103         private void setIsProxy() {\r
104                 String str;\r
105                 if((str=access.getProperty(Config.AAF_URL, null))!=null) {\r
106                         isProxy = str.contains("service=com.att.authz.authz-gw/version=");\r
107                 }\r
108         }\r
109 \r
110         private DME2Manager newManager(PropAccess access) throws CadiException {\r
111                 Properties props = access.getDME2Properties();\r
112                 // Critical that TLS Settings not ignored\r
113                 try {\r
114                         return new DME2Manager("AAFCon",props);\r
115                 } catch (DME2Exception e) {\r
116                         throw new CadiException(e);\r
117                 }\r
118         }\r
119 \r
120 \r
121         /* (non-Javadoc)\r
122          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
123          */\r
124         @Override\r
125         public SecuritySetter<DME2Client> basicAuth(String user, String password) throws CadiException {\r
126                 if(password.startsWith("enc:???")) {\r
127                         try {\r
128                                 password = access.decrypt(password, true);\r
129                         } catch (IOException e) {\r
130                                 throw new CadiException("Error Decrypting Password",e);\r
131                         }\r
132                 }\r
133 \r
134                 try {\r
135                         return set(new DME2BasicAuth(user,password,si));\r
136                 } catch (IOException e) {\r
137                         throw new CadiException("Error setting up DME2BasicAuth",e);\r
138                 }\r
139         }\r
140 \r
141         /* (non-Javadoc)\r
142          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
143          */\r
144         @Override\r
145         protected Rcli<DME2Client> rclient(URI uri, SecuritySetter<DME2Client> ss) {\r
146                 DRcli dc = new DRcli(uri, ss);\r
147                 dc.setProxy(isProxy);\r
148                 dc.setManager(manager);\r
149                 return dc;\r
150         }\r
151 \r
152         @Override\r
153         public SecuritySetter<DME2Client> transferSS(Principal principal) throws CadiException {\r
154                 try {\r
155                         return principal==null?ss:new DME2TransferSS(principal, app, si);\r
156                 } catch (IOException e) {\r
157                         throw new CadiException("Error creating DME2TransferSS",e);\r
158                 }\r
159         }\r
160 \r
161         @Override\r
162         public SecuritySetter<DME2Client> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
163                 try {\r
164                         return new DME2BasicAuth(principal,si);\r
165                 } catch (IOException e) {\r
166                         throw new CadiException("Error creating DME2BasicAuth",e);\r
167                 }\r
168 \r
169         }\r
170 \r
171         @Override\r
172         public SecuritySetter<DME2Client> x509Alias(String alias) throws CadiException {\r
173                 try {\r
174                         presetProps(access, alias);\r
175                         return new DME2x509SS(alias,si);\r
176                 } catch (Exception e) {\r
177                         throw new CadiException("Error creating DME2x509SS",e);\r
178                 }\r
179         }\r
180 \r
181         @Override\r
182         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
183                 // NOTE: DME2 had Retry Logic embedded lower.  \r
184                 try {\r
185                         return (retryable.code(rclient(initURI,ss)));\r
186                 } catch (ConnectException e) {\r
187                         // DME2 should catch\r
188                         try {\r
189                                 manager.refresh();\r
190                         } catch (Exception e1) {\r
191                                 throw new CadiException(e1);\r
192                         }\r
193                         throw new CadiException(e);\r
194                 }\r
195         }\r
196         \r
197         public static void presetProps(PropAccess access, String alias) throws IOException {\r
198                 System.setProperty(Config.AFT_DME2_CLIENT_SSL_CERT_ALIAS, alias);\r
199                 if(System.getProperty(Config.AFT_DME2_CLIENT_IGNORE_SSL_CONFIG)==null) {\r
200                         access.getDME2Properties();\r
201                 }\r
202 \r
203         }\r
204 \r
205         /* (non-Javadoc)\r
206          * @see com.att.cadi.aaf.v2_0.AAFCon#initURI()\r
207          */\r
208         @Override\r
209         protected URI initURI() {\r
210                 return initURI;\r
211         }\r
212 \r
213         /* (non-Javadoc)\r
214          * @see com.att.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)\r
215          */\r
216         @Override\r
217         protected void setInitURI(String uriString) throws CadiException {\r
218                 try {\r
219                         initURI = new URI(uriString);\r
220                 } catch (URISyntaxException e) {\r
221                         throw new CadiException(e);\r
222                 }\r
223         }\r
224 }\r