7d251badb9d857bbfac103d06dbf2816af377135
[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.RESTException;
31 import org.onap.aaf.cadi.enduser.SimpleRESTClient;
32 import org.onap.aaf.misc.env.APIException;
33
34
35 public class SimpleRestClientExample {
36         public final static void main(final String args[]) throws URISyntaxException, LocatorException {
37                 try {
38                         // Note: Expect ClientFactory to be long-lived... do NOT create more than once.
39                         ClientFactory cf = new ClientFactory(args);
40                         
41         
42                         String urlString = cf.getAccess().getProperty("myurl", null);
43                         if(urlString==null) {
44                                 System.out.println("Note: In your startup, add \"myurl=https://<aaf hello machine>:8130\" to command line\n\t"
45                                                 + "OR\n\t" 
46                                                 + " add -Dmyurl=https://<aaf hello machine>:8130 to VM Args\n\t"
47                                                 + "where \"aaf hello machine\" is an aaf Installation you know about.");
48                         } else {
49                                 SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf");
50                                 
51                                 // Make some calls
52                                 
53                                 // Call with no Queries
54                                 String rv = restClient.get("resthello");
55                                 System.out.println(rv);
56                                 
57                                 // Same call with "read" style
58                                 rv = restClient.read("resthello");
59                                 System.out.println(rv);
60                                 
61                                 
62                                 // Call with Queries
63                                 rv = restClient.get("resthello?perm=org.osaaf.people|*|read");
64                                 System.out.println(rv);
65                                 
66                                 // Call setting ID from principal coming from Trans
67                                 // Pretend Transaction
68                                 HRequest req = new HRequest("demo@people.osaaf.org"); // Pretend Trans has Jonathan as Identity
69                                 
70                                 // Call with RESTException, which allows obtaining HTTPCode and any Error message sent
71                                 rv = restClient.endUser(req.userPrincipal()).get("resthello?perm=org.osaaf.people|*|read");
72                                 System.out.println(rv);
73
74                                 try {
75                                         restClient.get("notAnAPI");
76                                 } catch(RESTException e) {
77                                         System.out.println(e.getCode());
78                                         System.out.println(e.getMsg());
79                                         System.out.println(e.getMessage());
80                                         System.out.println(e.getLocalizedMessage());
81                                         System.out.println(e);
82                                 }
83
84                         }                       
85                 } catch (CadiException | APIException | RESTException e) {
86                         e.printStackTrace();
87                 }
88         }
89         
90         private static class HRequest { 
91                 
92                 public HRequest(String fqi) {
93                         name = fqi;
94                 }
95                 protected final String name;
96
97         // fake out HttpServletRequest, only for get Principal
98                 public Principal userPrincipal() {
99                         return new Principal() {
100
101                                 @Override
102                                 public String getName() {
103                                         return name;
104                                 }
105                                 
106                         };
107                 }
108         }
109 }