From dee32207e33106d6dca25adaf15a9e46ece370f3 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 12 Jan 2018 13:02:42 +0100 Subject: [PATCH] Upgraded HttpCore dependency to version 5.0-beta2 --- .../impl/cache/HttpCacheEntryMatcher.java | 16 +- httpclient5-testing/pom.xml | 6 - .../AbstractHttpAsyncFundamentalsTest.java | 10 + .../client5/testing/async/TestHttp1Async.java | 10 - .../sync/TestClientRequestExecution.java | 4 +- .../AbstractMinimalHttpAsyncClientBase.java | 20 +- .../Http2AsyncClientEventHandlerFactory.java | 5 +- .../InternalAbstractHttpAsyncClient.java | 10 +- ...InternalHttp2ClientProtocolNegotiator.java | 230 ------------------ pom.xml | 8 +- 10 files changed, 31 insertions(+), 288 deletions(-) delete mode 100644 httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2ClientProtocolNegotiator.java diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpCacheEntryMatcher.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpCacheEntryMatcher.java index c0db981dd..9c4a52c52 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpCacheEntryMatcher.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpCacheEntryMatcher.java @@ -28,12 +28,12 @@ package org.apache.hc.client5.http.impl.cache; import java.util.Arrays; import java.util.Date; -import java.util.Objects; import org.apache.hc.client5.http.cache.HttpCacheEntry; import org.apache.hc.client5.http.cache.Resource; import org.apache.hc.client5.http.cache.ResourceIOException; import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.util.LangUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Factory; @@ -60,18 +60,26 @@ public class HttpCacheEntryMatcher extends BaseMatcher { } final Date expectedRequestDate = expectedValue.getRequestDate(); final Date otherRequestDate = otherValue.getRequestDate(); - if (!Objects.equals(expectedRequestDate, otherRequestDate)) { + if (!LangUtils.equals(expectedRequestDate, otherRequestDate)) { return false; } final Date expectedResponseDate = expectedValue.getResponseDate(); final Date otherResponseDate = otherValue.getResponseDate(); - if (!Objects.equals(expectedResponseDate, otherResponseDate)) { + if (!LangUtils.equals(expectedResponseDate, otherResponseDate)) { return false; } final Header[] expectedHeaders = expectedValue.getAllHeaders(); final Header[] otherHeaders = otherValue.getAllHeaders(); - if (!Arrays.deepEquals(expectedHeaders, otherHeaders)) { + if (expectedHeaders.length != otherHeaders.length) { return false; + } else { + for (int i = 0; i < expectedHeaders.length; i++) { + final Header h1 = expectedHeaders[i]; + final Header h2 = otherHeaders[i]; + if (!h1.getName().equals(h2.getName()) || !LangUtils.equals(h1.getValue(), h2.getValue())) { + return false; + } + } } final Resource expectedResource = expectedValue.getResource(); final byte[] expectedContent = expectedResource != null ? expectedResource.get() : null; diff --git a/httpclient5-testing/pom.xml b/httpclient5-testing/pom.xml index 35212ec6b..ad037f1c1 100644 --- a/httpclient5-testing/pom.xml +++ b/httpclient5-testing/pom.xml @@ -59,12 +59,6 @@ compile true - - - org.apache.logging.log4j - log4j-api - compile - org.apache.logging.log4j log4j-core diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncFundamentalsTest.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncFundamentalsTest.java index 6f36fd8c5..71eb25cc2 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncFundamentalsTest.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/AbstractHttpAsyncFundamentalsTest.java @@ -202,4 +202,14 @@ public abstract class AbstractHttpAsyncFundamentalsTest future = httpclient.execute( + SimpleHttpRequest.get(target, "/random/boom"), null); + final SimpleHttpResponse response = future.get(); + Assert.assertThat(response, CoreMatchers.notNullValue()); + Assert.assertThat(response.getCode(), CoreMatchers.equalTo(400)); + } + } \ No newline at end of file diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java index 54172927b..4bab918c4 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/async/TestHttp1Async.java @@ -184,14 +184,4 @@ public class TestHttp1Async extends AbstractHttpAsyncFundamentalsTest future = httpclient.execute( - SimpleHttpRequest.get(target, "/random/boom"), null); - final SimpleHttpResponse response = future.get(); - Assert.assertThat(response, CoreMatchers.notNullValue()); - Assert.assertThat(response.getCode(), CoreMatchers.equalTo(400)); - } - } \ No newline at end of file diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientRequestExecution.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientRequestExecution.java index d0dd55de1..a2a4fa8ae 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientRequestExecution.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientRequestExecution.java @@ -200,7 +200,7 @@ public class TestClientRequestExecution extends LocalServerTestBase { final HttpHost target = start(); final HttpClientContext context = HttpClientContext.create(); - final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "blah.:.blah.:."); + final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "{{|boom|}}"); final ClassicHttpResponse response = this.httpclient.execute(target, request, context); EntityUtils.consume(response.getEntity()); @@ -208,7 +208,7 @@ public class TestClientRequestExecution extends LocalServerTestBase { final HttpRequest reqWrapper = context.getRequest(); - Assert.assertEquals("blah.:.blah.:.", reqWrapper.getRequestUri()); + Assert.assertEquals("{{|boom|}}", reqWrapper.getRequestUri()); } @Test diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java index ceaf09f61..bf9fa88c8 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AbstractMinimalHttpAsyncClientBase.java @@ -58,25 +58,7 @@ abstract class AbstractMinimalHttpAsyncClientBase extends AbstractHttpAsyncClien execute(new BasicClientExchangeHandler<>( requestProducer, responseConsumer, - //TODO: eliminate this callback after upgrade to HttpCore 5.0b2 - new FutureCallback() { - - @Override - public void completed(final T result) { - future.completed(result); - } - - @Override - public void failed(final Exception ex) { - future.failed(ex); - } - - @Override - public void cancelled() { - future.cancel(); - } - - }), + callback), context, future, new Supplier() { @Override diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/Http2AsyncClientEventHandlerFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/Http2AsyncClientEventHandlerFactory.java index 9c5cd87d2..3bbc9c862 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/Http2AsyncClientEventHandlerFactory.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/Http2AsyncClientEventHandlerFactory.java @@ -43,6 +43,7 @@ import org.apache.hc.core5.http2.config.H2Config; import org.apache.hc.core5.http2.frame.FramePrinter; import org.apache.hc.core5.http2.frame.RawFrame; import org.apache.hc.core5.http2.impl.nio.ClientHttp2StreamMultiplexerFactory; +import org.apache.hc.core5.http2.impl.nio.Http2OnlyClientProtocolNegotiator; import org.apache.hc.core5.http2.impl.nio.Http2StreamListener; import org.apache.hc.core5.reactor.IOEventHandler; import org.apache.hc.core5.reactor.IOEventHandlerFactory; @@ -178,7 +179,7 @@ class Http2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { }); final LoggingIOSession loggingIOSession = new LoggingIOSession(ioSession, id, sessionLog, wireLog); - return new InternalHttp2ClientProtocolNegotiator(loggingIOSession, http2StreamHandlerFactory); + return new Http2OnlyClientProtocolNegotiator(loggingIOSession, http2StreamHandlerFactory, false); } else { final ClientHttp2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientHttp2StreamMultiplexerFactory( httpProcessor, @@ -186,7 +187,7 @@ class Http2AsyncClientEventHandlerFactory implements IOEventHandlerFactory { h2Config, charCodingConfig, null); - return new InternalHttp2ClientProtocolNegotiator(ioSession, http2StreamHandlerFactory); + return new Http2OnlyClientProtocolNegotiator(ioSession, http2StreamHandlerFactory, false); } } diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java index 7b6e279cc..916aec095 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalAbstractHttpAsyncClient.java @@ -33,13 +33,11 @@ import java.util.Set; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import org.apache.hc.client5.http.HttpRoute; import org.apache.hc.client5.http.async.AsyncExecCallback; import org.apache.hc.client5.http.async.AsyncExecChain; import org.apache.hc.client5.http.async.AsyncExecRuntime; -import org.apache.hc.client5.http.async.methods.SimpleRequestProducer; import org.apache.hc.client5.http.auth.AuthSchemeProvider; import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.config.Configurable; @@ -168,7 +166,6 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa final AsyncExecChain.Scope scope = new AsyncExecChain.Scope(exchangeId, route, request, future, clientContext, execRuntime); - final AtomicReference resultRef = new AtomicReference<>(null); final AtomicBoolean outputTerminated = new AtomicBoolean(false); execChain.execute( RequestCopier.INSTANCE.copy(request), @@ -186,8 +183,7 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa @Override public boolean isRepeatable() { - //TODO: use AsyncRequestProducer#isRepeatable once available - return requestProducer instanceof SimpleRequestProducer; + return requestProducer.isRepeatable(); } @Override @@ -242,12 +238,11 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa requestProducer.releaseResources(); } responseConsumer.consumeResponse(response, entityDetails, - //TODO: eliminate this callback after upgrade to HttpCore 5.0b2 new FutureCallback() { @Override public void completed(final T result) { - resultRef.set(result); + future.completed(result); } @Override @@ -271,7 +266,6 @@ abstract class InternalAbstractHttpAsyncClient extends AbstractHttpAsyncClientBa } try { execRuntime.releaseConnection(); - future.completed(resultRef.getAndSet(null)); } finally { responseConsumer.releaseResources(); requestProducer.releaseResources(); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2ClientProtocolNegotiator.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2ClientProtocolNegotiator.java deleted file mode 100644 index b7f6b2ecf..000000000 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/InternalHttp2ClientProtocolNegotiator.java +++ /dev/null @@ -1,230 +0,0 @@ -/* - * ==================================================================== - * 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 - * . - * - */ - -package org.apache.hc.client5.http.impl.async; - -import java.io.IOException; -import java.net.SocketAddress; -import java.net.SocketTimeoutException; -import java.nio.ByteBuffer; -import java.nio.channels.ByteChannel; - -import javax.net.ssl.SSLSession; - -import org.apache.hc.core5.http.ConnectionClosedException; -import org.apache.hc.core5.http.EndpointDetails; -import org.apache.hc.core5.http.HttpException; -import org.apache.hc.core5.http.ProtocolVersion; -import org.apache.hc.core5.http.impl.nio.HttpConnectionEventHandler; -import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler; -import org.apache.hc.core5.http.nio.command.ExecutionCommand; -import org.apache.hc.core5.http2.impl.nio.ClientHttp2IOEventHandler; -import org.apache.hc.core5.http2.impl.nio.ClientHttp2StreamMultiplexer; -import org.apache.hc.core5.http2.impl.nio.ClientHttp2StreamMultiplexerFactory; -import org.apache.hc.core5.http2.ssl.ApplicationProtocols; -import org.apache.hc.core5.io.ShutdownType; -import org.apache.hc.core5.reactor.Command; -import org.apache.hc.core5.reactor.IOEventHandler; -import org.apache.hc.core5.reactor.IOSession; -import org.apache.hc.core5.reactor.TlsCapableIOSession; -import org.apache.hc.core5.reactor.ssl.TlsDetails; -import org.apache.hc.core5.util.TextUtils; - -/** - * TODO: replace with Http2OnlyClientProtocolNegotiator after HttpCore 5.0b2 - */ -final class InternalHttp2ClientProtocolNegotiator implements HttpConnectionEventHandler { - - // PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n - final static byte[] PREFACE = new byte[] { - 0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, 0x54, 0x50, - 0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x53, 0x4d, - 0x0d, 0x0a, 0x0d, 0x0a}; - - private final TlsCapableIOSession ioSession; - private final ClientHttp2StreamMultiplexerFactory http2StreamHandlerFactory; - - private final ByteBuffer preface; - - public InternalHttp2ClientProtocolNegotiator( - final TlsCapableIOSession ioSession, - final ClientHttp2StreamMultiplexerFactory http2StreamHandlerFactory) { - this.ioSession = ioSession; - this.http2StreamHandlerFactory = http2StreamHandlerFactory; - this.preface = ByteBuffer.wrap(PREFACE); - } - - @Override - public void connected(final IOSession session) { - try { - final TlsDetails tlsDetails = ioSession.getTlsDetails(); - if (tlsDetails != null) { - final String applicationProtocol = tlsDetails.getApplicationProtocol(); - if (!TextUtils.isEmpty(applicationProtocol)) { - if (!ApplicationProtocols.HTTP_2.id.equals(applicationProtocol)) { - throw new HttpException("Unexpected application protocol: " + applicationProtocol); - } - } - } - writePreface(session); - } catch (final Exception ex) { - session.shutdown(ShutdownType.IMMEDIATE); - exception(session, ex); - } - } - - private void writePreface(final IOSession session) throws IOException { - if (preface.hasRemaining()) { - final ByteChannel channel = session.channel(); - channel.write(preface); - } - if (!preface.hasRemaining()) { - final ClientHttp2StreamMultiplexer streamMultiplexer = http2StreamHandlerFactory.create(ioSession); - final IOEventHandler newHandler = new ClientHttp2IOEventHandler(streamMultiplexer); - newHandler.connected(session); - session.setHandler(newHandler); - } - } - - @Override - public void inputReady(final IOSession session) { - outputReady(session); - } - - @Override - public void outputReady(final IOSession session) { - try { - if (preface != null) { - writePreface(session); - } else { - session.shutdown(ShutdownType.IMMEDIATE); - } - } catch (final IOException ex) { - session.shutdown(ShutdownType.IMMEDIATE); - exception(session, ex); - } - } - - @Override - public void timeout(final IOSession session) { - exception(session, new SocketTimeoutException()); - } - - @Override - public void exception(final IOSession session, final Exception cause) { - try { - for (;;) { - final Command command = ioSession.getCommand(); - if (command != null) { - if (command instanceof ExecutionCommand) { - final ExecutionCommand executionCommand = (ExecutionCommand) command; - final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler(); - exchangeHandler.failed(cause); - exchangeHandler.releaseResources(); - } else { - command.cancel(); - } - } else { - break; - } - } - } finally { - session.shutdown(ShutdownType.IMMEDIATE); - } - } - - @Override - public void disconnected(final IOSession session) { - for (;;) { - final Command command = ioSession.getCommand(); - if (command != null) { - if (command instanceof ExecutionCommand) { - final ExecutionCommand executionCommand = (ExecutionCommand) command; - final AsyncClientExchangeHandler exchangeHandler = executionCommand.getExchangeHandler(); - exchangeHandler.failed(new ConnectionClosedException("Connection closed")); - exchangeHandler.releaseResources(); - } else { - command.cancel(); - } - } else { - break; - } - } - } - - @Override - public SSLSession getSSLSession() { - final TlsDetails tlsDetails = ioSession.getTlsDetails(); - return tlsDetails != null ? tlsDetails.getSSLSession() : null; - } - - @Override - public EndpointDetails getEndpointDetails() { - return null; - } - - @Override - public void setSocketTimeout(final int timeout) { - ioSession.setSocketTimeout(timeout); - } - - @Override - public int getSocketTimeout() { - return ioSession.getSocketTimeout(); - } - - @Override - public ProtocolVersion getProtocolVersion() { - return null; - } - - @Override - public SocketAddress getRemoteAddress() { - return ioSession.getRemoteAddress(); - } - - @Override - public SocketAddress getLocalAddress() { - return ioSession.getLocalAddress(); - } - - @Override - public boolean isOpen() { - return !ioSession.isClosed(); - } - - @Override - public void close() throws IOException { - ioSession.close(); - } - - @Override - public void shutdown(final ShutdownType shutdownType) { - ioSession.shutdown(shutdownType); - } - -} diff --git a/pom.xml b/pom.xml index 2f3d0f0af..b6e7f67e7 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,7 @@ 1.7 1.7 - 5.0-beta1 + 5.0-beta2 2.9.1 1.10 3.4.0 @@ -103,12 +103,6 @@ slf4j-api ${slf4j.version} - - org.apache.logging.log4j - log4j-api - ${log4j.version} - - org.apache.logging.log4j log4j-slf4j-impl