Search in sources :

Example 1 with ClientCallback

use of io.undertow.client.ClientCallback in project spring-framework by spring-projects.

the class UndertowXhrTransport method executeReceiveRequest.

private void executeReceiveRequest(final TransportRequest transportRequest, final URI url, final HttpHeaders headers, final XhrClientSockJsSession session, final SettableListenableFuture<WebSocketSession> connectFuture) {
    if (logger.isTraceEnabled()) {
        logger.trace("Starting XHR receive request for " + url);
    }
    ClientCallback<ClientConnection> clientCallback = new ClientCallback<ClientConnection>() {

        @Override
        public void completed(ClientConnection connection) {
            ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath());
            HttpString headerName = HttpString.tryFromString(HttpHeaders.HOST);
            request.getRequestHeaders().add(headerName, url.getHost());
            addHttpHeaders(request, headers);
            HttpHeaders httpHeaders = transportRequest.getHttpRequestHeaders();
            connection.sendRequest(request, createReceiveCallback(transportRequest, url, httpHeaders, session, connectFuture));
        }

        @Override
        public void failed(IOException ex) {
            throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
        }
    };
    this.httpClient.connect(clientCallback, url, this.worker, this.bufferPool, this.optionMap);
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) ClientCallback(io.undertow.client.ClientCallback) HttpHeaders(org.springframework.http.HttpHeaders) ClientConnection(io.undertow.client.ClientConnection) IOException(java.io.IOException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString)

Example 2 with ClientCallback

use of io.undertow.client.ClientCallback in project spring-framework by spring-projects.

the class UndertowXhrTransport method createReceiveCallback.

private ClientCallback<ClientExchange> createReceiveCallback(final TransportRequest transportRequest, final URI url, final HttpHeaders headers, final XhrClientSockJsSession sockJsSession, final SettableListenableFuture<WebSocketSession> connectFuture) {
    return new ClientCallback<ClientExchange>() {

        @Override
        public void completed(final ClientExchange exchange) {
            exchange.setResponseListener(new ClientCallback<ClientExchange>() {

                @Override
                public void completed(ClientExchange result) {
                    ClientResponse response = result.getResponse();
                    if (response.getResponseCode() != 200) {
                        HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
                        IoUtils.safeClose(result.getConnection());
                        onFailure(new HttpServerErrorException(status, "Unexpected XHR receive status"));
                    } else {
                        SockJsResponseListener listener = new SockJsResponseListener(transportRequest, result.getConnection(), url, headers, sockJsSession, connectFuture);
                        listener.setup(result.getResponseChannel());
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("XHR receive headers: " + toHttpHeaders(response.getResponseHeaders()));
                    }
                    try {
                        StreamSinkChannel channel = result.getRequestChannel();
                        channel.shutdownWrites();
                        if (!channel.flush()) {
                            channel.getWriteSetter().set(ChannelListeners.<StreamSinkChannel>flushingChannelListener(null, null));
                            channel.resumeWrites();
                        }
                    } catch (IOException exc) {
                        IoUtils.safeClose(result.getConnection());
                        onFailure(exc);
                    }
                }

                @Override
                public void failed(IOException exc) {
                    IoUtils.safeClose(exchange.getConnection());
                    onFailure(exc);
                }
            });
        }

        @Override
        public void failed(IOException exc) {
            onFailure(exc);
        }

        private void onFailure(Throwable failure) {
            if (connectFuture.setException(failure)) {
                return;
            }
            if (sockJsSession.isDisconnected()) {
                sockJsSession.afterTransportClosed(null);
            } else {
                sockJsSession.handleTransportError(failure);
                sockJsSession.afterTransportClosed(new CloseStatus(1006, failure.getMessage()));
            }
        }
    };
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientResponse(io.undertow.client.ClientResponse) ClientCallback(io.undertow.client.ClientCallback) HttpStatus(org.springframework.http.HttpStatus) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException) CloseStatus(org.springframework.web.socket.CloseStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Example 3 with ClientCallback

use of io.undertow.client.ClientCallback in project undertow by undertow-io.

the class LoadBalancingProxyHTTP2TestCase method testHttp2ClientMultipleStreamsThreadSafety.

@Test
public void testHttp2ClientMultipleStreamsThreadSafety() throws IOException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
    //not actually a proxy test
    //but convent to put it here
    UndertowXnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, DefaultServer.createClientSslContext());
    final UndertowClient client = UndertowClient.getInstance();
    final ClientConnection connection = client.connect(new URI("https", null, DefaultServer.getHostAddress(), DefaultServer.getHostPort() + 1, "/", null, null), DefaultServer.getWorker(), ssl, DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
    final ExecutorService service = Executors.newFixedThreadPool(10);
    try {
        Deque<FutureResult<String>> futures = new ArrayDeque<>();
        for (int i = 0; i < 100; ++i) {
            final FutureResult<String> future = new FutureResult<>();
            futures.add(future);
            service.submit(new Callable<String>() {

                @Override
                public String call() throws Exception {
                    ClientRequest cr = new ClientRequest().setMethod(Methods.GET).setPath("/path").setProtocol(Protocols.HTTP_1_1);
                    connection.sendRequest(cr, new ClientCallback<ClientExchange>() {

                        @Override
                        public void completed(ClientExchange result) {
                            result.setResponseListener(new ClientCallback<ClientExchange>() {

                                @Override
                                public void completed(ClientExchange result) {
                                    new StringReadChannelListener(DefaultServer.getBufferPool()) {

                                        @Override
                                        protected void stringDone(String string) {
                                            future.setResult(string);
                                        }

                                        @Override
                                        protected void error(IOException e) {
                                            future.setException(e);
                                        }
                                    }.setup(result.getResponseChannel());
                                }

                                @Override
                                public void failed(IOException e) {
                                    future.setException(e);
                                }
                            });
                        }

                        @Override
                        public void failed(IOException e) {
                            future.setException(e);
                        }
                    });
                    return null;
                }
            });
        }
        while (!futures.isEmpty()) {
            FutureResult<String> future = futures.poll();
            Assert.assertNotEquals(IoFuture.Status.WAITING, future.getIoFuture().awaitInterruptibly(10, TimeUnit.SECONDS));
            Assert.assertEquals("/path", future.getIoFuture().get());
        }
    } finally {
        service.shutdownNow();
    }
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientCallback(io.undertow.client.ClientCallback) StringReadChannelListener(io.undertow.util.StringReadChannelListener) UndertowClient(io.undertow.client.UndertowClient) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) URI(java.net.URI) ArrayDeque(java.util.ArrayDeque) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FutureResult(org.xnio.FutureResult) ExecutorService(java.util.concurrent.ExecutorService) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Aggregations

ClientCallback (io.undertow.client.ClientCallback)3 IOException (java.io.IOException)3 ClientConnection (io.undertow.client.ClientConnection)2 ClientExchange (io.undertow.client.ClientExchange)2 ClientRequest (io.undertow.client.ClientRequest)2 HttpString (io.undertow.util.HttpString)2 ClientResponse (io.undertow.client.ClientResponse)1 UndertowClient (io.undertow.client.UndertowClient)1 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)1 StringReadChannelListener (io.undertow.util.StringReadChannelListener)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayDeque (java.util.ArrayDeque)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.junit.Test)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1