d39fc1d670f9fd61bc22d66c4061f5768c0dfc26
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / aaf / v2_0 / AAFConHttp.java
1 /**
2 r * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.aaf.v2_0;
23
24 import java.io.IOException;
25 import java.net.HttpURLConnection;
26 import java.net.URI;
27
28 import org.onap.aaf.cadi.Access;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.Locator;
31 import org.onap.aaf.cadi.Locator.Item;
32 import org.onap.aaf.cadi.LocatorException;
33 import org.onap.aaf.cadi.SecuritySetter;
34 import org.onap.aaf.cadi.client.AbsTransferSS;
35 import org.onap.aaf.cadi.client.Rcli;
36 import org.onap.aaf.cadi.client.Retryable;
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.config.SecurityInfoC;
39 import org.onap.aaf.cadi.http.HBasicAuthSS;
40 import org.onap.aaf.cadi.http.HMangr;
41 import org.onap.aaf.cadi.http.HRcli;
42 import org.onap.aaf.cadi.http.HTokenSS;
43 import org.onap.aaf.cadi.http.HTransferSS;
44 import org.onap.aaf.cadi.http.HX509SS;
45 import org.onap.aaf.cadi.principal.BasicPrincipal;
46 import org.onap.aaf.cadi.principal.TaggedPrincipal;
47 import org.onap.aaf.misc.env.APIException;
48
49 public class AAFConHttp extends AAFCon<HttpURLConnection> {
50     private final HMangr hman;
51
52     public AAFConHttp(Access access) throws CadiException, LocatorException {
53         super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
54         hman = new HMangr(access,Config.loadLocator(si, access.getProperty(Config.AAF_URL,null)));
55     }
56
57     protected SecuritySetter<HttpURLConnection> bestSS(SecurityInfoC<HttpURLConnection> si) throws CadiException {
58         return si.defSS;
59     }
60
61     public AAFConHttp(Access access, String tag) throws CadiException, LocatorException {
62         super(access,tag,SecurityInfoC.instance(access, HttpURLConnection.class));
63         bestSS(si);
64         hman = new HMangr(access,Config.loadLocator(si, access.getProperty(tag,tag/*try the content itself*/)));
65     }
66
67     public AAFConHttp(Access access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException {
68         super(access,urlTag,si);
69         bestSS(si);
70         hman = new HMangr(access,Config.loadLocator(si, access.getProperty(urlTag,null)));
71     }
72
73     public AAFConHttp(Access access, Locator<URI> locator) throws CadiException, LocatorException {
74         super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
75         bestSS(si);
76         hman = new HMangr(access,locator);
77     }
78
79     public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException, APIException {
80         super(access,Config.AAF_URL,si);
81         bestSS(si);
82         hman = new HMangr(access,locator);
83     }
84
85     public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException, LocatorException, APIException {
86         super(access,tag,si);
87         bestSS(si);
88         hman = new HMangr(access, locator);
89     }
90     
91     private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) throws LocatorException {
92         super(aafcon);
93         si=aafcon.si;
94         hman = new HMangr(aafcon.access,Config.loadLocator(si, url));
95     }
96
97     @Override
98     public AAFCon<HttpURLConnection> clone(String url) throws LocatorException {
99         return new AAFConHttp(this,url);
100     }
101
102     /* (non-Javadoc)
103      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)
104      */
105     @Override
106     public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {
107         if (password.startsWith("enc:")) {
108             try {
109                 password = access.decrypt(password, true);
110             } catch (IOException e) {
111                 throw new CadiException("Error decrypting password",e);
112             }
113         }
114         try {
115             return new HBasicAuthSS(si,user,password);
116         } catch (IOException e) {
117             throw new CadiException("Error creating HBasicAuthSS",e);
118         }
119     }
120
121     public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws CadiException {
122         try {
123             return set(new HX509SS(alias,si));
124         } catch (Exception e) {
125             throw new CadiException("Error creating X509SS",e);
126         }
127     }
128
129     /* (non-Javadoc)
130      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, org.onap.aaf.cadi.SecuritySetter)
131      */
132     @Override
133     protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {
134         if (hman.loc==null) {
135             throw new CadiException("No Locator set in AAFConHttp"); 
136         }
137         try {
138             return new HRcli(hman, hman.loc.best() ,ss);
139         } catch (Exception e) {
140             throw new CadiException(e);
141         }
142     }
143     
144     @Override
145     public Rcli<HttpURLConnection> rclient(Locator<URI> loc, SecuritySetter<HttpURLConnection> ss) throws CadiException {
146         try {
147             HMangr newHMan = new HMangr(access, loc);
148             return new HRcli(newHMan,newHMan.loc.best(),ss);
149         } catch (Exception e) {
150             throw new CadiException(e);
151         }
152     }
153     @Override
154     public AbsTransferSS<HttpURLConnection> transferSS(TaggedPrincipal principal) {
155         return new HTransferSS(principal, app,si);
156     }
157     
158     /* (non-Javadoc)
159      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)
160      */
161     @Override
162     public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {
163         try {
164             return new HBasicAuthSS(principal,si);
165         } catch (IOException e) {
166             throw new CadiException("Error creating HBasicAuthSS",e);
167         }
168     }
169
170     @Override
171     public SecuritySetter<HttpURLConnection> tokenSS(final String client_id, final String accessToken) throws CadiException {
172         try {
173             return new HTokenSS(si, client_id, accessToken);
174         } catch (IOException e) {
175             throw new CadiException(e);
176         }
177     }
178
179     public HMangr hman() {
180         return hman;
181     }
182
183     @Override
184     public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
185         return hman.best(si.defSS, retryable);
186     }
187
188     /* (non-Javadoc)
189      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#bestForUser(org.onap.aaf.cadi.SecuritySetter, org.onap.aaf.cadi.client.Retryable)
190      */
191     @Override
192     public <RET> RET bestForUser(GetSetter getSetter, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
193         return hman.best(getSetter.get(this), retryable);
194     }
195
196     /* (non-Javadoc)
197      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#initURI()
198      */
199     @Override
200     protected URI initURI() {
201         try {
202             Item item = hman.loc.best();
203             if (item!=null) {
204                 return hman.loc.get(item);
205             }
206         } catch (LocatorException e) {
207             access.log(e, "Error in AAFConHttp obtaining initial URI");
208         }
209         return null;
210     }
211
212     /* (non-Javadoc)
213      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)
214      */
215     @Override
216     protected void setInitURI(String uriString) {
217         // Using Locator, not URLString, which is mostly for DME2
218     }
219
220 }