hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [1/2] httpcomponents-client git commit: CacheInvalidator to use URI class instead of URL
Date Mon, 16 Oct 2017 21:43:19 GMT
Repository: httpcomponents-client
Updated Branches:
  refs/heads/master 935573104 -> f70c97424


CacheInvalidator to use URI class instead of URL


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/fc41f47a
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/fc41f47a
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/fc41f47a

Branch: refs/heads/master
Commit: fc41f47ad3e7bbe4b428706765c5ddf03f90b2c9
Parents: 9355731
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Mon Oct 16 23:24:19 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Mon Oct 16 23:39:11 2017 +0200

----------------------------------------------------------------------
 .../http/impl/cache/CacheInvalidator.java       | 133 +++++++++----------
 .../http/impl/cache/CacheKeyGenerator.java      |   9 +-
 .../http/impl/cache/TestCacheInvalidator.java   |   6 +-
 3 files changed, 67 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fc41f47a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheInvalidator.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheInvalidator.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheInvalidator.java
index 09638c9..3240fff 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheInvalidator.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheInvalidator.java
@@ -27,8 +27,8 @@
 package org.apache.hc.client5.http.impl.cache;
 
 import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Date;
 
 import org.apache.hc.client5.http.cache.HeaderConstants;
@@ -36,6 +36,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
 import org.apache.hc.client5.http.cache.HttpCacheInvalidator;
 import org.apache.hc.client5.http.cache.HttpCacheStorage;
 import org.apache.hc.client5.http.utils.DateUtils;
+import org.apache.hc.client5.http.utils.URIUtils;
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.http.Header;
@@ -74,6 +75,17 @@ class CacheInvalidator implements HttpCacheInvalidator {
         this.storage = storage;
     }
 
+    private static URI parse(final String uri) {
+        if (uri == null) {
+            return null;
+        }
+        try {
+            return new URI(uri);
+        } catch (final URISyntaxException ex) {
+            return null;
+        }
+    }
+
     /**
      * Remove cache entries from the cache that are no longer fresh or
      * have been invalidated in some way.
@@ -96,21 +108,23 @@ class CacheInvalidator implements HttpCacheInvalidator {
                 }
                 flushEntry(key);
             }
-            final URL reqURL = getAbsoluteURL(key);
-            if (reqURL == null) {
-                log.error("Couldn't transform request into valid URL");
+            final URI uri = parse(key);
+            if (uri == null) {
+                log.error("Couldn't transform request into valid URI");
                 return;
             }
             final Header clHdr = req.getFirstHeader("Content-Location");
             if (clHdr != null) {
-                final String contentLocation = clHdr.getValue();
-                if (!flushAbsoluteUriFromSameHost(reqURL, contentLocation)) {
-                    flushRelativeUriFromSameHost(reqURL, contentLocation);
+                final URI contentLocation = parse(clHdr.getValue());
+                if (contentLocation != null) {
+                    if (!flushAbsoluteUriFromSameHost(uri, contentLocation)) {
+                        flushRelativeUriFromSameHost(uri, contentLocation);
+                    }
                 }
             }
             final Header lHdr = req.getFirstHeader("Location");
             if (lHdr != null) {
-                flushAbsoluteUriFromSameHost(reqURL, lHdr.getValue());
+                flushAbsoluteUriFromSameHost(uri, parse(lHdr.getValue()));
             }
         }
     }
@@ -144,52 +158,31 @@ class CacheInvalidator implements HttpCacheInvalidator {
         return null;
     }
 
-    protected void flushUriIfSameHost(final URL requestURL, final URL targetURL) {
-        final URL canonicalTarget = getAbsoluteURL(cacheKeyGenerator.generateKey(targetURL));
+    protected void flushUriIfSameHost(final URI requestURI, final URI targetURI) {
+        final URI canonicalTarget = parse(cacheKeyGenerator.generateKey(targetURI));
         if (canonicalTarget == null) {
             return;
         }
-        if (canonicalTarget.getAuthority().equalsIgnoreCase(requestURL.getAuthority())) {
+        if (canonicalTarget.getAuthority().equalsIgnoreCase(requestURI.getAuthority())) {
             flushEntry(canonicalTarget.toString());
         }
     }
 
-    protected void flushRelativeUriFromSameHost(final URL reqURL, final String relUri) {
-        final URL relURL = getRelativeURL(reqURL, relUri);
-        if (relURL == null) {
-            return;
+    protected void flushRelativeUriFromSameHost(final URI requestUri, final URI uri) {
+        final URI resolvedUri = uri != null ? URIUtils.resolve(requestUri, uri) : null;
+        if (resolvedUri != null) {
+            flushUriIfSameHost(requestUri, resolvedUri);
         }
-        flushUriIfSameHost(reqURL, relURL);
     }
 
 
-    protected boolean flushAbsoluteUriFromSameHost(final URL reqURL, final String uri) {
-        final URL absURL = getAbsoluteURL(uri);
-        if (absURL == null) {
+    protected boolean flushAbsoluteUriFromSameHost(final URI requestUri, final URI uri) {
+        if (uri != null && uri.isAbsolute()) {
+            flushUriIfSameHost(requestUri, uri);
+            return true;
+        } else {
             return false;
         }
-        flushUriIfSameHost(reqURL,absURL);
-        return true;
-    }
-
-    private URL getAbsoluteURL(final String uri) {
-        URL absURL = null;
-        try {
-            absURL = new URL(uri);
-        } catch (final MalformedURLException mue) {
-            // nop
-        }
-        return absURL;
-    }
-
-    private URL getRelativeURL(final URL reqURL, final String relUri) {
-        URL relURL = null;
-        try {
-            relURL = new URL(reqURL,relUri);
-        } catch (final MalformedURLException e) {
-            // nop
-        }
-        return relURL;
     }
 
     protected boolean requestShouldNotBeCached(final HttpRequest req) {
@@ -206,28 +199,26 @@ class CacheInvalidator implements HttpCacheInvalidator {
      * received for the given host/request pair.
      */
     @Override
