Replaced deprecated functionality

This commit is contained in:
Oleg Kalnichevski 2024-06-22 15:06:31 +02:00
parent 0b4e653eda
commit 10e8a7acbc
9 changed files with 87 additions and 41 deletions

View File

@ -97,6 +97,10 @@ public class TestAsyncClient extends CloseableHttpAsyncClient {
return client.execute(target, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
}
/**
* @deprecated Do not use.
*/
@Deprecated
@Override
public void register(final String hostname,
final String uriPattern,

View File

@ -108,9 +108,9 @@ public class TestServerBootstrap {
.build());
for (final HandlerEntry<HttpRequestHandler> entry: handlerList) {
if (entry.hostname != null) {
server.registerHandlerVirtual(entry.hostname, entry.uriPattern, entry.handler);
server.register(entry.hostname, entry.uriPattern, entry.handler);
} else {
server.registerHandler(entry.uriPattern, entry.handler);
server.register(entry.uriPattern, entry.handler);
}
}
return new TestServer(

View File

@ -70,6 +70,11 @@ abstract class AbstractHttpAsyncClientBase extends CloseableHttpAsyncClient {
}
}
/**
* @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter}
* at the construction time
*/
@Deprecated
@Override
public void register(final String hostname, final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
pushConsumerRegistry.register(hostname, uriPattern, supplier);

View File

@ -34,25 +34,25 @@ import java.util.concurrent.ConcurrentMap;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.protocol.UriPatternMatcher;
import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.util.Args;
@SuppressWarnings("deprecation")
class AsyncPushConsumerRegistry {
private final UriPatternMatcher<Supplier<AsyncPushConsumer>> primary;
private final ConcurrentMap<String, UriPatternMatcher<Supplier<AsyncPushConsumer>>> hostMap;
private final org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> primary;
private final ConcurrentMap<String, org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>>> hostMap;
public AsyncPushConsumerRegistry() {
this.primary = new UriPatternMatcher<>();
this.primary = new org.apache.hc.core5.http.protocol.UriPatternMatcher<>();
this.hostMap = new ConcurrentHashMap<>();
}
private UriPatternMatcher<Supplier<AsyncPushConsumer>> getPatternMatcher(final String hostname) {
private org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> getPatternMatcher(final String hostname) {
if (hostname == null) {
return primary;
}
final UriPatternMatcher<Supplier<AsyncPushConsumer>> hostMatcher = hostMap.get(hostname);
final org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> hostMatcher = hostMap.get(hostname);
if (hostMatcher != null) {
return hostMatcher;
}
@ -63,7 +63,7 @@ class AsyncPushConsumerRegistry {
Args.notNull(request, "Request");
final URIAuthority authority = request.getAuthority();
final String key = authority != null ? authority.getHostName().toLowerCase(Locale.ROOT) : null;
final UriPatternMatcher<Supplier<AsyncPushConsumer>> patternMatcher = getPatternMatcher(key);
final org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> patternMatcher = getPatternMatcher(key);
if (patternMatcher == null) {
return null;
}
@ -83,9 +83,9 @@ class AsyncPushConsumerRegistry {
primary.register(uriPattern, supplier);
} else {
final String key = hostname.toLowerCase(Locale.ROOT);
UriPatternMatcher<Supplier<AsyncPushConsumer>> matcher = hostMap.get(key);
org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> matcher = hostMap.get(key);
if (matcher == null) {
final UriPatternMatcher<Supplier<AsyncPushConsumer>> newMatcher = new UriPatternMatcher<>();
final org.apache.hc.core5.http.protocol.UriPatternMatcher<Supplier<AsyncPushConsumer>> newMatcher = new org.apache.hc.core5.http.protocol.UriPatternMatcher<>();
matcher = hostMap.putIfAbsent(key, newMatcher);
if (matcher == null) {
matcher = newMatcher;

View File

@ -129,8 +129,18 @@ public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Modal
return execute(request, null, callback);
}
/**
* @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter}
* at the construction time
*/
@Deprecated
public abstract void register(String hostname, String uriPattern, Supplier<AsyncPushConsumer> supplier);
/**
* @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter}
* at the construction time
*/
@Deprecated
public final void register(final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
register(null, uriPattern, supplier);
}

View File

@ -37,8 +37,12 @@ import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBu
import org.apache.hc.client5.http.nio.AsyncClientConnectionManager;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.config.CharCodingConfig;
import org.apache.hc.core5.http.config.Http1Config;
import org.apache.hc.core5.http.impl.routing.RequestRouter;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.HandlerFactory;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
import org.apache.hc.core5.http.protocol.HttpProcessor;
@ -333,4 +337,16 @@ public final class HttpAsyncClients {
return createHttp2Minimal(H2Config.DEFAULT);
}
/**
* Creates {@link HandlerFactory} backed by a push {@link RequestRouter}.
*
* @since 5.4
*/
public static HandlerFactory<AsyncPushConsumer> pushRouter(final RequestRouter<Supplier<AsyncPushConsumer>> requestRouter) {
return (request, context) -> {
final Supplier<AsyncPushConsumer> supplier = requestRouter.resolve(request, context);
return supplier != null ? supplier.get() : null;
};
}
}

View File

@ -101,7 +101,7 @@ public class BasicDomainHandler implements CommonCookieAttributeHandler {
}
static boolean domainMatch(final String domain, final String host) {
if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
if (InetAddressUtils.isIPv4(host) || InetAddressUtils.isIPv6(host)) {
return false;
}
final String normalizedDomain = domain.startsWith(".") ? domain.substring(1) : domain;

View File

@ -273,14 +273,14 @@ public final class DefaultHostnameVerifier implements HttpClientHostnameVerifier
}
static HostNameType determineHostFormat(final String host) {
if (InetAddressUtils.isIPv4Address(host)) {
if (InetAddressUtils.isIPv4(host)) {
return HostNameType.IPv4;
}
String s = host;
if (s.startsWith("[") && s.endsWith("]")) {
s = host.substring(1, host.length() - 1);
}
if (InetAddressUtils.isIPv6Address(s)) {
if (InetAddressUtils.isIPv6(s)) {
return HostNameType.IPv6;
}
return HostNameType.DNS;

View File

@ -37,12 +37,15 @@ import org.apache.hc.client5.http.config.TlsConfig;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.impl.routing.RequestRouter;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.hc.core5.http.nio.AsyncPushConsumer;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.http.support.BasicRequestBuilder;
import org.apache.hc.core5.http2.HttpVersionPolicy;
@ -69,39 +72,44 @@ public class AsyncClientH2ServerPush {
client.start();
client.register("*", () -> new AbstractBinPushConsumer() {
final RequestRouter<Supplier<AsyncPushConsumer>> pushRequestRouter = RequestRouter.<Supplier<AsyncPushConsumer>>builder()
// Route all requests to the local authority
.resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER)
// Use the same route for all requests
.addRoute(RequestRouter.LOCAL_AUTHORITY, "*", () -> new AbstractBinPushConsumer() {
@Override
protected void start(
final HttpRequest promise,
final HttpResponse response,
final ContentType contentType) throws HttpException, IOException {
System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
}
@Override
protected void start(
final HttpRequest promise,
final HttpResponse response,
final ContentType contentType) throws HttpException, IOException {
System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
}
@Override
protected int capacityIncrement() {
return Integer.MAX_VALUE;
}
@Override
protected int capacityIncrement() {
return Integer.MAX_VALUE;
}
@Override
protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
}
@Override
protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
}
@Override
protected void completed() {
}
@Override
protected void completed() {
}
@Override
public void failed(final Exception cause) {
System.out.println("(push)->" + cause);
}
@Override
public void failed(final Exception cause) {
System.out.println("(push)->" + cause);
}
@Override
public void releaseResources() {
}
@Override
public void releaseResources() {
}
});
})
.build();
final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build();
@ -140,7 +148,10 @@ public class AsyncClientH2ServerPush {
public void releaseResources() {
}
}, null);
},
HttpAsyncClients.pushRouter(pushRequestRouter),
null,
null);
future.get();
System.out.println("Shutting down");