Add support for http PUT 07/63807/1
authormmis <michael.morris@ericsson.com>
Thu, 30 Aug 2018 12:34:55 +0000 (13:34 +0100)
committermmis <michael.morris@ericsson.com>
Thu, 30 Aug 2018 12:39:26 +0000 (13:39 +0100)
Added support for http PUT for use by the policy forwarder in
policy/distribution

Issue-ID: POLICY-926
Change-Id: Ifa5c2e8be0582797936b95b772ad236f35c10f24
Signed-off-by: mmis <michael.morris@ericsson.com>
policy-endpoints/pom.xml
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestEchoService.java

index aedb56d..03b3d59 100644 (file)
             <artifactId>jersey-client</artifactId>
             <version>${jersey.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-common</artifactId>
+            <version>${jersey.version}</version>
+        </dependency>
 
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
index f886e5c..6e8865f 100644 (file)
@@ -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();
+
 }
index c227071..0be8109 100644 (file)
@@ -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() {
index 7d37521..3b8c08f 100644 (file)
@@ -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));
     }
 
     /**
index 1248744..61525c3 100644 (file)
 
 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);
@@ -194,21 +205,31 @@ public class HttpClientTest {
         assertTrue(body.equals("hello"));
     }
 
+    @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;
+        }
+
+    }
 
 }
index fdb8770..56ed893 100644 (file)
@@ -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();
+    }
+
 }