Update DCAE Startup Info
[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) {
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         hman = new HMangr(access,Config.loadLocator(si, access.getProperty(tag,tag/*try the content itself*/)));
64     }
65
66     public AAFConHttp(Access access, String urlTag, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException {
67         super(access,urlTag,si);
68         hman = new HMangr(access,Config.loadLocator(si, access.getProperty(urlTag,null)));
69     }
70
71     public AAFConHttp(Access access, Locator<URI> locator) throws CadiException, LocatorException {
72         super(access,Config.AAF_URL,SecurityInfoC.instance(access, HttpURLConnection.class));
73         hman = new HMangr(access,locator);
74     }
75
76     public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si) throws CadiException, LocatorException, APIException {
77         super(access,Config.AAF_URL,si);
78         hman = new HMangr(access,locator);
79     }
80
81     public AAFConHttp(Access access, Locator<URI> locator, SecurityInfoC<HttpURLConnection> si, String tag) throws CadiException, LocatorException, APIException {
82         super(access,tag,si);
83         hman = new HMangr(access, locator);
84     }
85
86     private AAFConHttp(AAFCon<HttpURLConnection> aafcon, String url) throws LocatorException {
87         super(aafcon);
88         si=aafcon.si;
89         hman = new HMangr(aafcon.access,Config.loadLocator(si, url));
90     }
91
92     @Override
93     public AAFCon<HttpURLConnection> clone(String url) throws LocatorException {
94         return new AAFConHttp(this,url);
95     }
96
97     /* (non-Javadoc)
98      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuth(java.lang.String, java.lang.String)
99      */
100     @Override
101     public SecuritySetter<HttpURLConnection> basicAuth(String user, String password) throws CadiException {
102         if (password.startsWith("enc:")) {
103             try {
104                 password = access.decrypt(password, true);
105             } catch (IOException e) {
106                 throw new CadiException("Error decrypting password",e);
107             }
108         }
109         try {
110             return new HBasicAuthSS(si,user,password);
111         } catch (IOException e) {
112             throw new CadiException("Error creating HBasicAuthSS",e);
113         }
114     }
115
116     public SecuritySetter<HttpURLConnection> x509Alias(String alias) throws CadiException {
117         try {
118             return set(new HX509SS(alias,si));
119         } catch (Exception e) {
120             throw new CadiException("Error creating X509SS",e);
121         }
122     }
123
124     /* (non-Javadoc)
125      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#rclient(java.net.URI, org.onap.aaf.cadi.SecuritySetter)
126      */
127     @Override
128     protected Rcli<HttpURLConnection> rclient(URI ignoredURI, SecuritySetter<HttpURLConnection> ss) throws CadiException {
129         if (hman.loc==null) {
130             throw new CadiException("No Locator set in AAFConHttp");
131         }
132         try {
133             return new HRcli(hman, hman.loc.best() ,ss);
134         } catch (Exception e) {
135             throw new CadiException(e);
136         }
137     }
138
139     @Override
140     public Rcli<HttpURLConnection> rclient(Locator<URI> loc, SecuritySetter<HttpURLConnection> ss) throws CadiException {
141         try {
142             HMangr newHMan = new HMangr(access, loc);
143             return new HRcli(newHMan,newHMan.loc.best(),ss);
144         } catch (Exception e) {
145             throw new CadiException(e);
146         }
147     }
148     @Override
149     public AbsTransferSS<HttpURLConnection> transferSS(TaggedPrincipal principal) {
150         return new HTransferSS(principal, app,si);
151     }
152
153     /* (non-Javadoc)
154      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#basicAuthSS(java.security.Principal)
155      */
156     @Override
157     public SecuritySetter<HttpURLConnection> basicAuthSS(BasicPrincipal principal) throws CadiException {
158         try {
159             return new HBasicAuthSS(principal,si);
160         } catch (IOException e) {
161             throw new CadiException("Error creating HBasicAuthSS",e);
162         }
163     }
164
165     @Override
166     public SecuritySetter<HttpURLConnection> tokenSS(final String client_id, final String accessToken) throws CadiException {
167         try {
168             return new HTokenSS(si, client_id, accessToken);
169         } catch (IOException e) {
170             throw new CadiException(e);
171         }
172     }
173
174     public HMangr hman() {
175         return hman;
176     }
177
178     @Override
179     public <RET> RET best(Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
180         return hman.best(si.defSS, retryable);
181     }
182
183     /* (non-Javadoc)
184      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#bestForUser(org.onap.aaf.cadi.SecuritySetter, org.onap.aaf.cadi.client.Retryable)
185      */
186     @Override
187     public <RET> RET bestForUser(GetSetter getSetter, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {
188         return hman.best(getSetter.get(this), retryable);
189     }
190
191     /* (non-Javadoc)
192      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#initURI()
193      */
194     @Override
195     protected URI initURI() {
196         try {
197             Item item = hman.loc.best();
198             if (item!=null) {
199                 return hman.loc.get(item);
200             }
201         } catch (LocatorException e) {
202             access.log(e, "Error in AAFConHttp obtaining initial URI");
203         }
204         return null;
205     }
206
207     /* (non-Javadoc)
208      * @see org.onap.aaf.cadi.aaf.v2_0.AAFCon#setInitURI(java.lang.String)
209      */
210     @Override
211     protected void setInitURI(String uriString) {
212         // Using Locator, not URLString, which is mostly for DME2
213     }
214
215 }