-    public void flushInvalidatedCacheEntries(final HttpHost host,
-            final HttpRequest request, final HttpResponse response) {
+    public void flushInvalidatedCacheEntries(final HttpHost host, final HttpRequest request,
final HttpResponse response) {
         final int status = response.getCode();
         if (status < 200 || status > 299) {
             return;
         }
-        final URL reqURL = getAbsoluteURL(cacheKeyGenerator.generateKey(host, request));
-        if (reqURL == null) {
+        final URI uri = parse(cacheKeyGenerator.generateKey(host, request));
+        if (uri == null) {
             return;
         }
-        final URL contentLocation = getContentLocationURL(reqURL, response);
+        final URI contentLocation = getContentLocationURI(uri, response);
         if (contentLocation != null) {
-            flushLocationCacheEntry(reqURL, response, contentLocation);
+            flushLocationCacheEntry(uri, response, contentLocation);
         }
-        final URL location = getLocationURL(reqURL, response);
+        final URI location = getLocationURI(uri, response);
         if (location != null) {
-            flushLocationCacheEntry(reqURL, response, location);
+            flushLocationCacheEntry(uri, response, location);
         }
     }
 
-    private void flushLocationCacheEntry(final URL reqURL,
-            final HttpResponse response, final URL location) {
+    private void flushLocationCacheEntry(final URI requestUri, final HttpResponse response,
final URI location) {
         final String cacheKey = cacheKeyGenerator.generateKey(location);
         final HttpCacheEntry entry = getEntry(cacheKey);
         if (entry == null) {
@@ -244,33 +235,31 @@ class CacheInvalidator implements HttpCacheInvalidator {
             return;
         }
 
-        flushUriIfSameHost(reqURL, location);
+        flushUriIfSameHost(requestUri, location);
     }
 
-    private URL getContentLocationURL(final URL reqURL, final HttpResponse response) {
-        final Header clHeader = response.getFirstHeader("Content-Location");
-        if (clHeader == null) {
+    private static URI getLocationURI(final URI requestUri, final HttpResponse response,
final String headerName) {
+        final Header h = response.getFirstHeader(headerName);
+        if (h == null) {
             return null;
         }
-        final String contentLocation = clHeader.getValue();
-        final URL canonURL = getAbsoluteURL(contentLocation);
-        if (canonURL != null) {
-            return canonURL;
+        final URI locationUri = parse(h.getValue());
+        if (locationUri == null) {
+            return requestUri;
+        }
+        if (locationUri.isAbsolute()) {
+            return locationUri;
+        } else {
+            return URIUtils.resolve(requestUri, locationUri);
         }
-        return getRelativeURL(reqURL, contentLocation);
     }
 
-    private URL getLocationURL(final URL reqURL, final HttpResponse response) {
-        final Header clHeader = response.getFirstHeader("Location");
-        if (clHeader == null) {
-            return null;
-        }
-        final String location = clHeader.getValue();
-        final URL canonURL = getAbsoluteURL(location);
-        if (canonURL != null) {
-            return canonURL;
-        }
-        return getRelativeURL(reqURL, location);
+    private URI getContentLocationURI(final URI requestUri, final HttpResponse response)
{
+        return getLocationURI(requestUri, response, HttpHeaders.CONTENT_LOCATION);
+    }
+
+    private URI getLocationURI(final URI requestUri, final HttpResponse response) {
+        return getLocationURI(requestUri, response, HttpHeaders.LOCATION);
     }
 
     private boolean responseAndEntryEtagsDiffer(final HttpResponse response,

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fc41f47a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
index 082322e..7706340 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheKeyGenerator.java
@@ -29,7 +29,6 @@ package org.apache.hc.client5.http.impl.cache;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -118,14 +117,14 @@ class CacheKeyGenerator {
         }
     }
 
-    public String generateKey(final URL url) {
-        if (url == null) {
+    public String generateKey(final URI uri) {
+        if (uri == null) {
             return null;
         }
         try {
-            return normalize(url.toURI()).toASCIIString();
+            return normalize(uri).toASCIIString();
         } catch (final URISyntaxException ex) {
-            return url.toString();
+            return uri.toString();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/fc41f47a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheInvalidator.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheInvalidator.java
b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheInvalidator.java
index 8cfefed..1f55518 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheInvalidator.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheInvalidator.java
@@ -32,7 +32,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
 import java.io.IOException;
-import java.net.URL;
+import java.net.URI;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -55,8 +55,6 @@ import org.junit.Test;
 
 public class TestCacheInvalidator {
 
-    private static final ProtocolVersion HTTP_1_1 = new ProtocolVersion("HTTP", 1, 1);
-
     private CacheInvalidator impl;
     private HttpCacheStorage mockStorage;
     private HttpHost host;
@@ -141,7 +139,7 @@ public class TestCacheInvalidator {
         verify(mockEntry).getVariantMap();
         verify(mockStorage).getEntry(theUri);
         verify(mockStorage).removeEntry(theUri);
-        verify(mockStorage).removeEntry(cacheKeyGenerator.generateKey(new URL(contentLocation)));
+        verify(mockStorage).removeEntry(cacheKeyGenerator.generateKey(new URI(contentLocation)));
     }
 
     @Test


Mime
View raw message