Removed HttpCacheSupport from public API
This commit is contained in:
parent
fbed77880b
commit
38b8398a20
|
@ -68,7 +68,7 @@ class CacheInvalidatorBase {
|
|||
if (h == null) {
|
||||
return null;
|
||||
}
|
||||
final URI locationUri = HttpCacheSupport.normalizeQuietly(h.getValue());
|
||||
final URI locationUri = CacheSupport.normalize(h.getValue());
|
||||
if (locationUri == null) {
|
||||
return requestUri;
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ public class CacheKeyGenerator implements Resolver<URI, String> {
|
|||
*/
|
||||
public String generateKey(final URI requestUri) {
|
||||
try {
|
||||
final URI normalizeRequestUri = HttpCacheSupport.normalize(requestUri);
|
||||
final URI normalizeRequestUri = CacheSupport.normalize(requestUri);
|
||||
return normalizeRequestUri.toASCIIString();
|
||||
} catch (final URISyntaxException ex) {
|
||||
return requestUri.toASCIIString();
|
||||
|
@ -86,7 +86,7 @@ public class CacheKeyGenerator implements Resolver<URI, String> {
|
|||
* @return cache key
|
||||
*/
|
||||
public String generateKey(final HttpHost host, final HttpRequest request) {
|
||||
final String s = HttpCacheSupport.getRequestUri(request, host);
|
||||
final String s = CacheSupport.getRequestUri(request, host);
|
||||
try {
|
||||
return generateKey(new URI(s));
|
||||
} catch (final URISyntaxException ex) {
|
||||
|
|
133
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java
vendored
Normal file
133
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheSupport.java
vendored
Normal file
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* ====================================================================
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.hc.client5.http.impl.cache;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.apache.hc.client5.http.utils.URIUtils;
|
||||
import org.apache.hc.core5.annotation.Internal;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
import org.apache.hc.core5.http.URIScheme;
|
||||
import org.apache.hc.core5.net.URIAuthority;
|
||||
import org.apache.hc.core5.net.URIBuilder;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
|
||||
/**
|
||||
* HTTP cache support utilities.
|
||||
*
|
||||
* @since 5.3
|
||||
*/
|
||||
@Internal
|
||||
public final class CacheSupport {
|
||||
|
||||
private static final URI BASE_URI = URI.create("http://example.com/");
|
||||
|
||||
/**
|
||||
* Returns text representation of the request URI of the given {@link HttpRequest}.
|
||||
* This method will use {@link HttpRequest#getPath()}, {@link HttpRequest#getScheme()} and
|
||||
* {@link HttpRequest#getAuthority()} values when available or attributes of target
|
||||
* {@link HttpHost } in order to construct an absolute URI.
|
||||
* <p>
|
||||
* This method will not attempt to ensure validity of the resultant text representation.
|
||||
*
|
||||
* @param request the {@link HttpRequest}
|
||||
* @param target target host
|
||||
*
|
||||
* @return String the request URI
|
||||
*/
|
||||
public static String getRequestUri(final HttpRequest request, final HttpHost target) {
|
||||
Args.notNull(request, "HTTP request");
|
||||
Args.notNull(target, "Target");
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
final URIAuthority authority = request.getAuthority();
|
||||
if (authority != null) {
|
||||
final String scheme = request.getScheme();
|
||||
buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
|
||||
buf.append(authority.getHostName());
|
||||
if (authority.getPort() >= 0) {
|
||||
buf.append(":").append(authority.getPort());
|
||||
}
|
||||
} else {
|
||||
buf.append(target.getSchemeName()).append("://");
|
||||
buf.append(target.getHostName());
|
||||
if (target.getPort() >= 0) {
|
||||
buf.append(":").append(target.getPort());
|
||||
}
|
||||
}
|
||||
final String path = request.getPath();
|
||||
if (path == null) {
|
||||
buf.append("/");
|
||||
} else {
|
||||
if (buf.length() > 0 && !path.startsWith("/")) {
|
||||
buf.append("/");
|
||||
}
|
||||
buf.append(path);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns normalized representation of the request URI optimized for use as a cache key.
|
||||
* This method ensures the resultant URI has an explicit port in the authority component,
|
||||
* and explicit path component and no fragment.
|
||||
*/
|
||||
public static URI normalize(final URI requestUri) throws URISyntaxException {
|
||||
Args.notNull(requestUri, "URI");
|
||||
final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ;
|
||||
if (builder.getHost() != null) {
|
||||
if (builder.getScheme() == null) {
|
||||
builder.setScheme(URIScheme.HTTP.id);
|
||||
}
|
||||
if (builder.getPort() <= -1) {
|
||||
if (URIScheme.HTTP.same(builder.getScheme())) {
|
||||
builder.setPort(80);
|
||||
} else if (URIScheme.HTTPS.same(builder.getScheme())) {
|
||||
builder.setPort(443);
|
||||
}
|
||||
}
|
||||
}
|
||||
builder.setFragment(null);
|
||||
return builder.normalizeSyntax().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lenient URI parser that normalizes valid {@link URI}s and returns {@code null} for malformed URIs.
|
||||
*/
|
||||
public static URI normalize(final String requestUri) {
|
||||
if (requestUri == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return normalize(new URI(requestUri));
|
||||
} catch (final URISyntaxException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -100,8 +100,8 @@ public class DefaultAsyncCacheInvalidator extends CacheInvalidatorBase implement
|
|||
final Resolver<URI, String> cacheKeyResolver,
|
||||
final HttpAsyncCacheStorage storage,
|
||||
final FutureCallback<Boolean> callback) {
|
||||
final String s = HttpCacheSupport.getRequestUri(request, host);
|
||||
final URI uri = HttpCacheSupport.normalizeQuietly(s);
|
||||
final String s = CacheSupport.getRequestUri(request, host);
|
||||
final URI uri = CacheSupport.normalize(s);
|
||||
final String cacheKey = uri != null ? cacheKeyResolver.resolve(uri) : s;
|
||||
return storage.getEntry(cacheKey, new FutureCallback<HttpCacheEntry>() {
|
||||
|
||||
|
@ -123,7 +123,7 @@ public class DefaultAsyncCacheInvalidator extends CacheInvalidatorBase implement
|
|||
}
|
||||
final Header clHdr = request.getFirstHeader(HttpHeaders.CONTENT_LOCATION);
|
||||
if (clHdr != null) {
|
||||
final URI contentLocation = HttpCacheSupport.normalizeQuietly(clHdr.getValue());
|
||||
final URI contentLocation = CacheSupport.normalize(clHdr.getValue());
|
||||
if (contentLocation != null) {
|
||||
if (!flushAbsoluteUriFromSameHost(uri, contentLocation, cacheKeyResolver, storage)) {
|
||||
flushRelativeUriFromSameHost(uri, contentLocation, cacheKeyResolver, storage);
|
||||
|
@ -132,7 +132,7 @@ public class DefaultAsyncCacheInvalidator extends CacheInvalidatorBase implement
|
|||
}
|
||||
final Header lHdr = request.getFirstHeader(HttpHeaders.LOCATION);
|
||||
if (lHdr != null) {
|
||||
final URI location = HttpCacheSupport.normalizeQuietly(lHdr.getValue());
|
||||
final URI location = CacheSupport.normalize(lHdr.getValue());
|
||||
if (location != null) {
|
||||
flushAbsoluteUriFromSameHost(uri, location, cacheKeyResolver, storage);
|
||||
}
|
||||
|
@ -189,8 +189,8 @@ public class DefaultAsyncCacheInvalidator extends CacheInvalidatorBase implement
|
|||
final FutureCallback<Boolean> callback) {
|
||||
final int status = response.getCode();
|
||||
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
|
||||
final String s = HttpCacheSupport.getRequestUri(request, host);
|
||||
final URI requestUri = HttpCacheSupport.normalizeQuietly(s);
|
||||
final String s = CacheSupport.getRequestUri(request, host);
|
||||
final URI requestUri = CacheSupport.normalize(s);
|
||||
if (requestUri != null) {
|
||||
final List<String> cacheKeys = new ArrayList<>(2);
|
||||
final URI contentLocation = getContentLocationURI(requestUri, response);
|
||||
|
|
|
@ -86,8 +86,8 @@ public class DefaultCacheInvalidator extends CacheInvalidatorBase implements Htt
|
|||
final HttpRequest request,
|
||||
final Resolver<URI, String> cacheKeyResolver,
|
||||
final HttpCacheStorage storage) {
|
||||
final String s = HttpCacheSupport.getRequestUri(request, host);
|
||||
final URI uri = HttpCacheSupport.normalizeQuietly(s);
|
||||
final String s = CacheSupport.getRequestUri(request, host);
|
||||
final URI uri = CacheSupport.normalize(s);
|
||||
final String cacheKey = uri != null ? cacheKeyResolver.resolve(uri) : s;
|
||||
final HttpCacheEntry parent = getEntry(storage, cacheKey);
|
||||
|
||||
|
@ -107,7 +107,7 @@ public class DefaultCacheInvalidator extends CacheInvalidatorBase implements Htt
|
|||
}
|
||||
final Header clHdr = request.getFirstHeader(HttpHeaders.CONTENT_LOCATION);
|
||||
if (clHdr != null) {
|
||||
final URI contentLocation = HttpCacheSupport.normalizeQuietly(clHdr.getValue());
|
||||
final URI contentLocation = CacheSupport.normalize(clHdr.getValue());
|
||||
if (contentLocation != null) {
|
||||
if (!flushAbsoluteUriFromSameHost(uri, contentLocation, cacheKeyResolver, storage)) {
|
||||
flushRelativeUriFromSameHost(uri, contentLocation, cacheKeyResolver, storage);
|
||||
|
@ -116,7 +116,7 @@ public class DefaultCacheInvalidator extends CacheInvalidatorBase implements Htt
|
|||
}
|
||||
final Header lHdr = request.getFirstHeader(HttpHeaders.LOCATION);
|
||||
if (lHdr != null) {
|
||||
final URI location = HttpCacheSupport.normalizeQuietly(lHdr.getValue());
|
||||
final URI location = CacheSupport.normalize(lHdr.getValue());
|
||||
if (location != null) {
|
||||
flushAbsoluteUriFromSameHost(uri, location, cacheKeyResolver, storage);
|
||||
}
|
||||
|
@ -159,8 +159,8 @@ public class DefaultCacheInvalidator extends CacheInvalidatorBase implements Htt
|
|||
if (status < 200 || status > 299) {
|
||||
return;
|
||||
}
|
||||
final String s = HttpCacheSupport.getRequestUri(request, host);
|
||||
final URI uri = HttpCacheSupport.normalizeQuietly(s);
|
||||
final String s = CacheSupport.getRequestUri(request, host);
|
||||
final URI uri = CacheSupport.normalize(s);
|
||||
if (uri == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -29,93 +29,27 @@ package org.apache.hc.client5.http.impl.cache;
|
|||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.apache.hc.client5.http.utils.URIUtils;
|
||||
import org.apache.hc.core5.http.HttpHost;
|
||||
import org.apache.hc.core5.http.HttpRequest;
|
||||
import org.apache.hc.core5.http.URIScheme;
|
||||
import org.apache.hc.core5.net.URIAuthority;
|
||||
import org.apache.hc.core5.net.URIBuilder;
|
||||
import org.apache.hc.core5.util.Args;
|
||||
|
||||
/**
|
||||
* HTTP cache support utilities.
|
||||
*
|
||||
* @since 5.0
|
||||
*
|
||||
* @deprecated Do not use. This functionality is internal.
|
||||
*/
|
||||
@Deprecated
|
||||
public final class HttpCacheSupport {
|
||||
|
||||
private static final URI BASE_URI = URI.create("http://example.com/");
|
||||
|
||||
/**
|
||||
* Returns text representation of the request URI of the given {@link HttpRequest}.
|
||||
* This method will use {@link HttpRequest#getPath()}, {@link HttpRequest#getScheme()} and
|
||||
* {@link HttpRequest#getAuthority()} values when available or attributes of target
|
||||
* {@link HttpHost } in order to construct an absolute URI.
|
||||
* <p>
|
||||
* This method will not attempt to ensure validity of the resultant text representation.
|
||||
*
|
||||
* @param request the {@link HttpRequest}
|
||||
* @param target target host
|
||||
*
|
||||
* @return String the request URI
|
||||
*/
|
||||
public static String getRequestUri(final HttpRequest request, final HttpHost target) {
|
||||
Args.notNull(request, "HTTP request");
|
||||
Args.notNull(target, "Target");
|
||||
final StringBuilder buf = new StringBuilder();
|
||||
final URIAuthority authority = request.getAuthority();
|
||||
if (authority != null) {
|
||||
final String scheme = request.getScheme();
|
||||
buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
|
||||
buf.append(authority.getHostName());
|
||||
if (authority.getPort() >= 0) {
|
||||
buf.append(":").append(authority.getPort());
|
||||
}
|
||||
} else {
|
||||
buf.append(target.getSchemeName()).append("://");
|
||||
buf.append(target.getHostName());
|
||||
if (target.getPort() >= 0) {
|
||||
buf.append(":").append(target.getPort());
|
||||
}
|
||||
}
|
||||
final String path = request.getPath();
|
||||
if (path == null) {
|
||||
buf.append("/");
|
||||
} else {
|
||||
if (buf.length() > 0 && !path.startsWith("/")) {
|
||||
buf.append("/");
|
||||
}
|
||||
buf.append(path);
|
||||
}
|
||||
return buf.toString();
|
||||
return CacheSupport.getRequestUri(request, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns normalized representation of the request URI optimized for use as a cache key.
|
||||
* This method ensures the resultant URI has an explicit port in the authority component,
|
||||
* and explicit path component and no fragment.
|
||||
*
|
||||
* @param requestUri original request URI
|
||||
* @return normalized URI.
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
public static URI normalize(final URI requestUri) throws URISyntaxException {
|
||||
Args.notNull(requestUri, "URI");
|
||||
final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ;
|
||||
if (builder.getHost() != null) {
|
||||
if (builder.getScheme() == null) {
|
||||
builder.setScheme(URIScheme.HTTP.id);
|
||||
}
|
||||
if (builder.getPort() <= -1) {
|
||||
if (URIScheme.HTTP.same(builder.getScheme())) {
|
||||
builder.setPort(80);
|
||||
} else if (URIScheme.HTTPS.same(builder.getScheme())) {
|
||||
builder.setPort(443);
|
||||
}
|
||||
}
|
||||
}
|
||||
builder.setFragment(null);
|
||||
return builder.normalizeSyntax().build();
|
||||
return CacheSupport.normalize(requestUri);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,14 +73,7 @@ public final class HttpCacheSupport {
|
|||
* @since 5.2
|
||||
*/
|
||||
public static URI normalizeQuietly(final String requestUri) {
|
||||
if (requestUri == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return normalize(new URI(requestUri));
|
||||
} catch (final URISyntaxException ex) {
|
||||
return null;
|
||||
}
|
||||
return CacheSupport.normalize(requestUri);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ public class TestDefaultAsyncCacheInvalidator {
|
|||
|
||||
when(cacheKeyResolver.resolve(ArgumentMatchers.any())).thenAnswer((Answer<String>) invocation -> {
|
||||
final URI uri = invocation.getArgument(0);
|
||||
return HttpCacheSupport.normalize(uri).toASCIIString();
|
||||
return CacheSupport.normalize(uri).toASCIIString();
|
||||
});
|
||||
|
||||
host = new HttpHost("foo.example.com");
|
||||
|
|
|
@ -77,7 +77,7 @@ public class TestDefaultCacheInvalidator {
|
|||
|
||||
when(cacheKeyResolver.resolve(ArgumentMatchers.any())).thenAnswer((Answer<String>) invocation -> {
|
||||
final URI uri = invocation.getArgument(0);
|
||||
return HttpCacheSupport.normalize(uri).toASCIIString();
|
||||
return CacheSupport.normalize(uri).toASCIIString();
|
||||
});
|
||||
|
||||
host = new HttpHost("foo.example.com");
|
||||
|
|
Loading…
Reference in New Issue