cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-5996] Ignoring null serializable entries and checking the cache control visibility constraint
Date Thu, 27 Nov 2014 11:14:01 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 5475624bc -> 1bfbb320f


[CXF-5996] Ignoring null serializable entries and checking the cache control visibility constraint


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1bfbb320
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1bfbb320
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1bfbb320

Branch: refs/heads/master
Commit: 1bfbb320fa15c29a410d0682033af4d54fa17c4a
Parents: 5475624
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Thu Nov 27 11:13:44 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu Nov 27 11:13:44 2014 +0000

----------------------------------------------------------------------
 .../CacheControlClientReaderInterceptor.java    | 48 +++++++++++++-------
 .../cache/CacheControlClientRequestFilter.java  | 13 ++++--
 2 files changed, 42 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/1bfbb320/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java
b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java
index 1cba11f..6f60397 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientReaderInterceptor.java
@@ -86,13 +86,13 @@ public class CacheControlClientReaderInterceptor implements ReaderInterceptor
{
             // non GET HTTP method or other restriction applies
             return context.proceed();
         }
-        final MultivaluedMap<String, String> headers = context.getHeaders(); 
-        final String cacheControlHeader = headers.getFirst(HttpHeaders.CACHE_CONTROL);
+        final MultivaluedMap<String, String> responseHeaders = context.getHeaders();

+        final String cacheControlHeader = responseHeaders.getFirst(HttpHeaders.CACHE_CONTROL);
         final CacheControl cacheControl = CacheControl.valueOf(cacheControlHeader.toString());
         
         byte[] cachedBytes = null;
