cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [cxf] 01/02: Make org..client.cache.Entry serializable (cache by value), handle 304 status response
Date Wed, 12 Dec 2018 21:05:18 GMT
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 21d1bca310e5815fbbeb85df4ad6c7aec278475c
Author: Nicola Buso <nbuso@ebi.ac.uk>
AuthorDate: Wed Nov 14 10:39:26 2018 +0000

    Make org..client.cache.Entry serializable (cache by value), handle 304 status response
    
    (cherry picked from commit cf49b1a2fa653c41405027e4c1f4c30ca8231ae2)
---
 .../apache/cxf/jaxrs/client/AbstractClient.java    |  2 +-
 .../org/apache/cxf/jaxrs/client/WebClient.java     |  3 ++-
 .../cache/CacheControlClientRequestFilter.java     | 12 +++++++++++-
 .../org/apache/cxf/jaxrs/client/cache/Entry.java   | 18 ++++++++++++++----
 .../cxf/jaxrs/client/cache/ClientCacheTest.java    | 22 ++++++++++++++++++++++
 5 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
index 223e45d..01d448c 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
@@ -555,7 +555,7 @@ public abstract class AbstractClient implements Client {
         }
 
         int status = r.getStatus();
-        if ((status < 200 || status == 204) && r.getLength() <= 0 || status
>= 300) {
+        if ((status < 200 || status == 204) && r.getLength() <= 0 || (status
>= 300 && status != 304)) {
             return null;
         }
         return ((ResponseImpl)r).doReadEntity(cls, type, anns);
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
index 38a62ec..ca0af79 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
@@ -900,7 +900,8 @@ public class WebClient extends AbstractClient {
             resetResponseStateImmediatelyIfNeeded();
         }
         
-        if (r.getStatus() >= 300 && responseClass != Response.class) {
+        int status = r.getStatus();
+        if (status != 304 && status >= 300 && responseClass != Response.class)
{
             throw convertToWebApplicationException(r);
         }
         return r;
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientRequestFilter.java
b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientRequestFilter.java
index 9542db1..0d4c625 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientRequestFilter.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientRequestFilter.java
@@ -32,6 +32,8 @@ import javax.ws.rs.client.ClientRequestFilter;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response;
 
+import org.apache.cxf.common.util.StringUtils;
+
 @Priority(Priorities.USER - 1)
 public class CacheControlClientRequestFilter implements ClientRequestFilter {
     static final String NO_CACHE_PROPERTY = "no_client_cache";
@@ -64,7 +66,15 @@ public class CacheControlClientRequestFilter implements ClientRequestFilter
{
             //TODO: do the extra validation against the conditional headers
             //      which may be contained in the current request
             if (entry.isOutDated()) {
-                cache.remove(key, entry);
+                String ifNoneMatchHeader = entry.getCacheHeaders().get(HttpHeaders.IF_NONE_MATCH);
+                String ifModifiedSinceHeader = entry.getCacheHeaders().get(HttpHeaders.IF_MODIFIED_SINCE);
+                
+                if (StringUtils.isEmpty(ifNoneMatchHeader) && StringUtils.isEmpty(ifModifiedSinceHeader))
{
+                    cache.remove(key, entry);
+                } else {
+                    request.getHeaders().add(HttpHeaders.IF_NONE_MATCH, ifNoneMatchHeader);
+                    request.setProperty(CACHED_ENTITY_PROPERTY, entry.getData());
+                }
             } else {
                 Object cachedEntity = entry.getData();
                 Response.ResponseBuilder ok = Response.ok(cachedEntity);
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/Entry.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/Entry.java
index a93df67..20775f4 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/Entry.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/Entry.java
@@ -20,22 +20,25 @@ package org.apache.cxf.jaxrs.client.cache;
 
 import java.io.Serializable;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.MultivaluedHashMap;
 import javax.ws.rs.core.MultivaluedMap;
 
 public class Entry implements Serializable {
     private static final long serialVersionUID = -3551501551331222546L;
     private Map<String, String> cacheHeaders = Collections.emptyMap();
     private Serializable data;
-    private MultivaluedMap<String, String> headers;
+    private Map<String, List<String>> headers;
     private long expiresValue;
     private long initialTimestamp = now();
 
     public Entry(final Serializable data, final MultivaluedMap<String, String> headers,
                  final Map<String, String> cacheHeaders, final long expiresHeaderValue)
{
         this.data = data;
-        this.headers = headers;
+        initHeaders(headers);
         this.cacheHeaders = cacheHeaders;
         this.expiresValue = expiresHeaderValue;
     }
@@ -65,11 +68,18 @@ public class Entry implements Serializable {
     }
 
     public MultivaluedMap<String, String> getHeaders() {
-        return headers;
+        final MultivaluedHashMap<String, String> toReturn = new MultivaluedHashMap<String,
String>();
+        toReturn.putAll(headers);
+        return toReturn;
     }
 
+    private void initHeaders(final  MultivaluedMap<String, String> mHeaders) {
+        this.headers = new HashMap<String, List<String>>();
+        headers.putAll(mHeaders);
+    }
+    
     public void setHeaders(final MultivaluedMap<String, String> headers) {
-        this.headers = headers;
+        initHeaders(headers);
     }
 
     public long getExpiresValue() {
diff --git a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/cache/ClientCacheTest.java
b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/cache/ClientCacheTest.java
index 7b39971..5dfd735 100644
--- a/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/cache/ClientCacheTest.java
+++ b/rt/rs/client/src/test/java/org/apache/cxf/jaxrs/client/cache/ClientCacheTest.java
@@ -158,7 +158,29 @@ public class ClientCacheTest extends Assert {
             feature.close();
         }
     }
+    
 
+    @Test
+    public void testGetJaxbBookCacheByValue() {
+        // org.apache.cxf.jaxrs.client.cache.CacheControlFeature.storeByValue
+        CacheControlFeature feature = new CacheControlFeature();
+        try {
+            final WebTarget base = ClientBuilder.newBuilder()
+                .property("org.apache.cxf.jaxrs.client.cache.CacheControlFeature.storeByValue",
"true")
+                .register(feature).build().target(ADDRESS);
+            final Invocation.Builder cached =
+                setAsLocal(base.request("application/xml")).header(HttpHeaders.CACHE_CONTROL,
"public");
+            final Response r = cached.get();
+            assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
+            final Book b1 = r.readEntity(Book.class);
+            assertEquals("JCache", b1.getName());
+            assertNotNull(b1.getId());
+            waitABit();
+            assertEquals(b1, cached.get().readEntity(Book.class));
+        } finally {
+            feature.close();
+        }
+    }
 
     private static Invocation.Builder setAsLocal(final Invocation.Builder client) {
         WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH,
Boolean.TRUE);


Mime
View raw message