dc1824fcf9892ad7f9bd724ae3182aeb9e89551b
[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.io.PrintWriter;
25 import java.net.URISyntaxException;
26 import java.security.Principal;
27
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.enduser.ClientFactory;
31 import org.onap.aaf.cadi.enduser.RESTException;
32 import org.onap.aaf.cadi.enduser.SimpleRESTClient;
33 import org.onap.aaf.cadi.enduser.SimpleRESTClient.Input;
34 import org.onap.aaf.misc.env.APIException;
35
36
37 public class SimpleRestClientExample {
38     public final static void main(final String args[]) throws URISyntaxException, LocatorException {
39         try {
40             // Note: Expect ClientFactory to be long-lived... do NOT create more than once.
41             ClientFactory cf = new ClientFactory(args);
42             
43     
44             String urlString = cf.getAccess().getProperty("myurl", null);
45             if (urlString==null) {
46                 System.out.println("Note: In your startup, add \"myurl=https://<aaf hello machine>:8130\" to command line\n\t"
47                         + "OR\n\t" 
48                         + " add -Dmyurl=https://<aaf hello machine>:8130 to VM Args\n\t"
49                         + "where \"aaf hello machine\" is an aaf Installation you know about.");
50             } else {
51                 
52                 SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf");
53
54                 /////////////////////////////////////////////////////////////
55                 //  
56                 // Creating Content for CREATE/UPDATE
57                 //
58                 /////////////////////////////////////////////////////////////
59                 // Create an object that can be reusable IN THIS THREAD ONLY... Not Thread-safe on purpose
60                 Input input = new SimpleRESTClient.Input();
61                 
62                 // Note: alternate use is to set the input object to an already created String
63                 // Input input = new SimpleRESTClient.Input(aString);
64                 
65                 PrintWriter pw = input.writer();
66                 pw.print("{\"something\": [");
67                 for (int i=0;i<4;++i) {
68                     if (i>0) {
69                         pw.print(',');
70                     }
71                     pw.print("{\"myint\":");
72                     pw.print(i);
73                     pw.print('}');
74                 }
75                 pw.println("]}");
76                 
77                 // You can check or log the content
78                 String content = input.toString();
79                 System.out.println(content);
80                 
81                 // Good form for Writers is that you should close it... 
82                 pw.close();
83
84                 /////////////////////////////////////////////////////////////
85                 //  
86                 // CREATE/POST
87                 //
88                 /////////////////////////////////////////////////////////////
89                 System.out.println("-------- START REST CREATE/UPDATE --------");
90                 try {
91                     restClient.create("resthello/rest_id", input);
92                     // No Error code, it worked.
93                     System.out.println("No Error Code, Create worked...");
94                 } catch (RESTException e) {
95                     System.out.println(e.getCode());
96                     System.out.println(e.getMsg());
97                 } finally {
98                     System.out.println("-------- END REST CREATE/UPDATE --------");
99                 }
100
101
102                 /////////////////////////////////////////////////////////////
103                 //  
104                 // READ/GET
105                 //
106                 /////////////////////////////////////////////////////////////
107
108                 // Make some calls.  Note that RESTException is thrown if Call does not complete.
109                 // RESTException has HTTP Code and any Message sent from Server
110                 System.out.println("-------- START REST READ/GET --------");
111                 boolean expectException = false;
112                 try {
113                     
114                     // Call with no Queries
115                     String rv = restClient.get("resthello/rest_id");
116                     System.out.println(rv);
117                     
118                     // Same call with "read" style
119                     rv = restClient.read("resthello/rest_id");
120                     System.out.println(rv);
121                     
122                     
123                     // Call with Queries
124                     rv = restClient.get("resthello/rest_id?perm=org.osaaf.people|*|read");
125                     System.out.println(rv);
126                     
127                     // Call setting ID from principal coming from Trans
128                     // Pretend Transaction
129                     HRequest req = new HRequest("demo@people.osaaf.org"); // Pretend Trans has Jonathan as Identity
130                     
131                     // Call with RESTException, which allows obtaining HTTPCode and any Error message sent
132                     rv = restClient.endUser(req.userPrincipal()).get("resthello/rest_id?perm=org.osaaf.people|*|read");
133                     System.out.println(rv);
134
135                     // Expect Exception here.
136                     System.out.println("-------- START Expecting Exception starting here --------");
137                     expectException = true;
138                     restClient.get("notAnAPI");
139                 } catch (RESTException e) {
140                     System.out.println(e.getCode());
141                     System.out.println(e.getMsg());
142                     System.out.println(e.getMessage());
143                     System.out.println(e.getLocalizedMessage());
144                     System.out.println(e);
145                 } finally {
146                     if (expectException) {
147                         System.out.println("-------- END Expecting Exception starting here --------");
148                     }
149                     System.out.println("-------- END REST READ/GET --------");
150                 }
151
152                 /////////////////////////////////////////////////////////////
153                 //  
154                 // UPDATE/PUT
155                 //
156                 /////////////////////////////////////////////////////////////
157
158                 
159                 // If you use "input" object again as a writer, you can clear it on the same thread, and go again
160                 input.clear();
161                 // Here we just set to a String, instead of Writing
162                 input.set("{\"something\" : []}");
163                 
164                 System.out.println("-------- END REST UPDATE/PUT --------");
165                 try {
166                     String rv = restClient.update("resthello/rest_id", input);
167                     // No Error code, it worked.  REST Update will return the updated Data
168                     System.out.println("Update worked");
169                     System.out.println(rv);
170                 } catch (RESTException e) {
171                     System.out.println(e.getCode());
172                     System.out.println(e.getMsg());
173                 } finally {
174                     System.out.println("-------- END REST UPDATE/PUT --------");
175                 }
176
177                 /////////////////////////////////////////////////////////////
178                 //  
179                 // DELETE
180                 //
181                 /////////////////////////////////////////////////////////////
182
183                 System.out.println("-------- START REST DELETE --------");
184                 try {
185                     restClient.delete("resthello/rest_id");
186                     // No Error code, it worked.  REST Update will return the updated Data
187                     System.out.println("Delete worked");
188                 } catch (RESTException e) {
189                     System.out.println(e.getCode());
190                     System.out.println(e.getMsg());
191                 } finally {
192                     System.out.println("-------- END REST DELETE --------");
193                 }
194             }            
195         } catch (CadiException | APIException e) {
196             e.printStackTrace();
197         }
198     }
199     
200     private static class HRequest { 
201         
202         public HRequest(String fqi) {
203             name = fqi;
204         }
205         protected final String name;
206
207     // fake out HttpServletRequest, only for get Principal
208         public Principal userPrincipal() {
209             return new Principal() {
210
211                 @Override
212                 public String getName() {
213                     return name;
214                 }
215                 
216             };
217         }
218     }
219 }