Search in sources :

Example 6 with HttpClientTransportOverHTTP2

use of org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2 in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testLastStreamId.

@Test
public void testLastStreamId() throws Exception {
    prepareServer(new RawHTTP2ServerConnectionFactory(new HttpConfiguration(), new ServerSessionListener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
            return settings;
        }

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            if (HttpMethod.HEAD.is(request.getMethod())) {
                stream.getSession().close(ErrorCode.REFUSED_STREAM_ERROR.code, null, Callback.NOOP);
            } else {
                MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            }
            return null;
        }
    }));
    server.start();
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger lastStream = new AtomicInteger();
    AtomicReference<Stream> streamRef = new AtomicReference<>();
    CountDownLatch streamLatch = new CountDownLatch(1);
    client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client()) {

        @Override
        protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
            return new HttpConnectionOverHTTP2(destination, session) {

                @Override
                protected HttpChannelOverHTTP2 newHttpChannel(boolean push) {
                    return new HttpChannelOverHTTP2(getHttpDestination(), this, getSession(), push) {

                        @Override
                        public void setStream(Stream stream) {
                            super.setStream(stream);
                            streamRef.set(stream);
                            streamLatch.countDown();
                        }
                    };
                }
            };
        }

        @Override
        protected void onClose(HttpConnectionOverHTTP2 connection, GoAwayFrame frame) {
            super.onClose(connection, frame);
            lastStream.set(frame.getLastStreamId());
            latch.countDown();
        }
    }, null);
    QueuedThreadPool clientExecutor = new QueuedThreadPool();
    clientExecutor.setName("client");
    client.setExecutor(clientExecutor);
    client.start();
    // Prime the connection to allow client and server prefaces to be exchanged.
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).path("/zero").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    org.eclipse.jetty.client.api.Request request = client.newRequest("localhost", connector.getLocalPort()).method(HttpMethod.HEAD).path("/one");
    request.send(result -> {
        if (result.isFailed())
            latch.countDown();
    });
    Assert.assertTrue(streamLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Stream stream = streamRef.get();
    Assert.assertNotNull(stream);
    Assert.assertEquals(lastStream.get(), stream.getId());
}
Also used : HashMap(java.util.HashMap) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 7 with HttpClientTransportOverHTTP2

use of org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2 in project jetty.project by eclipse.

the class AbstractTest method prepareClient.

protected void prepareClient() throws Exception {
    client = new HttpClient(new HttpClientTransportOverHTTP2(new HTTP2Client()), null);
    QueuedThreadPool clientExecutor = new QueuedThreadPool();
    clientExecutor.setName("client");
    client.setExecutor(clientExecutor);
}
Also used : QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client)

Example 8 with HttpClientTransportOverHTTP2

use of org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2 in project dropwizard by dropwizard.

the class Http2CIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    final HTTP2Client http2Client = new HTTP2Client();
    // No need for ALPN
    http2Client.setClientConnectionFactory(new HTTP2ClientConnectionFactory());
    client = new HttpClient(new HttpClientTransportOverHTTP2(http2Client), null);
    client.start();
}
Also used : HTTP2ClientConnectionFactory(org.eclipse.jetty.http2.client.HTTP2ClientConnectionFactory) HttpClientTransportOverHTTP2(org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Before(org.junit.Before)

Example 9 with HttpClientTransportOverHTTP2

use of org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2 in project cayenne by apache.

the class JettyHttp2ClientConnectionProvider method initJettyHttpClient.

@Override
protected HttpClient initJettyHttpClient() {
    try {
        HttpClientTransportOverHTTP2 http2 = new HttpClientTransportOverHTTP2(new HTTP2Client());
        boolean useALPN = runtimeProperties.getBoolean(ClientConstants.ROP_SERVICE_USE_ALPN_PROPERTY, false);
        http2.setUseALPN(useALPN);
        HttpClient httpClient = new HttpClient(http2, new SslContextFactory());
        httpClient.start();
        return httpClient;
    } catch (Exception e) {
        throw new CayenneRuntimeException("Exception while starting Jetty HttpClient over HTTP/2.", e);
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpClientTransportOverHTTP2(org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 10 with HttpClientTransportOverHTTP2

use of org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2 in project cayenne by apache.

the class JettyHttpROPConnectorIT method initJettyHttp2Client.

protected static HttpClient initJettyHttp2Client() {
    try {
        HttpClientTransportOverHTTP2 http2 = new HttpClientTransportOverHTTP2(new HTTP2Client());
        http2.setUseALPN(false);
        HttpClient httpClient = new HttpClient(http2, new SslContextFactory());
        httpClient.start();
        return httpClient;
    } catch (Exception e) {
        throw new CayenneRuntimeException("Exception while starting Jetty HttpClient over HTTP/2.", e);
    }
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpClientTransportOverHTTP2(org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ServletException(javax.servlet.ServletException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)10 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)10 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)6 HttpClientTransportOverHTTP2 (org.eclipse.jetty.http2.client.http.HttpClientTransportOverHTTP2)4 Test (org.junit.Test)4 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Executor (java.util.concurrent.Executor)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 HttpDestination (org.eclipse.jetty.client.HttpDestination)2 HttpFields (org.eclipse.jetty.http.HttpFields)2 MetaData (org.eclipse.jetty.http.MetaData)2 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)2 Session (org.eclipse.jetty.http2.api.Session)2 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)2 Before (org.junit.Before)2