You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2019/05/08 17:14:21 UTC

[cxf] branch 3.2.x-fixes updated (aa007f2 -> ce7d8f3)

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a change to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git.


    from aa007f2  Recording .gitmergeinfo Changes
     new 128d12b  Adding Client Cache tests for JAX-RS
     new ce7d8f3  Recording .gitmergeinfo Changes

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .gitmergeinfo                                      |   1 +
 systests/jaxrs/pom.xml                             |  12 ++
 .../org/apache/cxf/systest/jaxrs/BookStore.java    |  51 ++++++++-
 .../systest/jaxrs/JAXRSClientServerBookTest.java   | 125 ++++++++++++++++++++-
 4 files changed, 179 insertions(+), 10 deletions(-)


[cxf] 01/02: Adding Client Cache tests for JAX-RS

Posted by co...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 128d12b9c8cc09a61660740239eac1a12276da77
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Wed May 8 18:08:53 2019 +0100

    Adding Client Cache tests for JAX-RS
    
    (cherry picked from commit 4fea734b1349ba023c2e560deda4aa0eaede7f3b)
    
    # Conflicts:
    #	systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
---
 systests/jaxrs/pom.xml                             |  12 ++
 .../org/apache/cxf/systest/jaxrs/BookStore.java    |  51 ++++++++-
 .../systest/jaxrs/JAXRSClientServerBookTest.java   | 125 ++++++++++++++++++++-
 3 files changed, 178 insertions(+), 10 deletions(-)

diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml
index bb32fd3..90e8b81 100644
--- a/systests/jaxrs/pom.xml
+++ b/systests/jaxrs/pom.xml
@@ -546,6 +546,18 @@
             <version>${cxf.reactor.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>javax.cache</groupId>
+            <artifactId>cache-api</artifactId>
+            <version>${cxf.jcache.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ehcache</groupId>
+            <artifactId>ehcache</artifactId>
+            <version>${cxf.ehcache3.version}</version>
+            <scope>test</scope>
+       </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index fd218c4..f73df80 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -19,7 +19,6 @@
 
 package org.apache.cxf.systest.jaxrs;
 
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -32,6 +31,7 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
+import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.List;
@@ -61,6 +61,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.container.ResourceContext;
+import javax.ws.rs.core.CacheControl;
 import javax.ws.rs.core.Configuration;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Cookie;
@@ -1030,17 +1031,59 @@ public class BookStore {
         return books.get(id + 123);
     }
 
-
-
     @GET
     @Path("/books/response/{bookId}/")
     @Produces("application/xml")
     public Response getBookAsResponse(@PathParam("bookId") String id) throws BookNotFoundFault {
         Book entity = doGetBook(id);
         EntityTag etag = new EntityTag(Integer.toString(entity.hashCode()));
-        return Response.ok().tag(etag).entity(entity).build();
+
+        CacheControl cacheControl = new CacheControl();
+        cacheControl.setMaxAge(100000);
+        cacheControl.setPrivate(true);
+
+        return Response.ok().tag(etag).entity(entity).cacheControl(cacheControl).build();
+    }
+
+    @GET
+    @Path("/books/response2/{bookId}/")
+    @Produces("application/xml")
+    public Response getBookAsResponse2(@PathParam("bookId") String id) throws BookNotFoundFault {
+        Book entity = doGetBook(id);
+        EntityTag etag = new EntityTag(Integer.toString(entity.hashCode()));
+
+        CacheControl cacheControl = new CacheControl();
+        cacheControl.setMaxAge(1);
+        cacheControl.setPrivate(true);
+
+        return Response.ok().tag(etag).entity(entity).cacheControl(cacheControl).build();
+    }
+
+    @GET
+    @Path("/books/response3/{bookId}/")
+    @Produces("application/xml")
+    public Response getBookAsResponse3(@PathParam("bookId") String id,
+                                       @HeaderParam("If-Modified-Since") String modifiedSince
+    ) throws BookNotFoundFault {
+        Book entity = doGetBook(id);
+
+        EntityTag etag = new EntityTag(Integer.toString(entity.hashCode()));
+
+        CacheControl cacheControl = new CacheControl();
+        cacheControl.setMaxAge(1);
+        cacheControl.setPrivate(true);
+
+        if (modifiedSince != null) {
+            return Response.status(304).tag(etag)
+                .cacheControl(cacheControl).lastModified(new Date()).build();
+        } else {
+            return Response.ok().tag(etag).entity(entity)
+                .cacheControl(cacheControl).lastModified(new Date()).build();
+        }
     }
 
+
+
     @GET
     @Path("/books/{bookId}/cglib")
     @Produces("application/xml")
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 54b3d20..663d187 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -43,7 +43,10 @@ import javax.ws.rs.NotAcceptableException;
 import javax.ws.rs.ProcessingException;
 import javax.ws.rs.ServerErrorException;
 import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.ResponseProcessingException;
+import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.Form;
 import javax.ws.rs.core.GenericEntity;
 import javax.ws.rs.core.GenericType;
@@ -62,6 +65,7 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
 import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.client.cache.CacheControlFeature;
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
@@ -756,21 +760,130 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase {
     }
 
     @Test
-    public void testGetBookResponseAndETag() throws Exception {
+    public void testCaching() throws Exception {
 
         String endpointAddress =
             "http://localhost:" + PORT + "/bookstore/books/response/123";
-        WebClient wc = WebClient.create(endpointAddress);
-        Book book = wc.get(Book.class);
-        assertEquals(200, wc.getResponse().getStatus());
+
+        // Add the CacheControlFeature to cache books returned by the service on the client side
+        CacheControlFeature cacheControlFeature = new CacheControlFeature();
+        cacheControlFeature.setCacheResponseInputStream(true);
+        Client client = ClientBuilder.newBuilder()
+            .register(cacheControlFeature)
+            .build();
+        WebTarget target = client.target(endpointAddress);
+
+        // First call
+        Response response = target.request().get();
+        assertEquals(200, response.getStatus());
+        Book book = response.readEntity(Book.class);
         assertEquals(123L, book.getId());
-        MultivaluedMap<String, Object> headers = wc.getResponse().getMetadata();
-        assertTrue(!headers.isEmpty());
+
+        MultivaluedMap<String, Object> headers = response.getMetadata();
+        assertFalse(headers.isEmpty());
         Object etag = headers.getFirst("ETag");
         assertNotNull(etag);
         assertTrue(etag.toString().startsWith("\""));
         assertTrue(etag.toString().endsWith("\""));
 
+        Object cacheControl = headers.getFirst("Cache-Control");
+        assertNotNull(cacheControl);
+        assertTrue(cacheControl.toString().contains("private"));
+        assertTrue(cacheControl.toString().contains("max-age=100000"));
+
+        // Now make a second call. This should be retrieved from the client's cache
+        target.request().get();
+        assertEquals(200, response.getStatus());
+        book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+
+        cacheControlFeature.close();
+    }
+
+    @Test
+    public void testCachingExpires() throws Exception {
+
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/books/response2/123";
+
+        // Add the CacheControlFeature to cache books returned by the service on the client side
+        CacheControlFeature cacheControlFeature = new CacheControlFeature();
+        cacheControlFeature.setCacheResponseInputStream(true);
+        Client client = ClientBuilder.newBuilder()
+            .register(cacheControlFeature)
+            .build();
+        WebTarget target = client.target(endpointAddress);
+
+        // First call
+        Response response = target.request().get();
+        assertEquals(200, response.getStatus());
+        Book book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+
+        MultivaluedMap<String, Object> headers = response.getMetadata();
+        assertFalse(headers.isEmpty());
+        Object etag = headers.getFirst("ETag");
+        assertNotNull(etag);
+        assertTrue(etag.toString().startsWith("\""));
+        assertTrue(etag.toString().endsWith("\""));
+
+        Object cacheControl = headers.getFirst("Cache-Control");
+        assertNotNull(cacheControl);
+        assertTrue(cacheControl.toString().contains("private"));
+        assertTrue(cacheControl.toString().contains("max-age=1"));
+
+        // Now make a second call. The value in the cache will have expired, so
+        // it should call the service again
+        Thread.sleep(1500L);
+        target.request().get();
+        assertEquals(200, response.getStatus());
+        book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+
+        cacheControlFeature.close();
+    }
+
+    @Test
+    public void testCachingExpiresUsingETag() throws Exception {
+
+        String endpointAddress =
+            "http://localhost:" + PORT + "/bookstore/books/response3/123";
+
+        // Add the CacheControlFeature to cache books returned by the service on the client side
+        CacheControlFeature cacheControlFeature = new CacheControlFeature();
+        cacheControlFeature.setCacheResponseInputStream(true);
+        Client client = ClientBuilder.newBuilder()
+            .register(cacheControlFeature)
+            .build();
+        WebTarget target = client.target(endpointAddress);
+
+        // First call
+        Response response = target.request().get();
+        assertEquals(200, response.getStatus());
+        Book book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+
+        MultivaluedMap<String, Object> headers = response.getMetadata();
+        assertFalse(headers.isEmpty());
+        Object etag = headers.getFirst("ETag");
+        assertNotNull(etag);
+        assertTrue(etag.toString().startsWith("\""));
+        assertTrue(etag.toString().endsWith("\""));
+
+        Object cacheControl = headers.getFirst("Cache-Control");
+        assertNotNull(cacheControl);
+        assertTrue(cacheControl.toString().contains("private"));
+        assertTrue(cacheControl.toString().contains("max-age=1"));
+
+        // Now make a second call. The value in the clients cache will have expired, so it should call
+        // out to the service, which will return 304, and the client will re-use the cached payload
+        Thread.sleep(1500L);
+        target.request().get();
+        assertEquals(200, response.getStatus());
+        book = response.readEntity(Book.class);
+        assertEquals(123L, book.getId());
+
+        cacheControlFeature.close();
     }
 
 


[cxf] 02/02: Recording .gitmergeinfo Changes

Posted by co...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 3.2.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit ce7d8f3c9370b151a3d64f7b7fe22ac8cfeabd3b
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Wed May 8 18:10:00 2019 +0100

    Recording .gitmergeinfo Changes
---
 .gitmergeinfo | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitmergeinfo b/.gitmergeinfo
index 073770f..8d8933b 100644
--- a/.gitmergeinfo
+++ b/.gitmergeinfo
@@ -540,6 +540,7 @@ M 4a07401299848a1d9b54346b6827132cdf5ec80b
 M 4a9d4a1b6e9ce5423c3d84d46978287213e6495f
 M 4f8ae843f56e3ef84af43ae90f507e196726f0bf
 M 4f9923c32688c57e31f933c69d9c2a667f20d63d
+M 4fea734b1349ba023c2e560deda4aa0eaede7f3b
 M 504a1b7827bc76f3c5106b901b44e54513db17aa
 M 52f2cc152ceccd3981361dc840338c16416786fa
 M 56f74c18d7c709307b174cb211b28e4384574e9b