- // 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<TimedToken> rtt = tc.getToken(ns,"org.onap.test");
-
- // 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.
- TzClient helloClient = tcf.newTzClient(endServicesURL);
- helloClient.setToken(client_id, token);
-
- // 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
- // Note that variables used in the inner class from this part of the code must be "final", see "CALL_TIMEOUT"
- String rv = helloClient.best(new Retryable<String>() {
- @Override
- public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
- Future<String> 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(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
- //////////////////////////////////////////////////////////////////////
- // CADI Framework (i.e. CadiFilter) works with the Introspection to drive the J2EE interfaces (
- // i.e. if(isUserInRole("ns.perm|instance|action")) {...
- //
- // Here, however, is a way to introspect via Java
- //
- // now, call Introspect (making sure right URLs are set in properties)
- // We need a Different Introspect TokenClient, because different Endpoint (and usually different Services)
- TokenClient tci = tcf.newClient(tokenIntrospectURL);
- tci.client_creds(client_id, client_secret);
- Result<Introspect> is = tci.introspect(token.getAccessToken());
- if(is.isOK()) {
- // Note that AAF will add JSON set of Permissions as part of "Content:", legitimate extension of OAuth Structure
- print(is.value); // do something with Introspect Object
- } else {
- access.printf(Level.ERROR, "Unable to introspect OAuth Token %s: %d %s\n",
- token.getAccessToken(),rtt.code,rtt.error);
- }
- } 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
- /////////////////////////////////////////////////////////////
- private static void print(Token t) {
- GregorianCalendar exp_date = new GregorianCalendar();
- exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
- System.out.printf("Access Token\n\tToken:\t\t%s\n\tToken Type:\t%s\n\tExpires In:\t%d (%s)\n\tScope:\t\t%s\n\tRefresh Token:\t%s\n",
- t.getAccessToken(),
- t.getTokenType(),
- t.getExpiresIn(),
- Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
- t.getScope(),
- t.getRefreshToken());
- }
-
- private static void print(Introspect ti) {
- if(ti==null || ti.getClientId()==null) {
- System.out.println("Empty Introspect");
- return;
- }
- Date exp = new Date(ti.getExp()*1000); // seconds
- System.out.printf("Introspect\n"
- + "\tAccessToken:\t%s\n"
- + "\tClient-id:\t%s\n"
- + "\tClient Type:\t%s\n"
- + "\tActive: \t%s\n"
- + "\tUserName:\t%s\n"
- + "\tExpires: \t%d (%s)\n"
- + "\tScope:\t\t%s\n"
- + "\tContent:\t%s\n",
- ti.getAccessToken(),
- ti.getClientId(),
- ti.getClientType(),
- ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
- ti.getUsername(),
- ti.getExp(),
- Chrono.timeStamp(exp),
- ti.getScope(),
- ti.getContent()==null?"":ti.getContent());
-
- System.out.println();
- }
+ // 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<TimedToken> rtt = tc.getToken(ns,"org.onap.test");
+
+ // 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.
+ TzClient helloClient = tcf.newTzClient(endServicesURL);
+ helloClient.setToken(client_id, token);
+
+ // 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
+ // Note that variables used in the inner class from this part of the code must be "final", see "CALL_TIMEOUT"
+ String rv = helloClient.best(new Retryable<String>() {
+ @Override
+ public String code(Rcli<?> client) throws CadiException, ConnectException, APIException {
+ Future<String> 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 (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
+ //////////////////////////////////////////////////////////////////////
+ // CADI Framework (i.e. CadiFilter) works with the Introspection to drive the J2EE interfaces (
+ // i.e. if (isUserInRole("ns.perm|instance|action")) {...
+ //
+ // Here, however, is a way to introspect via Java
+ //
+ // now, call Introspect (making sure right URLs are set in properties)
+ // We need a Different Introspect TokenClient, because different Endpoint (and usually different Services)
+ TokenClient tci = tcf.newClient(tokenIntrospectURL);
+ tci.client_creds(client_id, client_secret);
+ Result<Introspect> is = tci.introspect(token.getAccessToken());
+ if (is.isOK()) {
+ // Note that AAF will add JSON set of Permissions as part of "Content:", legitimate extension of OAuth Structure
+ print(is.value); // do something with Introspect Object
+ } else {
+ access.printf(Level.ERROR, "Unable to introspect OAuth Token %s: %d %s\n",
+ token.getAccessToken(),rtt.code,rtt.error);
+ }
+ } 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
+ /////////////////////////////////////////////////////////////
+ private static void print(Token t) {
+ GregorianCalendar exp_date = new GregorianCalendar();
+ exp_date.add(GregorianCalendar.SECOND, t.getExpiresIn());
+ System.out.printf("Access Token\n\tToken:\t\t%s\n\tToken Type:\t%s\n\tExpires In:\t%d (%s)\n\tScope:\t\t%s\n\tRefresh Token:\t%s\n",
+ t.getAccessToken(),
+ t.getTokenType(),
+ t.getExpiresIn(),
+ Chrono.timeStamp(new Date(System.currentTimeMillis()+(t.getExpiresIn()*1000))),
+ t.getScope(),
+ t.getRefreshToken());
+ }
+
+ private static void print(Introspect ti) {
+ if (ti==null || ti.getClientId()==null) {
+ System.out.println("Empty Introspect");
+ return;
+ }
+ Date exp = new Date(ti.getExp()*1000); // seconds
+ System.out.printf("Introspect\n"
+ + "\tAccessToken:\t%s\n"
+ + "\tClient-id:\t%s\n"
+ + "\tClient Type:\t%s\n"
+ + "\tActive: \t%s\n"
+ + "\tUserName:\t%s\n"
+ + "\tExpires: \t%d (%s)\n"
+ + "\tScope:\t\t%s\n"
+ + "\tContent:\t%s\n",
+ ti.getAccessToken(),
+ ti.getClientId(),
+ ti.getClientType(),
+ ti.isActive()?Boolean.TRUE.toString():Boolean.FALSE.toString(),
+ ti.getUsername(),
+ ti.getExp(),
+ Chrono.timeStamp(exp),
+ ti.getScope(),
+ ti.getContent()==null?"":ti.getContent());
+
+ System.out.println();
+ }