diff options
Diffstat (limited to 'policy-endpoints/src')
5 files changed, 106 insertions, 45 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java index f886e5ca..6e8865f5 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java @@ -20,6 +20,9 @@ package org.onap.policy.common.endpoints.http.client; +import java.util.Map; + +import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import org.onap.policy.common.capabilities.Startable; @@ -30,6 +33,8 @@ public interface HttpClient extends Startable { public Response get(); + public Response put(String path, Entity<?> entity, Map<String, Object> headers); + public static <T> T getBody(Response response, Class<T> entityType) { return response.readEntity(entityType); } @@ -54,4 +59,5 @@ public interface HttpClient extends Startable { public static final HttpClientFactory factory = new IndexedHttpClientFactory(); + } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index c227071c..0be81099 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -28,12 +28,16 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.Map; +import java.util.Map.Entry; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; @@ -66,14 +70,9 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * name the name - * https is it https or not - * selfSignedCerts are there self signed certs - * hostname the hostname - * port port being used - * basePath base context - * userName user - * password password + * name the name https is it https or not selfSignedCerts are there self signed certs hostname + * the hostname port port being used basePath base context userName user password password + * * @param busTopicParams Input parameters object * @throws KeyManagementException key exception * @throws NoSuchAlgorithmException no algorithm exception @@ -163,6 +162,14 @@ public class JerseyClient implements HttpClient { return this.client.target(this.baseUrl).request().get(); } + @Override + public Response put(String path, Entity<?> entity, Map<String, Object> headers) { + Builder builder = this.client.target(this.baseUrl).path(path).request(); + for (Entry<String, Object> header : headers.entrySet()) { + builder.header(header.getKey(), header.getValue()); + } + return builder.put(entity); + } @Override public boolean start() { diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java index 7d37521e..3b8c08fc 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java @@ -23,7 +23,9 @@ package org.onap.policy.common.endpoints.http.server.internal; import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.EnumSet; + import javax.servlet.DispatcherType; + import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.HashLoginService; @@ -164,7 +166,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable } else { this.connector = httpConnector(); } - + this.connector.setName(srvName); this.connector.setReuseAddress(true); this.connector.setPort(port); @@ -189,8 +191,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable tempFilterPath = "/*"; } - context.addFilter(filterClass, tempFilterPath, - EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); + context.addFilter(filterClass, tempFilterPath, EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST)); } /** diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java index 1248744d..61525c33 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java @@ -21,13 +21,17 @@ package org.onap.policy.common.endpoints.http.server.test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Properties; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.junit.AfterClass; @@ -51,7 +55,7 @@ public class HttpClientTest { * Setup before class method. * * @throws InterruptedException can be interrupted - * @throws IOException can have an IO exception + * @throws IOException can have an IO exception */ @BeforeClass public static void setUp() throws InterruptedException, IOException { @@ -76,21 +80,20 @@ public class HttpClientTest { String keyStorePasswordSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME); if (keyStorePasswordSystemProperty != null) { - savedValuesMap.put( - JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, keyStorePasswordSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_KEYSTORE_PASSWORD_PROPERTY_NAME, + keyStorePasswordSystemProperty); } String trustStoreSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME); if (trustStoreSystemProperty != null) { - savedValuesMap - .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PROPERTY_NAME, trustStoreSystemProperty); } String trustStorePasswordSystemProperty = System.getProperty(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME); if (trustStorePasswordSystemProperty != null) { - savedValuesMap - .put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, trustStorePasswordSystemProperty); + savedValuesMap.put(JettyJerseyServer.SYSTEM_TRUSTSTORE_PASSWORD_PROPERTY_NAME, + trustStorePasswordSystemProperty); } System.setProperty(JettyJerseyServer.SYSTEM_KEYSTORE_PROPERTY_NAME, "src/test/resources/keystore-test"); @@ -158,14 +161,12 @@ public class HttpClientTest { } @Test - public void testHttpNoAuthClient() throws Exception { + public void testHttpGetNoAuthClient() throws Exception { logger.info("-- testHttpNoAuthClient() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient") - .useHttps(false) - .allowSelfSignedCerts(false) - .hostname("localhost") - .port(6666).basePath("junit/echo").userName(null).password(null).managed(true).build()); + .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -174,18 +175,28 @@ public class HttpClientTest { } @Test - public void testHttpAuthClient() throws Exception { + public void testHttpPutNoAuthClient() throws Exception { + logger.info("-- testHttpNoAuthClient() --"); + + final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpNoAuthClient") + .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6666).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); + + Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON); + final Response response = client.put("hello", entity, Collections.emptyMap()); + final String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello:{myParameter=myValue}")); + } + + @Test + public void testHttpGetAuthClient() throws Exception { logger.info("-- testHttpAuthClient() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient") - .useHttps(true) - .allowSelfSignedCerts(true) - .hostname("localhost") - .port(6667) - .basePath("junit/echo") - .userName("x") - .password("y") - .managed(true).build()); + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName("x").password("y").managed(true).build()); final Response response = client.get("hello"); final String body = HttpClient.getBody(response, String.class); @@ -195,20 +206,30 @@ public class HttpClientTest { } @Test + public void testHttpPutAuthClient() throws Exception { + logger.info("-- testHttpAuthClient() --"); + + final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient") + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName("x").password("y").managed(true).build()); + + Entity<MyEntity> entity = Entity.entity(new MyEntity("myValue"), MediaType.APPLICATION_JSON); + final Response response = client.put("hello", entity, Collections.emptyMap()); + final String body = HttpClient.getBody(response, String.class); + + assertTrue(response.getStatus() == 200); + assertTrue(body.equals("hello:{myParameter=myValue}")); + } + + @Test public void testHttpAuthClient401() throws Exception { logger.info("-- testHttpAuthClient401() --"); final HttpClient client = HttpClient.factory.build(BusTopicParams.builder().clientName("testHttpAuthClient401") - .useHttps(true) - .allowSelfSignedCerts(true) - .hostname("localhost") - .port(6667) - .basePath("junit/echo") - .userName(null) - .password(null) - .managed(true).build()); + .useHttps(true).allowSelfSignedCerts(true).hostname("localhost").port(6667).basePath("junit/echo") + .userName(null).password(null).managed(true).build()); final Response response = client.get("hello"); - assertTrue(response.getStatus() == 401); + assertEquals(new Integer(response.getStatus()).toString(), response.getStatus(), 401); } @Test @@ -298,5 +319,22 @@ public class HttpClientTest { assertTrue(response2.getStatus() == 500); } + class MyEntity { + + private String myParameter; + + public MyEntity(final String myParameter) { + this.myParameter = myParameter; + } + + public void setMyParameter(final String myParameter) { + this.myParameter = myParameter; + } + + public String getMyParameter() { + return myParameter; + } + + } } diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java index fdb8770c..56ed8933 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java @@ -22,12 +22,15 @@ package org.onap.policy.common.endpoints.http.server.test; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; + import javax.ws.rs.GET; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; + @Api(value = "echo") @Path("/junit/echo") public class RestEchoService { @@ -35,11 +38,17 @@ public class RestEchoService { @GET @Path("{word}") @Produces(MediaType.TEXT_PLAIN) - @ApiOperation( - value = "echoes back whatever received" - ) - public String echo(@PathParam("word") String word) { + @ApiOperation(value = "echoes back whatever received") + public String echo(@PathParam("word") String word) { return word; } + @PUT + @Path("{word}") + @Produces(MediaType.TEXT_PLAIN) + @ApiOperation(value = "echoes back whatever received") + public String echoPut(@PathParam("word") String word, Object entity) { + return word + ":" + entity.toString(); + } + } |