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.net.URISyntaxException;
25 import java.security.Principal;
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;
35 public class SimpleRestClientExample {
36 public final static void main(final String args[]) throws URISyntaxException, LocatorException {
38 // Note: Expect ClientFactory to be long-lived... do NOT create more than once.
39 ClientFactory cf = new ClientFactory(args);
42 String urlString = cf.getAccess().getProperty("myurl", null);
44 System.out.println("Note: In your startup, add \"myurl=https://<aaf hello machine>:8130\" to command line\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.");
49 SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf");
53 // Call with no Queries
54 String rv = restClient.get("resthello");
55 System.out.println(rv);
57 // Same call with "read" style
58 rv = restClient.read("resthello");
59 System.out.println(rv);
63 rv = restClient.get("resthello?perm=org.osaaf.people|*|read");
64 System.out.println(rv);
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
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);
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);
85 } catch (CadiException | APIException | RESTException e) {
90 private static class HRequest {
92 public HRequest(String fqi) {
95 protected final String name;
97 // fake out HttpServletRequest, only for get Principal
98 public Principal userPrincipal() {
99 return new Principal() {
102 public String getName() {