[AAF-21] Initial code import
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / aaf / v2_0 / AAFConHttp.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.HttpURLConnection;\r
28 import java.net.URI;\r
29 import java.security.GeneralSecurityException;\r
30 import java.security.Principal;\r
31 \r
32 import com.att.cadi.Access;\r
33 import com.att.cadi.CadiException;\r
34 import com.att.cadi.Locator;\r
35 import com.att.cadi.LocatorException;\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.SecurityInfo;\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(Access access) throws CadiException, GeneralSecurityException, IOException {\r
54                 super(access,Config.AAF_URL,new SecurityInfo<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(Access access, String tag) throws CadiException, GeneralSecurityException, IOException {\r
59                 super(access,tag,new SecurityInfo<HttpURLConnection>(access));\r
60                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(tag,null)));\r
61         }\r
62 \r
63         public AAFConHttp(Access access, String urlTag, SecurityInfo<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(Access access, Locator locator) throws CadiException, GeneralSecurityException, IOException {\r
69                 super(access,Config.AAF_URL,new SecurityInfo<HttpURLConnection>(access));\r
70                 hman = new HMangr(access,locator);\r
71         }\r
72 \r
73         public AAFConHttp(Access access, Locator locator, SecurityInfo<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(Access access, Locator locator, SecurityInfo<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         /* (non-Javadoc)\r
84          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
85          */\r
86         @Override\r
87         public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {\r
88                 if(password.startsWith("enc:???")) {\r
89                         try {\r
90                                 password = access.decrypt(password, true);\r
91                         } catch (IOException e) {\r
92                                 throw new CadiException("Error decrypting password",e);\r
93                         }\r
94                 }\r
95                 try {\r
96                         return set(new HBasicAuthSS(user,password,si));\r
97                 } catch (IOException e) {\r
98                         throw new CadiException("Error creating HBasicAuthSS",e);\r
99                 }\r
100         }\r
101 \r
102         public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws APIException, CadiException {\r
103                 try {\r
104                         return set(new HX509SS(alias,si));\r
105                 } catch (Exception e) {\r
106                         throw new CadiException("Error creating X509SS",e);\r
107                 }\r
108         }\r
109 \r
110         /* (non-Javadoc)\r
111          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
112          */\r
113         @Override\r
114         protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {\r
115                 try {\r
116                         return new HRcli(hman, hman.loc.best() ,ss);\r
117                 } catch (Exception e) {\r
118                         throw new CadiException(e);\r
119                 }\r
120         }\r
121 \r
122         @Override\r
123         public AbsTransferSS<HttpURLConnection> transferSS(Principal principal) throws CadiException {\r
124                 return new HTransferSS(principal, app,si);\r
125         }\r
126         \r
127         /* (non-Javadoc)\r
128          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)\r
129          */\r
130         @Override\r
131         public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
132                 try {\r
133                         return new HBasicAuthSS(principal,si);\r
134                 } catch (IOException e) {\r
135                         throw new CadiException("Error creating HBasicAuthSS",e);\r
136                 }\r
137         }\r
138 \r
139         public HMangr hman() {\r
140                 return hman;\r
141         }\r
142 \r
143         @Override\r
144         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
145                 return hman.best(ss, (Retryable<RET>)retryable);\r
146         }\r
147         \r
148 }\r