[AAF-21] Updated Copyright Headers for AAF
[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.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.Access;\r
32 import com.att.cadi.CadiException;\r
33 import com.att.cadi.Locator;\r
34 import com.att.cadi.LocatorException;\r
35 import com.att.cadi.SecuritySetter;\r
36 import com.att.cadi.client.AbsTransferSS;\r
37 import com.att.cadi.client.Rcli;\r
38 import com.att.cadi.client.Retryable;\r
39 import com.att.cadi.config.Config;\r
40 import com.att.cadi.config.SecurityInfo;\r
41 import com.att.cadi.http.HBasicAuthSS;\r
42 import com.att.cadi.http.HMangr;\r
43 import com.att.cadi.http.HRcli;\r
44 import com.att.cadi.http.HTransferSS;\r
45 import com.att.cadi.http.HX509SS;\r
46 import com.att.cadi.principal.BasicPrincipal;\r
47 import com.att.inno.env.APIException;\r
48 \r
49 public class AAFConHttp extends AAFCon<HttpURLConnection> {\r
50         private final HMangr hman;\r
51 \r
52         public AAFConHttp(Access access) throws CadiException, GeneralSecurityException, IOException {\r
53                 super(access,Config.AAF_URL,new SecurityInfo<HttpURLConnection>(access));\r
54                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(Config.AAF_URL,null)));\r
55         }\r
56 \r
57         public AAFConHttp(Access access, String tag) throws CadiException, GeneralSecurityException, IOException {\r
58                 super(access,tag,new SecurityInfo<HttpURLConnection>(access));\r
59                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(tag,null)));\r
60         }\r
61 \r
62         public AAFConHttp(Access access, String urlTag, SecurityInfo<HttpURLConnection> si) throws CadiException {\r
63                 super(access,urlTag,si);\r
64                 hman = new HMangr(access,Config.loadLocator(access, access.getProperty(urlTag,null)));\r
65         }\r
66 \r
67         public AAFConHttp(Access access, Locator locator) throws CadiException, GeneralSecurityException, IOException {\r
68                 super(access,Config.AAF_URL,new SecurityInfo<HttpURLConnection>(access));\r
69                 hman = new HMangr(access,locator);\r
70         }\r
71 \r
72         public AAFConHttp(Access access, Locator locator, SecurityInfo<HttpURLConnection> si) throws CadiException {\r
73                 super(access,Config.AAF_URL,si);\r
74                 hman = new HMangr(access,locator);\r
75         }\r
76 \r
77         public AAFConHttp(Access access, Locator locator, SecurityInfo<HttpURLConnection> si, String tag) throws CadiException {\r
78                 super(access,tag,si);\r
79                 hman = new HMangr(access, locator);\r
80         }\r
81 \r
82         /* (non-Javadoc)\r
83          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)\r
84          */\r
85         @Override\r
86         public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {\r
87                 if(password.startsWith("enc:???")) {\r
88                         try {\r
89                                 password = access.decrypt(password, true);\r
90                         } catch (IOException e) {\r
91                                 throw new CadiException("Error decrypting password",e);\r
92                         }\r
93                 }\r
94                 try {\r
95                         return set(new HBasicAuthSS(user,password,si));\r
96                 } catch (IOException e) {\r
97                         throw new CadiException("Error creating HBasicAuthSS",e);\r
98                 }\r
99         }\r
100 \r
101         public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws APIException, CadiException {\r
102                 try {\r
103                         return set(new HX509SS(alias,si));\r
104                 } catch (Exception e) {\r
105                         throw new CadiException("Error creating X509SS",e);\r
106                 }\r
107         }\r
108 \r
109         /* (non-Javadoc)\r
110          * @see com.att.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, com.att.cadi.SecuritySetter)\r
111          */\r
112         @Override\r
113         protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {\r
114                 try {\r
115                         return new HRcli(hman, hman.loc.best() ,ss);\r
116                 } catch (Exception e) {\r
117                         throw new CadiException(e);\r
118                 }\r
119         }\r
120 \r
121         @Override\r
122         public AbsTransferSS<HttpURLConnection> transferSS(Principal principal) throws CadiException {\r
123                 return new HTransferSS(principal, app,si);\r
124         }\r
125         \r
126         /* (non-Javadoc)\r
127          * @see com.att.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)\r
128          */\r
129         @Override\r
130         public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {\r
131                 try {\r
132                         return new HBasicAuthSS(principal,si);\r
133                 } catch (IOException e) {\r
134                         throw new CadiException("Error creating HBasicAuthSS",e);\r
135                 }\r
136         }\r
137 \r
138         public HMangr hman() {\r
139                 return hman;\r
140         }\r
141 \r
142         @Override\r
143         public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
144                 return hman.best(ss, (Retryable<RET>)retryable);\r
145         }\r
146         \r
147 }\r