[AAF-21] Updated Copyright Headers for AAF
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / aaf / v2_0 / AAFConDME2.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.ConnectException;\r
27 import java.net.URI;\r
28 import java.security.GeneralSecurityException;\r
29 import java.security.Principal;\r
30 import java.util.Properties;\r
31 \r
32 import com.att.aft.dme2.api.DME2Client;\r
33 import com.att.aft.dme2.api.DME2Exception;\r
34 import com.att.aft.dme2.api.DME2Manager;\r
35 import com.att.cadi.Access;\r
36 import com.att.cadi.CadiException;\r
37 import com.att.cadi.LocatorException;\r
38 import com.att.cadi.SecuritySetter;\r
39 import com.att.cadi.client.Rcli;\r
40 import com.att.cadi.client.Retryable;\r
41 import com.att.cadi.config.Config;\r
42 import com.att.cadi.config.SecurityInfo;\r
43 import com.att.cadi.dme2.DME2BasicAuth;\r
44 import com.att.cadi.dme2.DME2TransferSS;\r
45 import com.att.cadi.dme2.DME2x509SS;\r
46 import com.att.cadi.dme2.DRcli;\r
47 import com.att.cadi.principal.BasicPrincipal;\r
48 import com.att.inno.env.APIException;\r
49 \r
50 public class AAFConDME2 extends AAFCon<DME2Client>{\r
51         private DME2Manager manager;\r
52 \r
53         public AAFConDME2(Access access) throws CadiException, GeneralSecurityException, IOException{\r
54                 super(access,Config.AAF_URL,new SecurityInfo<DME2Client> (access));\r
55                 manager = newManager(access);\r
56         }\r
57         \r
58         public AAFConDME2(Access access, String url) throws CadiException, GeneralSecurityException, IOException{\r
59                 super(access,url,new SecurityInfo<DME2Client> (access));\r
60                 manager = newManager(access);\r
61         }\r
62 \r
63         public AAFConDME2(Access access, SecurityInfo<DME2Client> si) throws CadiException {\r
64                 super(access,Config.AAF_URL,si);\r
65                 manager = newManager(access);\r
66         }\r
67 \r
68         public AAFConDME2(Access access, String url, SecurityInfo<DME2Client> si) throws CadiException {\r
69                 super(access,url,si);\r
70                 manager = newManager(access);\r
71         }\r
72 \r
73         private DME2Manager newManager(Access access) throws CadiException {\r
74                 Properties props = new Properties();\r
75                 Config.cadiToDME2(access, props);\r
76                 try {\r
77                         return new DME2Manager("AAFCon",props);\r
78                 } catch (DME2Exception e) {\r
79                         throw new CadiException(e);\r
80                 }\r
81         }\r
82 \r
83 \r
84         /* (non-Javadoc)\r
85          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
86          */\r
87         @Override\r
88         public SecuritySetter<DME2Client> basicAuth(String user, String password) throws CadiException {\r
89                 if(password.startsWith("enc:???")) {\r
90                         try {\r
91                                 password = access.decrypt(password, true);\r
92                         } catch (IOException e) {\r
93                                 throw new CadiException("Error Decrypting Password",e);\r
94                         }\r
95                 }\r
96 \r
97                 try {\r
98                         return set(new DME2BasicAuth(user,password,si));\r
99                 } catch (IOException e) {\r
100                         throw new CadiException("Error setting up DME2BasicAuth",e);\r
101                 }\r
102         }\r
103 \r
104         /* (non-Javadoc)\r
105          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
106          */\r
107         @Override\r
108         protected Rcli<DME2Client> rclient(URI uri, SecuritySetter<DME2Client> ss) {\r
109                 DRcli dc = new DRcli(uri, ss);\r
110                 dc.setManager(manager);\r
111                 return dc;\r
112         }\r
113 \r
114         @Override\r
115         public SecuritySetter<DME2Client> transferSS(Principal principal) throws CadiException {\r
116                 try {\r
117                         return principal==null?ss:new DME2TransferSS(principal, app);\r
118                 } catch (IOException e) {\r
119                         throw new CadiException("Error creating DME2TransferSS",e);\r
120                 }\r
121         }\r
122 \r
123         @Override\r
124         public SecuritySetter<DME2Client> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
125                 try {\r
126                         return new DME2BasicAuth(principal,si);\r
127                 } catch (IOException e) {\r
128                         throw new CadiException("Error creating DME2BasicAuth",e);\r
129                 }\r
130 \r
131         }\r
132 \r
133         @Override\r
134         public SecuritySetter<DME2Client> x509Alias(String alias) throws CadiException {\r
135                 try {\r
136                         return new DME2x509SS(alias,si);\r
137                 } catch (Exception e) {\r
138                         throw new CadiException("Error creating DME2x509SS",e);\r
139                 }\r
140         }\r
141 \r
142         @Override\r
143         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
144                 // NOTE: DME2 had Retry Logic embedded lower.  \r
145                 try {\r
146                         return (retryable.code(rclient(initURI,ss)));\r
147                 } catch (ConnectException e) {\r
148                         // DME2 should catch\r
149                         try {\r
150                                 manager.refresh();\r
151                         } catch (Exception e1) {\r
152                                 throw new CadiException(e1);\r
153                         }\r
154                         throw new CadiException(e);\r
155                 }\r
156         }\r
157 }\r