diff options
author | Instrumental <jonathan.gathman@att.com> | 2018-07-22 07:49:16 -0500 |
---|---|---|
committer | Instrumental <jonathan.gathman@att.com> | 2018-07-22 07:49:20 -0500 |
commit | cf52d77279f8d5a70429d45abad0ef3d1135070c (patch) | |
tree | f084fd660d3e201abc853df8f17542a044120070 /cadi/oauth-enduser/src | |
parent | 65c40b3bffb78a1a77d82fc74bbf633a6d0d906f (diff) |
Finish RESTful Client API
Issue-ID: AAF-361
Change-Id: I999cb419a79c5995baf3757fdbbe047528901597
Signed-off-by: Instrumental <jonathan.gathman@att.com>
Diffstat (limited to 'cadi/oauth-enduser/src')
-rw-r--r-- | cadi/oauth-enduser/src/main/java/org/onap/aaf/cadi/enduser/SimpleRESTClient.java | 175 | ||||
-rw-r--r-- | cadi/oauth-enduser/src/test/java/org/onap/aaf/cadi/enduser/test/SimpleRestClientExample.java | 150 |
2 files changed, 303 insertions, 22 deletions
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 386d938c..30344de5 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 @@ -21,11 +21,14 @@ package org.onap.aaf.cadi.enduser; import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; 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.EClient; import org.onap.aaf.cadi.client.Future; import org.onap.aaf.cadi.client.Rcli; import org.onap.aaf.cadi.client.Result; @@ -37,8 +40,10 @@ 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; +import org.onap.aaf.misc.env.util.StringBuilderWriter; public class SimpleRESTClient { + private static final String APPLICATION_JSON = "application/json"; private static final String[] EMPTY = new String[0]; private final TokenClient tokenClient; private final TzClient restClient; @@ -93,13 +98,116 @@ public class SimpleRESTClient { } return this; } + + /** + * Single Threaded Class for building up content + * @author jg1555 + * + */ + public static class Input { + private static final byte[] EMPTY_STREAM_BYTES = "".getBytes(); + + private String content; + private StringBuilder sb; + + public Input() { + content = null; + sb = null; + } + + public Input(final String content) { + this.content = content; + } + + public void set(final String content) { + this.content = content; + } + + public PrintWriter writer() { + return new PrintWriter(new StringBuilderWriter(builder())); + } + + public StringBuilder builder() { + if(sb==null) { + sb = new StringBuilder(); + content = null; + } + return sb; + } + + /** + * Reuse StringBuilder object + */ + public void clear() { + content = null; + if(sb!=null) { + sb.setLength(0); + } + } + + @Override + public String toString() { + if(content!=null) { + return content; + } else if(sb!=null) { + return sb.toString(); + } else { + return ""; + } + } + + public byte[] getBytes() { + byte[] rv; + if(content==null) { + if(sb==null) { + rv = EMPTY_STREAM_BYTES; + } else { + rv = sb.toString().getBytes(); + } + } else { + rv = content.getBytes(); + } + content = null; + return rv; + } + } + + ///////////////////////////////////////////////////////////// + // + // CREATE + // + ///////////////////////////////////////////////////////////// + public void create(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException { + post(path,APPLICATION_JSON, input); + } + + public void post(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException { + post(path,APPLICATION_JSON, input); + } + + public void post(final String path, final String contentType, final Input input) throws RESTException, CadiException, LocatorException, APIException { + Future<Void> future = restClient.best(new Retryable<Future<Void>>() { + @Override + public Future<Void> code(Rcli<?> client) throws CadiException, ConnectException, APIException { + return client.create(path, contentType, new ETransfer(input)); + } + }); + if(!future.get(callTimeout)) { + throw new RESTException(future); + } + } + ///////////////////////////////////////////////////////////// + // + // READ + // + ///////////////////////////////////////////////////////////// public String read(final String path) throws RESTException, CadiException, LocatorException, APIException { - return get(path,"application/json"); + return get(path,APPLICATION_JSON); } public String get(final String path) throws RESTException, CadiException, LocatorException, APIException { - return get(path,"application/json"); + return get(path,APPLICATION_JSON); } public String get(final String path, final String accepts) throws RESTException, CadiException, LocatorException, APIException { @@ -116,6 +224,69 @@ public class SimpleRESTClient { } } + ///////////////////////////////////////////////////////////// + // + // UPDATE + // + ///////////////////////////////////////////////////////////// + + public String update(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException { + return put(path,APPLICATION_JSON, input); + } + + public String put(final String path, final Input input) throws RESTException, CadiException, LocatorException, APIException { + return put(path,APPLICATION_JSON, input); + } + + public String put(final String path, final String contentType, final Input input) throws RESTException, CadiException, LocatorException, APIException { + Future<String> future = restClient.best(new Retryable<Future<String>>() { + @Override + public Future<String> code(Rcli<?> client) throws CadiException, ConnectException, APIException { + return client.update(path, contentType, new ETransfer(input)); + } + }); + if(future.get(callTimeout)) { + return future.value; + } else { + throw new RESTException(future); + } + } + + ///////////////////////////////////////////////////////////// + // + // DELETE + // + ///////////////////////////////////////////////////////////// + public void delete(final String path) throws RESTException, CadiException, LocatorException, APIException { + delete(path,APPLICATION_JSON); + } + + public void delete(final String path, final String contentType) throws RESTException, CadiException, LocatorException, APIException { + Future<Void> future = restClient.best(new Retryable<Future<Void>>() { + @Override + public Future<Void> code(Rcli<?> client) throws CadiException, ConnectException, APIException { + return client.delete(path, contentType); + } + }); + if(!future.get(callTimeout)) { + throw new RESTException(future); + } + } + + ///////////////////////////////////////////////////////////// + + private static class ETransfer implements EClient.Transfer { + private Input input; + public ETransfer(final Input input) { + this.input = input; + } + + @Override + public void transfer(OutputStream os) throws IOException, APIException { + os.write(input.getBytes()); + } + } + public interface Headers { String[] 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 7d251bad..6cabc654 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 @@ -21,6 +21,7 @@ package org.onap.aaf.cadi.enduser.test; +import java.io.PrintWriter; import java.net.URISyntaxException; import java.security.Principal; @@ -29,6 +30,7 @@ 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.cadi.enduser.SimpleRESTClient.Input; import org.onap.aaf.misc.env.APIException; @@ -46,32 +48,93 @@ public class SimpleRestClientExample { + " 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); - - // Same call with "read" style - rv = restClient.read("resthello"); - System.out.println(rv); + SimpleRESTClient restClient = cf.simpleRESTClient(urlString,"org.osaaf.aaf"); + + ///////////////////////////////////////////////////////////// + // + // Creating Content for CREATE/UPDATE + // + ///////////////////////////////////////////////////////////// + // Create an object that can be reusable IN THIS THREAD ONLY... Not Thread-safe on purpose + Input input = new SimpleRESTClient.Input(); + // Note: alternate use is to set the input object to an already created String + // Input input = new SimpleRESTClient.Input(aString); - // Call with Queries - rv = restClient.get("resthello?perm=org.osaaf.people|*|read"); - System.out.println(rv); + PrintWriter pw = input.writer(); + pw.print("{\"something\": ["); + for(int i=0;i<4;++i) { + if(i>0) { + pw.print(','); + } + pw.print("{\"myint\":"); + pw.print(i); + pw.print('}'); + } + pw.println("]}"); - // Call setting ID from principal coming from Trans - // Pretend Transaction - HRequest req = new HRequest("demo@people.osaaf.org"); // Pretend Trans has Jonathan as Identity + // You can check or log the content + String content = input.toString(); + System.out.println(content); - // 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); + // Good form for Writers is that you should close it... + pw.close(); + + ///////////////////////////////////////////////////////////// + // + // CREATE/POST + // + ///////////////////////////////////////////////////////////// + System.out.println("-------- START REST CREATE/UPDATE --------"); + try { + restClient.create("resthello/rest_id", input); + // No Error code, it worked. + System.out.println("No Error Code, Create worked..."); + } catch (RESTException e) { + System.out.println(e.getCode()); + System.out.println(e.getMsg()); + } finally { + System.out.println("-------- END REST CREATE/UPDATE --------"); + } + + ///////////////////////////////////////////////////////////// + // + // READ/GET + // + ///////////////////////////////////////////////////////////// + + // Make some calls. Note that RESTException is thrown if Call does not complete. + // RESTException has HTTP Code and any Message sent from Server + System.out.println("-------- START REST READ/GET --------"); + boolean expectException = false; try { + + // Call with no Queries + String rv = restClient.get("resthello/rest_id"); + System.out.println(rv); + + // Same call with "read" style + rv = restClient.read("resthello/rest_id"); + System.out.println(rv); + + + // Call with Queries + rv = restClient.get("resthello/rest_id?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 + + // Call with RESTException, which allows obtaining HTTPCode and any Error message sent + rv = restClient.endUser(req.userPrincipal()).get("resthello/rest_id?perm=org.osaaf.people|*|read"); + System.out.println(rv); + + // Expect Exception here. + System.out.println("-------- START Expecting Exception starting here --------"); + expectException = true; restClient.get("notAnAPI"); } catch(RESTException e) { System.out.println(e.getCode()); @@ -79,10 +142,57 @@ public class SimpleRestClientExample { System.out.println(e.getMessage()); System.out.println(e.getLocalizedMessage()); System.out.println(e); + } finally { + if(expectException) { + System.out.println("-------- END Expecting Exception starting here --------"); + } + System.out.println("-------- END REST READ/GET --------"); } + ///////////////////////////////////////////////////////////// + // + // UPDATE/PUT + // + ///////////////////////////////////////////////////////////// + + + // If you use "input" object again as a writer, you can clear it on the same thread, and go again + input.clear(); + // Here we just set to a String, instead of Writing + input.set("{\"something\" : []}"); + + System.out.println("-------- END REST UPDATE/PUT --------"); + try { + String rv = restClient.update("resthello/rest_id", input); + // No Error code, it worked. REST Update will return the updated Data + System.out.println("Update worked"); + System.out.println(rv); + } catch (RESTException e) { + System.out.println(e.getCode()); + System.out.println(e.getMsg()); + } finally { + System.out.println("-------- END REST UPDATE/PUT --------"); + } + + ///////////////////////////////////////////////////////////// + // + // DELETE + // + ///////////////////////////////////////////////////////////// + + System.out.println("-------- START REST DELETE --------"); + try { + restClient.delete("resthello/rest_id"); + // No Error code, it worked. REST Update will return the updated Data + System.out.println("Delete worked"); + } catch (RESTException e) { + System.out.println(e.getCode()); + System.out.println(e.getMsg()); + } finally { + System.out.println("-------- END REST DELETE --------"); + } } - } catch (CadiException | APIException | RESTException e) { + } catch (CadiException | APIException e) { e.printStackTrace(); } } |