diff options
Diffstat (limited to 'cadi/oauth-enduser')
3 files changed, 102 insertions, 14 deletions
diff --git a/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/RESTException.java b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/RESTException.java new file mode 100644 index 00000000..95c9fe85 --- /dev/null +++ b/cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/RESTException.java @@ -0,0 +1,66 @@ +/** + * ============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 org.onap.aaf.cadi.client.Future; + +public class RESTException extends Exception { + /** + * + */ + private static final long serialVersionUID = -5232371598208651058L; + private Future<?> future; + + public RESTException(Future<?> future) { + this.future = future; + } + + public int getCode() { + return future.code(); + } + + public String getMsg() { + return future.body(); + } + + public String errorString() { + String body = future.body(); + return "RESTClient Error: " + future.code() + ": " + (body.isEmpty()?"<no message in call>":body); + } + + /* (non-Javadoc) + * @see java.lang.Throwable#getMessage() + */ + @Override + public String getMessage() { + return errorString(); + } + + /* (non-Javadoc) + * @see java.lang.Throwable#getLocalizedMessage() + */ + @Override + public String getLocalizedMessage() { + return errorString(); + } + + +} 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 index 9535ad64..386d938c 100644 --- 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 @@ -80,7 +80,7 @@ public class SimpleRESTClient { } //Format:<ID>:<APP>:<protocol>[:AS][,<ID>:<APP>:<protocol>]* - public SimpleRESTClient as(Principal principal) { + public SimpleRESTClient endUser(Principal principal) { if(principal==null) { chain = null; } else { @@ -93,23 +93,27 @@ public class SimpleRESTClient { } return this; } - - public String get(final String path) throws CadiException, LocatorException, APIException { + + public String read(final String path) throws RESTException, CadiException, LocatorException, APIException { + return get(path,"application/json"); + } + + public String get(final String path) throws RESTException, 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>() { + public String get(final String path, final String accepts) throws RESTException, CadiException, LocatorException, APIException { + Future<String> future = restClient.best(new Retryable<Future<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 Future<String> code(Rcli<?> client) throws CadiException, ConnectException, APIException { + return client.read(path,accepts, headers()); } }); + if(future.get(callTimeout)) { + return future.value; + } else { + throw new RESTException(future); + } } public interface Headers { 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 index 7340618f..7d251bad 100644 --- 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 @@ -27,6 +27,7 @@ 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.RESTException; import org.onap.aaf.cadi.enduser.SimpleRESTClient; import org.onap.aaf.misc.env.APIException; @@ -53,6 +54,11 @@ public class SimpleRestClientExample { String rv = restClient.get("resthello"); System.out.println(rv); + // Same call with "read" style + rv = restClient.read("resthello"); + System.out.println(rv); + + // Call with Queries rv = restClient.get("resthello?perm=org.osaaf.people|*|read"); System.out.println(rv); @@ -61,10 +67,22 @@ public class SimpleRestClientExample { // 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"); + // Call with RESTException, which allows obtaining HTTPCode and any Error message sent + rv = restClient.endUser(req.userPrincipal()).get("resthello?perm=org.osaaf.people|*|read"); System.out.println(rv); + + try { + restClient.get("notAnAPI"); + } catch(RESTException e) { + System.out.println(e.getCode()); + System.out.println(e.getMsg()); + System.out.println(e.getMessage()); + System.out.println(e.getLocalizedMessage()); + System.out.println(e); + } + } - } catch (CadiException | APIException e) { + } catch (CadiException | APIException | RESTException e) { e.printStackTrace(); } } |