X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Foauth-enduser%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fenduser%2Ftest%2FOnapClientExample.java;fp=cadi%2Foauth-enduser%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fenduser%2Ftest%2FOnapClientExample.java;h=2d489f83bafb95ab7cccfa8a67e3860218355a09;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=b8cce9e5ecffd0a87273c76899588cbb19259aa4;hpb=bdce7667a6e272e2fa32e298d957a0d9090c5bc9;p=aaf%2Fauthz.git diff --git a/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java index b8cce9e5..2d489f83 100644 --- a/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java +++ b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -56,25 +56,25 @@ public class OnapClientExample { public final static void main(final String args[]) { // These Objects are expected to be Long-Lived... Construct once - + // Property Access - // This method will allow you to set "cadi_prop_files" (or any other property) on Command line + // This method will allow you to set "cadi_prop_files" (or any other property) on Command line access = new PropAccess(args); // Note: This style will load "cadi_prop_files" from VM Args // access = PropAccess(); try { Map aaf_urls = Agent.loadURLs(access); Agent.fillMissing(access, aaf_urls); - + // Token aware Client Factory tcf = TokenClientFactory.instance(access); } catch (APIException | GeneralSecurityException | IOException | CadiException e1) { access.log(e1, "Unable to setup OAuth Client Factory, Fail Fast"); System.exit(1); } - + final int CALL_TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF)); - + try { ////////////////////////////////////////////////////////////////////// // Scenario 1: @@ -84,7 +84,7 @@ public class OnapClientExample { // In this example, it is AAF, but it can be the Alternate OAuth TokenClient tc = tcf.newClient(Config.AAF_OAUTH2_TOKEN_URL); // can set your own timeout here (url, timeoutMilliseconds) - + // Here's a trick to get the namespace out of a Fully Qualified AAF Identity (your MechID) String ns = FQI.reverseDomain(tc.client_id()); System.out.printf("\nNote: The AAF Namespace of FQI (Fully Qualified Identity) %s is %s\n\n",tc.client_id(), ns); @@ -92,17 +92,17 @@ public class OnapClientExample { // Now, we can get a Token. Note: for "scope", use AAF Namespaces to get AAF Permissions embedded in // Note: getToken checks if Token is expired, if so, then refreshes before handing back. Result rtt = tc.getToken(ns,"org.onap.test"); // get multiple scopes - + // Note: you can clear a Token's Disk/Memory presence by // 1) removing the Token from the "token/outgoing" directory on the O/S // 2) programmatically by calling "clearToken" with exact params as "getToken", when it has the same credentials set // tc.clearToken("org.onap.aaf","org.onap.test"); - + // Result Object can be queried for success if (rtt.isOK()) { TimedToken token = rtt.value; print(token); // Take a look at what's in a Token - + // Use this Token in your client calls with "Tokenized Client" (TzClient) // These should NOT be used cross thread. // Get Hello Service URL... roll your own in your own world. @@ -111,8 +111,8 @@ public class OnapClientExample { TzClient helloClient = tcf.newTzClient(endServicesURL); helloClient.setToken(tc.client_id(), token); - - // This client call style, "best" call with "Retryable" inner class covers finding an available Service + + // This client call style, "best" call with "Retryable" inner class covers finding an available Service // (when Multi-services exist) for the best service, based (currently) on distance. // // the "Generic" in Type gives a Return Value for the Code, which you can set on the "best" method @@ -123,20 +123,20 @@ public class OnapClientExample { Future future = client.read("hello","text/plain"); // The "future" calling method allows you to do other processing, such as call more than one backend // client before picking up the result - // If "get" matches the HTTP Code for the method (i.e. read HTTP Return value is 200), then + // If "get" matches the HTTP Code for the method (i.e. read HTTP Return value is 200), then if (future.get(CALL_TIMEOUT)) { // Client Returned expected value return future.value; } else { throw new APIException(future.code() + future.body()); - } + } } }); - + // You want to do something with returned value. Here, we say "hello" System.out.printf("\nPositive Response from Hello: %s\n",rv); - - + + ////////////////////////////////////////////////////////////////////// // Scenario 2: // As a Service, read Introspection information as proof of Authenticated Authorization @@ -160,12 +160,12 @@ public class OnapClientExample { } else { access.printf(Level.ERROR, "Unable to obtain OAuth Token: %d %s\n",rtt.code,rtt.error); } - + } catch (CadiException | LocatorException | APIException | IOException e) { e.printStackTrace(); } } - + ///////////////////////////////////////////////////////////// // Examples of Object Access ///////////////////////////////////////////////////////////// @@ -180,7 +180,7 @@ public class OnapClientExample { t.getScope(), t.getRefreshToken()); } - + private static void print(Introspect ti) { if (ti==null || ti.getClientId()==null) { System.out.println("Empty Introspect"); @@ -205,7 +205,7 @@ public class OnapClientExample { Chrono.timeStamp(exp), ti.getScope(), ti.getContent()==null?"":ti.getContent()); - + System.out.println(); }