AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / aaf / test / TestHClient.java
1 /**
2  * ============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.test;
23
24 import java.net.HttpURLConnection;
25 import java.net.URI;
26
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.cadi.SecuritySetter;
30 import org.onap.aaf.cadi.Access.Level;
31 import org.onap.aaf.cadi.Locator.Item;
32 import org.onap.aaf.cadi.aaf.v2_0.AAFLocator;
33 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLocator;
34 import org.onap.aaf.cadi.client.Future;
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.misc.env.APIException;
42 import org.onap.aaf.misc.env.impl.BasicTrans;
43
44 public class TestHClient {
45         public static void main(String[] args) {
46                 try {
47                         PropAccess access = new PropAccess(args);
48                         String aaf_url = access.getProperty(Config.AAF_URL);
49                         if(aaf_url == null) {
50                                 access.log(Level.ERROR, Config.AAF_URL," is required");
51                         } else {
52                                 HMangr hman = null;
53                                 try {
54                                         SecurityInfoC<HttpURLConnection> si = SecurityInfoC.instance(access, HttpURLConnection.class);
55                                         AbsAAFLocator<BasicTrans> loc = new AAFLocator(si,new URI(aaf_url));
56                                         for(Item item = loc.first(); item!=null; item=loc.next(item)) {
57                                                 System.out.println(loc.get(item));
58                                         }
59                                         SecuritySetter<HttpURLConnection> ss = new HBasicAuthSS(si);
60                 //                      SecuritySetter<HttpURLConnection> ss = new X509SS(si, "aaf");
61                                         
62                                         hman = new HMangr(access,loc);
63                                         final String path = String.format("/authz/perms/user/%s",
64                                                         access.getProperty(Config.AAF_APPID,"xx9999@csp.att.com"));
65                                         hman.best(ss, new Retryable<Void>() {
66                                                 @Override
67                                                 public Void code(Rcli<?> cli) throws APIException, CadiException {
68                                                         Future<String> ft = cli.read(path,"application/json");  
69                                                         if(ft.get(10000)) {
70                                                                 System.out.println("Hurray,\n"+ft.body());
71                                                         } else {
72                                                                 System.out.println("not quite: " + ft.code());
73                                                         }
74                                                         return null;
75                                                 }});
76                                 } finally {
77                                         if(hman!=null) {
78                                                 hman.close();
79                                         }
80                                 }
81                         }
82                 } catch (Exception e) {
83                         e.printStackTrace();
84                 }
85         }
86         
87 }