-        if (cacheControl != null && !cacheControl.isNoCache() && !cacheControl.isNoStore()

-            && cacheResponseInputStream) {
+        final boolean validCacheControl = isCacheControlValid(context, cacheControl);
+        if (validCacheControl && cacheResponseInputStream) {
             // if Cache-Control is set and the stream needs to be cached then do it
             cachedBytes = IOUtils.readBytesFromStream((InputStream)context.getInputStream());
             context.setInputStream(new ByteArrayInputStream(cachedBytes));
@@ -100,15 +100,14 @@ public class CacheControlClientReaderInterceptor implements ReaderInterceptor
{
         // Read the stream and get the actual entity
         Object responseEntity = context.proceed();
         
-        if (cacheControl == null || cacheControl.isNoCache() || cacheControl.isNoStore())
{
-            // TODO: apparently no-cache also means the local cache has to be revalidated
+        if (!validCacheControl) {
             return responseEntity;
         }
         // if a max-age property is set then it overrides Expires
         long expiry = cacheControl.getMaxAge();
         if (expiry == -1) {
             //TODO: Review if Expires can be supported as an alternative to Cache-Control
-            String expiresHeader = headers.getFirst(HttpHeaders.EXPIRES);
+            String expiresHeader = responseHeaders.getFirst(HttpHeaders.EXPIRES);
             if (expiresHeader.startsWith("'") && expiresHeader.endsWith("'")) {
                 expiresHeader = expiresHeader.substring(1, expiresHeader.length() - 1);
             }
@@ -136,23 +135,24 @@ public class CacheControlClientReaderInterceptor implements ReaderInterceptor
{
             // the cached bytes will be returned immediately when a client cache will return
them
             ser = new BytesEntity((byte[])responseEntity, false);
         }
-
-        final Entry entry = new Entry(ser, context.getHeaders(), computeCacheHeaders(context.getHeaders()),
expiry);
-        final URI uri = uriInfo.getRequestUri();
-        final String accepts = (String)context.getProperty(CacheControlClientRequestFilter.CLIENT_ACCEPTS);
-        cache.put(new Key(uri, accepts), entry);
-
+        if (ser != null) { 
+            final Entry entry = 
+                new Entry(ser, responseHeaders, computeCacheHeaders(responseHeaders), expiry);
+            final URI uri = uriInfo.getRequestUri();
+            final String accepts = (String)context.getProperty(CacheControlClientRequestFilter.CLIENT_ACCEPTS);
+            cache.put(new Key(uri, accepts), entry);
+        }
         return responseEntity;
     }
 
-    private Map<String, String> computeCacheHeaders(final MultivaluedMap<String,
String> httpHeaders) {
+    private Map<String, String> computeCacheHeaders(final MultivaluedMap<String,
String> responseHeaders) {
         final Map<String, String> cacheHeaders = new HashMap<String, String>(2);
 
-        final String etagHeader = httpHeaders.getFirst(HttpHeaders.ETAG);
+        final String etagHeader = responseHeaders.getFirst(HttpHeaders.ETAG);
         if (etagHeader != null) {
             cacheHeaders.put(HttpHeaders.IF_NONE_MATCH, etagHeader);
         }
-        final String lastModifiedHeader = httpHeaders.getFirst(HttpHeaders.LAST_MODIFIED);
+        final String lastModifiedHeader = responseHeaders.getFirst(HttpHeaders.LAST_MODIFIED);
         if (lastModifiedHeader != null) {
             cacheHeaders.put(HttpHeaders.IF_MODIFIED_SINCE, lastModifiedHeader);
         }
@@ -176,4 +176,20 @@ public class CacheControlClientReaderInterceptor implements ReaderInterceptor
{
     public void setCacheResponseInputStream(boolean cacheInputStream) {
         this.cacheResponseInputStream = cacheInputStream;
     }
+    
+    protected boolean isCacheControlValid(final ReaderInterceptorContext context,
+                                          final CacheControl responseControl) {
+        
+        boolean valid =
+            responseControl != null && !responseControl.isNoCache() && !responseControl.isNoStore();
+        if (valid) {
+            String clientHeader = 
+                (String)context.getProperty(CacheControlClientRequestFilter.CLIENT_CACHE_CONTROL);
+            CacheControl clientControl = clientHeader == null ? null : CacheControl.valueOf(clientHeader);
+            if (clientControl != null && clientControl.isPrivate() != responseControl.isPrivate())
{
+                valid = false;
+            }
+        }
+        return valid;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/1bfbb320/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/cache/CacheControlClientRequestFilter.java
----------------------------------------------------------------------
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 0e46d17..e3f103d 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
@@ -34,9 +34,10 @@ import javax.ws.rs.core.Response;
 
 @Priority(Priorities.USER - 1)
 public class CacheControlClientRequestFilter implements ClientRequestFilter {
-    public static final String NO_CACHE_PROPERTY = "no_client_cache";
-    public static final String CACHED_ENTITY_PROPERTY = "client_cached_entity";
-    public static final String CLIENT_ACCEPTS = "client_accepts";
+    static final String NO_CACHE_PROPERTY = "no_client_cache";
+    static final String CACHED_ENTITY_PROPERTY = "client_cached_entity";
+    static final String CLIENT_ACCEPTS = "client_accepts";
+    static final String CLIENT_CACHE_CONTROL = "client_cache_control";
     private Cache<Key, Entry> cache;
 
     public CacheControlClientRequestFilter(final Cache<Key, Entry> cache) {
@@ -50,6 +51,8 @@ public class CacheControlClientRequestFilter implements ClientRequestFilter
{
     @Override
     public void filter(final ClientRequestContext request) throws IOException {
         if (!HttpMethod.GET.equals(request.getMethod())) {
+            //TODO: Review the possibility of supporting POST responses, example,
+            //      POST create request may get a created entity representation returned
             request.setProperty(NO_CACHE_PROPERTY, "true");
             return;
         }
@@ -58,6 +61,8 @@ public class CacheControlClientRequestFilter implements ClientRequestFilter
{
         final Key key = new Key(uri, accepts);
         Entry entry = cache.get(key);
         if (entry != null) {
+            //TODO: do the extra validation against the conditional headers
+            //      which may be contained in the current request
             if (entry.isOutDated()) {
                 cache.remove(key, entry);
             } else {
@@ -74,7 +79,9 @@ public class CacheControlClientRequestFilter implements ClientRequestFilter
{
                 request.abortWith(ok.build());
             }
         }
+        // Should the map of all request headers shared ?
         request.setProperty(CLIENT_ACCEPTS, accepts);
+        request.setProperty(CLIENT_CACHE_CONTROL, request.getHeaderString(HttpHeaders.CACHE_CONTROL));
     }
 
     public CacheControlClientRequestFilter setCache(final Cache<Key, Entry> c) {


Mime
View raw message