Search in sources :

Example 1 with Promise

use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP method newConnection.

@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
    HttpConnectionOverHTTP connection = newHttpConnection(endPoint, destination, promise);
    if (LOG.isDebugEnabled())
        LOG.debug("Created {}", connection);
    return customize(connection, context);
}
Also used : Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) HttpDestination(org.eclipse.jetty.client.HttpDestination)

Example 2 with Promise

use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.

the class HttpClientFailureTest method testFailureBeforeRequestCommit.

@Test
public void testFailureBeforeRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    try {
        client.newRequest("localhost", connector.getLocalPort()).onRequestHeaders(request -> connectionRef.get().getEndPoint().close()).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Promise(org.eclipse.jetty.util.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) ExecutionException(java.util.concurrent.ExecutionException) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Example 3 with Promise

use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.

the class ProxyProtocolTest method test_PROXY_GET_v1.

@Test
public void test_PROXY_GET_v1() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                Assert.assertEquals("1.2.3.4", request.getRemoteAddr());
                Assert.assertEquals(1111, request.getRemotePort());
                Assert.assertEquals("5.6.7.8", request.getLocalAddr());
                Assert.assertEquals(2222, request.getLocalPort());
            } catch (Throwable th) {
                th.printStackTrace();
                response.setStatus(500);
            }
            baseRequest.setHandled(true);
        }
    });
    String request1 = "PROXY TCP4 1.2.3.4 5.6.7.8 1111 2222\r\n";
    SocketChannel channel = SocketChannel.open();
    channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
    channel.write(ByteBuffer.wrap(request1.getBytes(StandardCharsets.UTF_8)));
    FuturePromise<Session> promise = new FuturePromise<>();
    client.accept(null, channel, new Session.Listener.Adapter(), promise);
    Session session = promise.get(5, TimeUnit.SECONDS);
    HttpFields fields = new HttpFields();
    String uri = "http://localhost:" + connector.getLocalPort() + "/";
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 4 with Promise

use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.

the class HTTP2ClientConnectionFactory method newConnection.

@Override
public Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HTTP2Client client = (HTTP2Client) context.get(CLIENT_CONTEXT_KEY);
    ByteBufferPool byteBufferPool = (ByteBufferPool) context.get(BYTE_BUFFER_POOL_CONTEXT_KEY);
    Executor executor = (Executor) context.get(EXECUTOR_CONTEXT_KEY);
    Scheduler scheduler = (Scheduler) context.get(SCHEDULER_CONTEXT_KEY);
    Session.Listener listener = (Session.Listener) context.get(SESSION_LISTENER_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Session> promise = (Promise<Session>) context.get(SESSION_PROMISE_CONTEXT_KEY);
    Generator generator = new Generator(byteBufferPool);
    FlowControlStrategy flowControl = client.getFlowControlStrategyFactory().newFlowControlStrategy();
    HTTP2ClientSession session = new HTTP2ClientSession(scheduler, endPoint, generator, listener, flowControl);
    Parser parser = new Parser(byteBufferPool, session, 4096, 8192);
    HTTP2ClientConnection connection = new HTTP2ClientConnection(client, byteBufferPool, executor, endPoint, parser, session, client.getInputBufferSize(), promise, listener);
    connection.addListener(connectionListener);
    return customize(connection, context);
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) Scheduler(org.eclipse.jetty.util.thread.Scheduler) Parser(org.eclipse.jetty.http2.parser.Parser) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) Promise(org.eclipse.jetty.util.Promise) Executor(java.util.concurrent.Executor) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Generator(org.eclipse.jetty.http2.generator.Generator)

Example 5 with Promise

use of org.eclipse.jetty.util.Promise in project jetty.project by eclipse.

the class HttpClientTest method testConnectHostWithMultipleAddresses.

@Test
public void testConnectHostWithMultipleAddresses() throws Exception {
    start(new EmptyServerHandler());
    client.setSocketAddressResolver(new SocketAddressResolver.Async(client.getExecutor(), client.getScheduler(), client.getConnectTimeout()) {

        @Override
        public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
            super.resolve(host, port, new Promise<List<InetSocketAddress>>() {

                @Override
                public void succeeded(List<InetSocketAddress> result) {
                    // Add as first address an invalid address so that we test
                    // that the connect operation iterates over the addresses.
                    result.add(0, new InetSocketAddress("idontexist", port));
                    promise.succeeded(result);
                }

                @Override
                public void failed(Throwable x) {
                    promise.failed(x);
                }
            });
        }
    });
    // If no exceptions the test passes.
    client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).header(HttpHeader.CONNECTION, "close").send();
}
Also used : FuturePromise(org.eclipse.jetty.util.FuturePromise) Promise(org.eclipse.jetty.util.Promise) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ArrayList(java.util.ArrayList) SocketAddressResolver(org.eclipse.jetty.util.SocketAddressResolver) EndPoint(org.eclipse.jetty.io.EndPoint) Test(org.junit.Test)

Aggregations

Promise (org.eclipse.jetty.util.Promise)12 Test (org.junit.Test)8 CountDownLatch (java.util.concurrent.CountDownLatch)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Connection (org.eclipse.jetty.client.api.Connection)5 EndPoint (org.eclipse.jetty.io.EndPoint)5 FuturePromise (org.eclipse.jetty.util.FuturePromise)5 InetSocketAddress (java.net.InetSocketAddress)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 HttpClientTransportOverHTTP (org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)4 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)4 MetaData (org.eclipse.jetty.http.MetaData)4 Session (org.eclipse.jetty.http2.api.Session)4 Stream (org.eclipse.jetty.http2.api.Stream)4 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4