diff options
author | Instrumental <jonathan.gathman@att.com> | 2018-06-21 15:08:47 -0500 |
---|---|---|
committer | Instrumental <jonathan.gathman@att.com> | 2018-06-21 15:08:54 -0500 |
commit | 19afb93b23f264471c6e8db77b54e9a301a3114b (patch) | |
tree | d791495495ce243ab16333623671166c8c813cee /cadi/oauth-enduser | |
parent | a7024ad2f0df65a2005e29d24a5974304d2b58c6 (diff) |
Update REST Samples for required behavior
Issue-ID: AAF-361
Change-Id: Ia6dabfa3eeec2253c3f017ddc2d70afe4afbd54b
Signed-off-by: Instrumental <jonathan.gathman@att.com>
Diffstat (limited to 'cadi/oauth-enduser')
-rw-r--r-- | cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/ClientFactory.java | 56 | ||||
-rw-r--r-- | cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/SimpleRESTClient.java | 133 | ||||
-rw-r--r-- | cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OAuthExample.java (renamed from cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OAuthExample.java) | 2 | ||||
-rw-r--r-- | cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java (renamed from cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OnapClientExample.java) | 2 | ||||
-rw-r--r-- | cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/SimpleRestClientExample.java | 91 |
5 files changed, 282 insertions, 2 deletions
diff --git a/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/ClientFactory.java b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/ClientFactory.java new file mode 100644 index 00000000..50eaa759 --- /dev/null +++ b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/ClientFactory.java @@ -0,0 +1,56 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.cadi.enduser; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.security.GeneralSecurityException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.oauth.TokenClientFactory; +import org.onap.aaf.misc.env.APIException; + +public class ClientFactory { + private final TokenClientFactory tcf; + public ClientFactory(final PropAccess access) throws APIException, CadiException { + try { + tcf = TokenClientFactory.instance(access); + } catch (GeneralSecurityException | IOException e) { + throw new CadiException(e); + } + } + + public ClientFactory(String[] args) throws APIException, CadiException { + this(new PropAccess(args)); + } + + public SimpleRESTClient simpleRESTClient(final String endpoint, final String ... scopes) throws URISyntaxException, LocatorException, CadiException, APIException { + return new SimpleRESTClient(tcf, Config.AAF_OAUTH2_TOKEN_URL, endpoint, scopes); + } + + public Access getAccess() { + return tcf.access; + } +} diff --git a/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/SimpleRESTClient.java b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/SimpleRESTClient.java new file mode 100644 index 00000000..9535ad64 --- /dev/null +++ b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/SimpleRESTClient.java @@ -0,0 +1,133 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.cadi.enduser; + +import java.io.IOException; +import java.net.ConnectException; +import java.security.Principal; + +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.client.Future; +import org.onap.aaf.cadi.client.Rcli; +import org.onap.aaf.cadi.client.Result; +import org.onap.aaf.cadi.client.Retryable; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.oauth.TimedToken; +import org.onap.aaf.cadi.oauth.TokenClient; +import org.onap.aaf.cadi.oauth.TokenClientFactory; +import org.onap.aaf.cadi.oauth.TzClient; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.misc.env.APIException; + +public class SimpleRESTClient { + private static final String[] EMPTY = new String[0]; + private final TokenClient tokenClient; + private final TzClient restClient; + private int callTimeout; + private String client_id; + private String app; + private String chain; + private Headers headers = new Headers() { + @Override + public String[] headers() { + return EMPTY; + }}; + + public SimpleRESTClient(final TokenClientFactory tcf, final String tokenURL, final String endpoint, final String[] scope) throws CadiException, LocatorException, APIException { + callTimeout = Integer.parseInt(tcf.access.getProperty(Config.AAF_CALL_TIMEOUT,Config.AAF_CALL_TIMEOUT_DEF)); + tokenClient = tcf.newClient(tokenURL); + Result<TimedToken> rtt = tokenClient.getToken(scope); + if(rtt.isOK()) { + restClient = tcf.newTzClient(endpoint); + + if((client_id = tcf.access.getProperty(Config.AAF_APPID, null))==null) { + if((client_id = tcf.access.getProperty(Config.CADI_ALIAS, null))==null) { + throw new CadiException(Config.AAF_APPID + " or " + Config.CADI_ALIAS + " needs to be defined"); + } + } + try { + restClient.setToken(client_id,rtt.value); + } catch (IOException e) { + throw new CadiException(e); + } + } else { + throw new CadiException(rtt.error); + } + } + + public SimpleRESTClient timeout(int newTimeout) { + callTimeout = newTimeout; + return this; + } + + //Format:<ID>:<APP>:<protocol>[:AS][,<ID>:<APP>:<protocol>]* + public SimpleRESTClient as(Principal principal) { + if(principal==null) { + chain = null; + } else { + if(principal instanceof TaggedPrincipal) { + TaggedPrincipal tp = (TaggedPrincipal)principal; + chain = tp.getName() + ':' + (app==null?"":app) + ':' + tp.tag() + ":AS"; + } else { + chain = principal.getName() + (app==null?"":':'+app); + } + } + return this; + } + + public String get(final String path) throws CadiException, LocatorException, APIException { + return get(path,"application/json"); + } + + public String get(final String path, final String accepts) throws CadiException, LocatorException, APIException { + return restClient.best(new Retryable<String>() { + @Override + public String code(Rcli<?> client) throws CadiException, ConnectException, APIException { + Future<String> future = client.read(path,accepts, headers()); + if(future.get(callTimeout)) { + return future.value; + } else { + throw new APIException(future.code() + future.body()); + } + } + }); + } + + public interface Headers { + String[] headers(); + } + + public String[] headers() { + if(chain==null) { + return headers.headers(); + } else { + String[] strs = headers.headers(); + String[] rv = new String[strs.length+2]; + rv[0]=Config.CADI_USER_CHAIN; + rv[1]=chain; + for(int i = 0;i<strs.length;++i) { + rv[i+2]=strs[i]; + } + return rv; + } + } +} diff --git a/cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OAuthExample.java b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OAuthExample.java index c79c2fe6..39e7b5b6 100644 --- a/cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OAuthExample.java +++ b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OAuthExample.java @@ -19,7 +19,7 @@ * */ -package com.att.cadi.enduser; +package org.onap.aaf.cadi.enduser.test; import java.io.IOException; import java.net.ConnectException; diff --git a/cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OnapClientExample.java b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java index ca1bb948..441be4d2 100644 --- a/cadi/oauth-enduser/src/test/java/com/att/cadi/enduser/OnapClientExample.java +++ b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/OnapClientExample.java @@ -19,7 +19,7 @@ * */ -package com.att.cadi.enduser; +package org.onap.aaf.cadi.enduser.test; import java.io.IOException; import java.net.ConnectException; diff --git a/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/SimpleRestClientExample.java b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/SimpleRestClientExample.java new file mode 100644 index 00000000..7340618f --- /dev/null +++ b/cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/SimpleRestClientExample.java @@ -0,0 +1,91 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.cadi.enduser.test; + +import java.net.URISyntaxException; +import java.security.Principal; + +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.LocatorException; +import org.onap.aaf.cadi.enduser.ClientFactory; +import org.onap.aaf.cadi.enduser.SimpleRESTClient; +import org.onap.aaf.misc.env.APIException; + + +public class SimpleRestClientExample { + public final static void main(final String args[]) throws URISyntaxException, LocatorException { + try { + // Note: Expect ClientFactory to be long-lived... do NOT create more than once. + ClientFactory cf = new ClientFactory(args); + + + String urlString = cf.getAccess().getProperty("myurl", null); + if(urlString==null) { + System.out.println("Note: In your startup, add \"myurl=https://<aaf hello machine>:8130\" to command line\n\t" + + "OR\n\t" + + " add -Dmyurl=https://<aaf hello machine>:8130 to VM Args\n\t" + + "where \"aaf hello machine\" is an aaf Installation you know about."); + } else { + SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf"); + + // Make some calls + + // Call with no Queries + String rv = restClient.get("resthello"); + System.out.println(rv); + + // Call with Queries + rv = restClient.get("resthello?perm=org.osaaf.people|*|read"); + System.out.println(rv); + + // Call setting ID from principal coming from Trans + // Pretend Transaction + HRequest req = new HRequest("demo@people.osaaf.org"); // Pretend Trans has Jonathan as Identity + + rv = restClient.as(req.userPrincipal()).get("resthello?perm=org.osaaf.people|*|read"); + System.out.println(rv); + } + } catch (CadiException | APIException e) { + e.printStackTrace(); + } + } + + private static class HRequest { + + public HRequest(String fqi) { + name = fqi; + } + protected final String name; + + // fake out HttpServletRequest, only for get Principal + public Principal userPrincipal() { + return new Principal() { + + @Override + public String getName() { + return name; + } + + }; + } + } +} |