7340618f064e4dd6a1e767426c67e2b0c5f309de
[aaf/authz.git] / cadi / oauth-enduser / src / test / java / org / onap / aaf / cadi / enduser / test / SimpleRestClientExample.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.enduser.test;
23
24 import java.net.URISyntaxException;
25 import java.security.Principal;
26
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.LocatorException;
29 import org.onap.aaf.cadi.enduser.ClientFactory;
30 import org.onap.aaf.cadi.enduser.SimpleRESTClient;
31 import org.onap.aaf.misc.env.APIException;
32
33
34 public class SimpleRestClientExample {
35         public final static void main(final String args[]) throws URISyntaxException, LocatorException {
36                 try {
37                         // Note: Expect ClientFactory to be long-lived... do NOT create more than once.
38                         ClientFactory cf = new ClientFactory(args);
39                         
40         
41                         String urlString = cf.getAccess().getProperty("myurl", null);
42                         if(urlString==null) {
43                                 System.out.println("Note: In your startup, add \"myurl=https://<aaf hello machine>:8130\" to command line\n\t"
44                                                 + "OR\n\t" 
45                                                 + " add -Dmyurl=https://<aaf hello machine>:8130 to VM Args\n\t"
46                                                 + "where \"aaf hello machine\" is an aaf Installation you know about.");
47                         } else {
48                                 SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf");
49                                 
50                                 // Make some calls
51                                 
52                                 // Call with no Queries
53                                 String rv = restClient.get("resthello");
54                                 System.out.println(rv);
55                                 
56                                 // Call with Queries
57                                 rv = restClient.get("resthello?perm=org.osaaf.people|*|read");
58                                 System.out.println(rv);
59                                 
60                                 // Call setting ID from principal coming from Trans
61                                 // Pretend Transaction
62                                 HRequest req = new HRequest("demo@people.osaaf.org"); // Pretend Trans has Jonathan as Identity
63                                 
64                                 rv = restClient.as(req.userPrincipal()).get("resthello?perm=org.osaaf.people|*|read");
65                                 System.out.println(rv);
66                         }                       
67                 } catch (CadiException | APIException e) {
68                         e.printStackTrace();
69                 }
70         }
71         
72         private static class HRequest { 
73                 
74                 public HRequest(String fqi) {
75                         name = fqi;
76                 }
77                 protected final String name;
78
79         // fake out HttpServletRequest, only for get Principal
80                 public Principal userPrincipal() {
81                         return new Principal() {
82
83                                 @Override
84                                 public String getName() {
85                                         return name;
86                                 }
87                                 
88                         };
89                 }
90         }
91 }