2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.enduser.test;
24 import java.io.PrintWriter;
25 import java.net.URISyntaxException;
26 import java.security.Principal;
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;
37 public class SimpleRestClientExample {
38 public final static void main(final String args[]) throws URISyntaxException, LocatorException {
40 // Note: Expect ClientFactory to be long-lived... do NOT create more than once.
41 ClientFactory cf = new ClientFactory(args);
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"
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.");
52 SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf");
54 /////////////////////////////////////////////////////////////
56 // Creating Content for CREATE/UPDATE
58 /////////////////////////////////////////////////////////////
59 // Create an object that can be reusable IN THIS THREAD ONLY... Not Thread-safe on purpose
60 Input input = new SimpleRESTClient.Input();
62 // Note: alternate use is to set the input object to an already created String
63 // Input input = new SimpleRESTClient.Input(aString);
65 PrintWriter pw = input.writer();
66 pw.print("{\"something\": [");
67 for (int i=0;i<4;++i) {
71 pw.print("{\"myint\":");
77 // You can check or log the content
78 String content = input.toString();
79 System.out.println(content);
81 // Good form for Writers is that you should close it...
84 /////////////////////////////////////////////////////////////
88 /////////////////////////////////////////////////////////////
89 System.out.println("-------- START REST CREATE/UPDATE --------");
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());
98 System.out.println("-------- END REST CREATE/UPDATE --------");
102 /////////////////////////////////////////////////////////////
106 /////////////////////////////////////////////////////////////
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;
114 // Call with no Queries
115 String rv = restClient.get("resthello/rest_id");
116 System.out.println(rv);
118 // Same call with "read" style
119 rv = restClient.read("resthello/rest_id");
120 System.out.println(rv);
124 rv = restClient.get("resthello/rest_id?perm=org.osaaf.people|*|read");
125 System.out.println(rv);
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
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);
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);
146 if (expectException) {
147 System.out.println("-------- END Expecting Exception starting here --------");
149 System.out.println("-------- END REST READ/GET --------");
152 /////////////////////////////////////////////////////////////
156 /////////////////////////////////////////////////////////////
159 // If you use "input" object again as a writer, you can clear it on the same thread, and go again
161 // Here we just set to a String, instead of Writing
162 input.set("{\"something\" : []}");
164 System.out.println("-------- END REST UPDATE/PUT --------");
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());
174 System.out.println("-------- END REST UPDATE/PUT --------");
177 /////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////
183 System.out.println("-------- START REST DELETE --------");
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());
192 System.out.println("-------- END REST DELETE --------");
195 } catch (CadiException | APIException e) {
200 private static class HRequest {
202 public HRequest(String fqi) {
205 protected final String name;
207 // fake out HttpServletRequest, only for get Principal
208 public Principal userPrincipal() {
209 return new Principal() {
212 public String getName() {