From 19afb93b23f264471c6e8db77b54e9a301a3114b Mon Sep 17 00:00:00 2001 From: Instrumental Date: Thu, 21 Jun 2018 15:08:47 -0500 Subject: Update REST Samples for required behavior Issue-ID: AAF-361 Change-Id: Ia6dabfa3eeec2253c3f017ddc2d70afe4afbd54b Signed-off-by: Instrumental --- .../main/java/org/onap/aaf/cadi/client/Rcli.java | 317 +++++++++++---------- .../aaf/cadi/locator/SingleEndpointLocator.java | 82 ++++++ .../org/onap/aaf/cadi/client/test/JU_Rcli.java | 40 ++- 3 files changed, 264 insertions(+), 175 deletions(-) create mode 100644 cadi/client/src/main/java/org/onap/aaf/cadi/locator/SingleEndpointLocator.java (limited to 'cadi/client/src') diff --git a/cadi/client/src/main/java/org/onap/aaf/cadi/client/Rcli.java b/cadi/client/src/main/java/org/onap/aaf/cadi/client/Rcli.java index c93d233a..a98feb25 100644 --- a/cadi/client/src/main/java/org/onap/aaf/cadi/client/Rcli.java +++ b/cadi/client/src/main/java/org/onap/aaf/cadi/client/Rcli.java @@ -57,7 +57,7 @@ public abstract class Rcli { protected int readTimeout = 5000; protected int connectionTimeout = 3000; protected URI uri; - private String queryParams, fragment; + private String oneCallQueryParams; public static Pool buffPool = new Pool(new Pool.Creator() { @Override public byte[] create() throws APIException { @@ -132,15 +132,15 @@ public abstract class Rcli { protected abstract EClient client() throws CadiException; - public Future create(String pathinfo, String contentType, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + public Future create(final String pathinfo, final String contentType, final RosettaDF df, final T t) throws APIException, CadiException { + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,contentType); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -148,19 +148,18 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureCreate(df.getTypeClass()); } public Future create(String pathinfo, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,typeString(df.getTypeClass())); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -168,19 +167,18 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureCreate(df.getTypeClass()); } public Future create(String pathinfo, Class cls, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,typeString(cls)); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -188,37 +186,34 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureCreate(df.getTypeClass()); } public Future create(String pathinfo, Class cls) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,typeString(cls)); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(null); client.send(); - queryParams = fragment = null; return client.futureCreate(cls); } public Future create(String pathinfo, String contentType) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,contentType); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(null); client.send(); - queryParams = fragment = null; return client.futureCreate(Void.class); } @@ -237,7 +232,7 @@ public abstract class Rcli { * @throws CadiException */ public Future postForm(String pathinfo, final RosettaDF df, final String ... formParam) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); @@ -252,9 +247,9 @@ public abstract class Rcli { default: break; } - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -280,7 +275,6 @@ public abstract class Rcli { } }}); client.send(); - queryParams = fragment = null; return client.futureRead(df,TYPE.JSON); } @@ -296,14 +290,14 @@ public abstract class Rcli { * @throws CadiException */ public Future readPost(String pathinfo, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,typeString(df.getTypeClass())); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -311,7 +305,6 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureReadString(); } @@ -327,14 +320,14 @@ public abstract class Rcli { * @throws CadiException */ public Future readPost(String pathinfo, final RosettaDF df, final T t, final RosettaDF resp) throws APIException, CadiException { - final String qp = setupParams(pathinfo); - + final ParsePath pp = new ParsePath(pathinfo); + EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,typeString(df.getTypeClass())); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -342,30 +335,28 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureRead(resp,resp.getOutType()); } public Future readPost(String pathinfo, String contentType, String ... headers) throws CadiException, APIException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(POST); client.addHeader(CONTENT_TYPE,contentType); - client.setPathInfo(pathinfo); - client.setQueryParams(qp); - client.setFragment(fragment); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { }}); client.send(); - queryParams = fragment = null; return client.futureReadString(); } public Future read(String pathinfo, String accept, String ... headers) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(GET); @@ -374,19 +365,16 @@ public abstract class Rcli { for(int i=1;i Future read(String pathinfo, String accept, RosettaDF df, String ... headers) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(GET); @@ -394,18 +382,16 @@ public abstract class Rcli { for(int i=1;i Future read(String pathinfo, RosettaDF df,String ... headers) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(GET); @@ -413,41 +399,39 @@ public abstract class Rcli { for(int i=1;i Future read(String pathinfo, Class cls, RosettaDF df) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(GET); client.addHeader(ACCEPT, typeString(cls)); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); - + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(null); client.send(); - queryParams = fragment = null; return client.futureRead(df,type); } public Future update(String pathinfo, String contentType, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(PUT); client.addHeader(CONTENT_TYPE,contentType); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -455,19 +439,19 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.future(t); } public Future updateRespondString(String pathinfo, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); - + final ParsePath pp = new ParsePath(pathinfo); + EClient client = client(); client.setMethod(PUT); client.addHeader(CONTENT_TYPE, typeString(df.getTypeClass())); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -475,20 +459,20 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.futureReadString(); } public Future update(String pathinfo, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(PUT); client.addHeader(CONTENT_TYPE, typeString(df.getTypeClass())); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -496,19 +480,19 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.future(t); } public Future update(String pathinfo, Class cls, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); - + final ParsePath pp = new ParsePath(pathinfo); + EClient client = client(); client.setMethod(PUT); client.addHeader(CONTENT_TYPE, typeString(cls)); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -516,7 +500,6 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.future(t); } @@ -530,33 +513,34 @@ public abstract class Rcli { * @throws CadiException */ public Future update(String pathinfo) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(PUT); client.addHeader(CONTENT_TYPE, typeString(Void.class)); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + // client.setPayload(new EClient.Transfer() { // @Override // public void transfer(OutputStream os) throws IOException, APIException { // } // }); client.send(); - queryParams = fragment = null; return client.future(null); } public Future delete(String pathinfo, String contentType, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(DELETE); client.addHeader(CONTENT_TYPE, contentType); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -564,19 +548,18 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.future(t); } public Future delete(String pathinfo, Class cls, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(DELETE); client.addHeader(CONTENT_TYPE, typeString(cls)); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -584,19 +567,18 @@ public abstract class Rcli { } }); client.send(); - queryParams = fragment = null; return client.future(t); } public Future delete(String pathinfo, final RosettaDF df, final T t) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(DELETE); client.addHeader(CONTENT_TYPE, typeString(df.getTypeClass())); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); client.setPayload(new EClient.Transfer() { @Override public void transfer(OutputStream os) throws IOException, APIException { @@ -605,38 +587,37 @@ public abstract class Rcli { }); client.send(); - queryParams = fragment = null; return client.future(t); } public Future delete(String pathinfo, Class cls) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(DELETE); client.addHeader(CONTENT_TYPE, typeString(cls)); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(null); client.send(); - queryParams = fragment = null; return client.future((T)null); } public Future delete(String pathinfo, String contentType) throws APIException, CadiException { - final String qp = setupParams(pathinfo); + final ParsePath pp = new ParsePath(pathinfo); EClient client = client(); client.setMethod(DELETE); client.addHeader(CONTENT_TYPE, contentType); - client.setQueryParams(qp); - client.setFragment(fragment); - client.setPathInfo(pathinfo); + client.setPathInfo(pp.path()); + client.setQueryParams(pp.query()); + client.setFragment(pp.frag()); + client.setPayload(null); client.send(); - queryParams = fragment = null; return client.future(null); } @@ -680,47 +661,75 @@ public abstract class Rcli { return client.future(resp, expected); } - private String setupParams(String pathinfo) { - final String qp; - if(pathinfo==null) { - qp=queryParams; - } else { - final int idx = pathinfo.indexOf('?'); - if(idx>=0) { - qp=pathinfo.substring(idx+1); - pathinfo=pathinfo.substring(0,idx); + private class ParsePath { + private final String path; + private final int query; + private final int queryEnd; + private final int pound; + private final String queryParams; + + public ParsePath(final String origPath) { + path = origPath; + if(origPath==null) { + query=queryEnd=pound=-1; + queryParams=null; } else { - qp=queryParams; + query = origPath.indexOf('?'); + pound = origPath.indexOf('#'); + queryEnd = pound>=0?pound:path.length(); + if(oneCallQueryParams==null) { + if(query>=0) { + queryParams = path.substring(query+1,queryEnd); + } else { + queryParams=null; + } + } else { + if(query>=0) { + queryParams = oneCallQueryParams + '&' + path.substring(query+1,queryEnd); + } else { + queryParams = oneCallQueryParams; + } + oneCallQueryParams = null; + } + } + } + + public String path() { + if(query>=0) { + if(pound>=0) { + return path.substring(pound+1); + } + return path.substring(0,query); + } else if(pound>=0) { + return path.substring(0,pound); + } else { + return path; + } + } + + public String query() { + return queryParams; + } + + public String frag() { + if(pound>=0) { + return path.substring(pound+1); + } else { + return null; } } - return qp; } public String toString() { return uri.toString(); } - /** - * @param queryParams the queryParams to set - * @return - */ - public Rcli setQueryParams(String queryParams) { - this.queryParams = queryParams; - return this; - } - - - /** - * @param fragment the fragment to set - * @return - */ - public Rcli setFragment(String fragment) { - this.fragment = fragment; - return this; - } - public URI getURI() { return uri; } + public void setQueryParams(final String queryParams) { + oneCallQueryParams=queryParams; + } + } \ No newline at end of file diff --git a/cadi/client/src/main/java/org/onap/aaf/cadi/locator/SingleEndpointLocator.java b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/SingleEndpointLocator.java new file mode 100644 index 00000000..b0c830f6 --- /dev/null +++ b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/SingleEndpointLocator.java @@ -0,0 +1,82 @@ +/** + * ============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.locator; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.onap.aaf.cadi.Locator; +import org.onap.aaf.cadi.LocatorException; + +public class SingleEndpointLocator implements Locator { + private final URI uri; + private final static Item item = new Item() {}; + + public SingleEndpointLocator(final URI uri) { + this.uri = uri; + } + + public SingleEndpointLocator(final String endpoint) throws URISyntaxException { + this.uri = new URI(endpoint); + } + + @Override + public URI get(Item item) throws LocatorException { + return uri; + } + + @Override + public boolean hasItems() { + return true; + } + + @Override + public void invalidate(Item item) throws LocatorException { + // Endpoints cannot be invalidated + } + + @Override + public Item best() throws LocatorException { + return item; + } + + @Override + public Item first() throws LocatorException { + return item; + } + + @Override + public Item next(Item inItem) throws LocatorException { + // only one item + return null; + } + + @Override + public boolean refresh() { + // Never refreshed + return true; + } + + @Override + public void destroy() { + // Nothing to do here + } +} diff --git a/cadi/client/src/test/java/org/onap/aaf/cadi/client/test/JU_Rcli.java b/cadi/client/src/test/java/org/onap/aaf/cadi/client/test/JU_Rcli.java index f957878b..886c5d84 100644 --- a/cadi/client/src/test/java/org/onap/aaf/cadi/client/test/JU_Rcli.java +++ b/cadi/client/src/test/java/org/onap/aaf/cadi/client/test/JU_Rcli.java @@ -21,22 +21,10 @@ package org.onap.aaf.cadi.client.test; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; -import org.mockito.*; - -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.SecuritySetter; -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.misc.env.APIException; -import org.onap.aaf.misc.env.Data; -import org.onap.aaf.misc.env.Data.TYPE; -import org.onap.aaf.misc.rosetta.env.RosettaDF; -import org.onap.aaf.misc.rosetta.env.RosettaData; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -50,6 +38,21 @@ import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.SecuritySetter; +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.misc.env.APIException; +import org.onap.aaf.misc.env.Data; +import org.onap.aaf.misc.env.Data.TYPE; +import org.onap.aaf.misc.rosetta.env.RosettaDF; +import org.onap.aaf.misc.rosetta.env.RosettaData; + public class JU_Rcli { @Mock RosettaDF dfMock; @@ -61,8 +64,6 @@ public class JU_Rcli { private final static String uriString = "example.com"; private final static String apiVersion = "v1.0"; - private final static String fragment = "framgent"; - private final static String queryParams = "queryParams"; private final static String contentType = "contentType"; private static URI uri; @@ -224,9 +225,6 @@ public class JU_Rcli { rcli.apiVersion(null); assertThat(rcli.typeString(HttpURLConnection.class), is("application/HttpURLConnection+xml")); - rcli.setFragment(fragment); - rcli.setQueryParams(queryParams); - rcliClone = rcli.forUser(null); assertThat(rcliClone.toString(), is(uriString)); } -- cgit 1.2.3-korg