org.xnio.IoFuture Java Examples

The following examples show how to use org.xnio.IoFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServletWebSocketHttpExchange.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        final ServletInputStream in = request.getInputStream();
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
 
Example #2
Source File: JConsoleCLIPlugin.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean connectUsingRemoting(CommandContext cmdCtx, RemotingMBeanServerConnection rmtMBeanSvrConn)
        throws IOException, CliInitializationException {
    Connection conn = rmtMBeanSvrConn.getConnection();
    Channel channel;
    final IoFuture<Channel> futureChannel = conn.openChannel("management", OptionMap.EMPTY);
    IoFuture.Status result = futureChannel.await(5, TimeUnit.SECONDS);
    if (result == IoFuture.Status.DONE) {
        channel = futureChannel.get();
    } else {
        futureChannel.cancel();
        return false;
    }

    ModelControllerClient modelCtlrClient = ExistingChannelModelControllerClient.createReceiving(channel, createExecutor());
    cmdCtx.bindClient(modelCtlrClient);

    return true;
}
 
Example #3
Source File: DomainTestConnection.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
Channel openChannel(final Connection connection) throws IOException {
    final IoFuture<Channel> future = connection.openChannel(DEFAULT_CHANNEL_SERVICE_TYPE, OptionMap.EMPTY);
    future.await(10L, TimeUnit.SECONDS);
    if (future.getStatus() == IoFuture.Status.WAITING) {
        future.cancel();
        throw ProtocolLogger.ROOT_LOGGER.channelTimedOut();
    }
    final Channel channel = future.get();
    channel.addCloseHandler(new CloseHandler<Channel>() {
        @Override
        public void handleClose(final Channel old, final IOException e) {
            synchronized (ChannelStrategy.this) {
                if(ChannelStrategy.this.channel == old) {
                    ChannelStrategy.this.handler.handleClose(old, e);
                    ChannelStrategy.this.channel = null;
                }
            }
            handler.handleChannelClosed(old, e);
        }
    });
    return channel;
}
 
Example #4
Source File: GeneralTimeoutHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public Status await(IoFuture<?> future, long timeoutMillis) {
    final long startTime = System.currentTimeMillis();

    IoFuture.Status status = future.await(timeoutMillis, TimeUnit.MILLISECONDS);
    while (status == IoFuture.Status.WAITING) {
        if (thinking) {
            status = future.await(timeoutMillis, TimeUnit.MILLISECONDS);
        } else {
            long timeToWait = (timeoutMillis + thinkTime.get()) - (System.currentTimeMillis() - startTime);
            if (timeToWait > 0) {
                status = future.await(timeToWait, TimeUnit.MILLISECONDS);
            } else {
                return status;
            }
        }
    }

    return status;
}
 
Example #5
Source File: JBoss.java    From ysoserial with MIT License 6 votes vote down vote up
private static Channel getChannel ( ConnectionProviderContextImpl context, ConnectionHandler ch, OptionMap options ) throws IOException {
    Channel c;
    FutureResult<Channel> chResult = new FutureResult<Channel>(context.getExecutor());
    ch.open("jmx", chResult, options);

    IoFuture<Channel> cFuture = chResult.getIoFuture();
    Status s2 = cFuture.await();
    if ( s2 == Status.FAILED ) {
        System.err.println("Cannot connect");
        if ( cFuture.getException() != null ) {
            throw new IOException("Connect failed", cFuture.getException());
        }
    }
    else if ( s2 != Status.DONE ) {
        cFuture.cancel();
        throw new IOException("Connect timeout");
    }

    c = cFuture.get();
    return c;
}
 
Example #6
Source File: JBoss.java    From ysoserial-modified with MIT License 6 votes vote down vote up
private static Channel getChannel ( ConnectionProviderContextImpl context, ConnectionHandler ch, OptionMap options ) throws IOException {
    Channel c;
    FutureResult<Channel> chResult = new FutureResult<Channel>(context.getExecutor());
    ch.open("jmx", chResult, options);

    IoFuture<Channel> cFuture = chResult.getIoFuture();
    Status s2 = cFuture.await();
    if ( s2 == Status.FAILED ) {
        System.err.println("Cannot connect");
        if ( cFuture.getException() != null ) {
            throw new IOException("Connect failed", cFuture.getException());
        }
    }
    else if ( s2 != Status.DONE ) {
        cFuture.cancel();
        throw new IOException("Connect timeout");
    }

    c = cFuture.get();
    return c;
}
 
Example #7
Source File: BlockingWebSocketHttpServerExchange.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
 
Example #8
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
    ClientProvider provider = getClientProvider(uri);
    final FutureResult<ClientConnection> result = new FutureResult<>();
    provider.connect(new ClientCallback<ClientConnection>() {
        @Override
        public void completed(ClientConnection r) {
            result.setResult(r);
        }

        @Override
        public void failed(IOException e) {
            result.setException(e);
        }
    }, bindAddress, uri, ioThread, ssl, bufferPool, options);
    return result.getIoFuture();
}
 
Example #9
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
    ClientProvider provider = getClientProvider(uri);
    final FutureResult<ClientConnection> result = new FutureResult<>();
    provider.connect(new ClientCallback<ClientConnection>() {
        @Override
        public void completed(ClientConnection r) {
            result.setResult(r);
        }

        @Override
        public void failed(IOException e) {
            result.setException(e);
        }
    }, bindAddress, uri, worker, ssl, bufferPool, options);
    return result.getIoFuture();
}
 
Example #10
Source File: AjpClientProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    ChannelListener<StreamConnection> openListener = new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, ssl, bufferPool, options);
        }
    };
    IoFuture.Notifier<StreamConnection, Object> notifier = new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
    if(bindAddress == null) {
        worker.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, options).addNotifier(notifier, null);
    } else {
        worker.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, null, options).addNotifier(notifier, null);
    }
}
 
Example #11
Source File: AjpClientProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress,final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
    ChannelListener<StreamConnection> openListener = new ChannelListener<StreamConnection>() {
        @Override
        public void handleEvent(StreamConnection connection) {
            handleConnected(connection, listener, uri, ssl, bufferPool, options);
        }
    };
    IoFuture.Notifier<StreamConnection, Object> notifier = new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
    if(bindAddress == null) {
        ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, options).addNotifier(notifier, null);
    } else {
        ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 8009 : uri.getPort()), openListener, null, options).addNotifier(notifier, null);
    }
}
 
Example #12
Source File: Handshake.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static void writePayload(final WebSocketHttpExchange exchange, final ByteBuffer payload) {
    exchange.sendData(payload).addNotifier(new IoFuture.Notifier<Void, Object>() {
        @Override
        public void notify(final IoFuture<? extends Void> ioFuture, final Object attachment) {
            if (ioFuture.getStatus() == IoFuture.Status.DONE) {
                exchange.endExchange();
            } else {
                exchange.close();
            }
        }
    }, null);
}
 
Example #13
Source File: ServletWebSocketHttpExchange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IoFuture<Void> sendData(final ByteBuffer data) {
    try {
        final ServletOutputStream outputStream = response.getOutputStream();
        while (data.hasRemaining()) {
            outputStream.write(data.get());
        }
        return new FinishedIoFuture<>(null);
    } catch (IOException e) {
        final FutureResult<Void> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
 
Example #14
Source File: ProtocolConnectionUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Connect sync.
 *
 * @param configuration the protocol configuration
 * @return the connection
 * @throws IOException
 */
public static Connection connectSync(final ProtocolConnectionConfiguration configuration) throws IOException {
    long timeoutMillis = configuration.getConnectionTimeout();
    CallbackHandler handler = configuration.getCallbackHandler();
    final CallbackHandler actualHandler;
    ProtocolTimeoutHandler timeoutHandler = configuration.getTimeoutHandler();
    // Note: If a client supplies a ProtocolTimeoutHandler it is taking on full responsibility for timeout management.
    if (timeoutHandler == null) {
        GeneralTimeoutHandler defaultTimeoutHandler = new GeneralTimeoutHandler();
        // No point wrapping our AnonymousCallbackHandler.
        actualHandler = handler != null ? new WrapperCallbackHandler(defaultTimeoutHandler, handler) : null;
        timeoutHandler = defaultTimeoutHandler;
    } else {
        actualHandler = handler;
    }

    final IoFuture<Connection> future = connect(actualHandler, configuration);

    IoFuture.Status status = timeoutHandler.await(future, timeoutMillis);

    if (status == IoFuture.Status.DONE) {
        return future.get();
    }
    if (status == IoFuture.Status.FAILED) {
        throw ProtocolLogger.ROOT_LOGGER.failedToConnect(configuration.getUri(), future.getException());
    }
    throw ProtocolLogger.ROOT_LOGGER.couldNotConnect(configuration.getUri());
}
 
Example #15
Source File: ProtocolConnectionUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static IoFuture<Connection> connect(final ProtocolConnectionConfiguration configuration, CallbackHandler handler, Map<String, String> saslOptions, SSLContext sslContext) throws IOException {
    final ProtocolConnectionConfiguration config = ProtocolConnectionConfiguration.copy(configuration);
    config.setCallbackHandler(handler);
    config.setSaslOptions(saslOptions);
    config.setSslContext(sslContext);
    return connect(config);
}
 
Example #16
Source File: UndertowXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public IoFuture<ClientConnection> httpClientConnect(UndertowClient httpClient, URI uri,
		XnioWorker worker, OptionMap options) {
	return (IoFuture<ClientConnection>) ReflectionUtils.invokeMethod(httpClientConnectMethod, httpClient, uri,
			worker, this.undertowBufferPool, options);
}
 
Example #17
Source File: UndertowXhrTransport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public IoFuture<ClientConnection> httpClientConnect(UndertowClient httpClient, URI uri,
		XnioWorker worker, OptionMap options) {
	return (IoFuture<ClientConnection>) ReflectionUtils.invokeMethod(httpClientConnectMethod, httpClient, uri,
			worker, this.xnioBufferPool, options);
}
 
Example #18
Source File: Light4jHttpClientProvider.java    From light-4j with Apache License 2.0 5 votes vote down vote up
private IoFuture.Notifier<StreamConnection, Object> createNotifier(final ClientCallback<ClientConnection> listener) {
    return new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
}
 
Example #19
Source File: WebSocketClient.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public static IoFuture<WebSocketChannel> connect(XnioWorker worker, XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap optionMap, InetSocketAddress bindAddress, final URI uri, WebSocketVersion version, WebSocketClientNegotiation clientNegotiation, Set<ExtensionHandshake> clientExtensions) {
    return connectionBuilder(worker, bufferPool, uri)
            .setSsl(ssl)
            .setOptionMap(optionMap)
            .setBindAddress(bindAddress)
            .setVersion(version)
            .setClientNegotiation(clientNegotiation)
            .setClientExtensions(clientExtensions)
            .connect();
}
 
Example #20
Source File: BlockingWebSocketHttpServerExchange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IoFuture<Void> sendData(final ByteBuffer data) {
    try {
        while (data.hasRemaining()) {
            out.write(data.get());
        }
        return new FinishedIoFuture<>(null);
    } catch (IOException e) {
        final FutureResult<Void> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
 
Example #21
Source File: HttpClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private IoFuture.Notifier<StreamConnection, Object> createNotifier(final ClientCallback<ClientConnection> listener) {
    return new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
}
 
Example #22
Source File: Http2ClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private IoFuture.Notifier<StreamConnection, Object> createNotifier(final ClientCallback<ClientConnection> listener) {
    return new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
}
 
Example #23
Source File: Http2PriorKnowledgeClientProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private IoFuture.Notifier<StreamConnection, Object> createNotifier(final ClientCallback<ClientConnection> listener) {
    return new IoFuture.Notifier<StreamConnection, Object>() {
        @Override
        public void notify(IoFuture<? extends StreamConnection> ioFuture, Object o) {
            if (ioFuture.getStatus() == IoFuture.Status.FAILED) {
                listener.failed(ioFuture.getException());
            }
        }
    };
}
 
Example #24
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IoFuture<ClientConnection> connect(final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
    return connect((InetSocketAddress) null, uri, ioThread, null, bufferPool, options);
}
 
Example #25
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IoFuture<ClientConnection> connect(final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
    return connect(uri, worker, null, bufferPool, options);
}
 
Example #26
Source File: UndertowXnioSsl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IoFuture<SslConnection> openSslConnection(final XnioWorker worker, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super SslConnection> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) {
    final FutureResult<SslConnection> futureResult = new FutureResult<>(worker);
    final IoFuture<StreamConnection> connection = worker.openStreamConnection(bindAddress, destination, new StreamConnectionChannelListener(optionMap, destination, futureResult, openListener), bindListener, optionMap);
    return setupSslConnection(futureResult, connection);
}
 
Example #27
Source File: ProtocolConnectionUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static IoFuture<Connection> connect(final CallbackHandler handler, final ProtocolConnectionConfiguration configuration) throws IOException {
    configuration.validate();
    final Endpoint endpoint = configuration.getEndpoint();
    final URI uri = configuration.getUri();
    String clientBindAddress = configuration.getClientBindAddress();

    AuthenticationContext captured = AuthenticationContext.captureCurrent();
    AuthenticationConfiguration mergedConfiguration = AUTH_CONFIGURATION_CLIENT.getAuthenticationConfiguration(uri, captured);
    if (handler != null) {
        // Clear the three values as the CallbackHandler will be used for these.
        mergedConfiguration = mergedConfiguration.useAnonymous();
        mergedConfiguration = mergedConfiguration.useCredentials(IdentityCredentials.NONE);
        mergedConfiguration = mergedConfiguration.useRealm(null);
        mergedConfiguration = mergedConfiguration.useCallbackHandler(handler, DEFAULT_CALLBACK_KINDS);
    }

    Map<String, String> saslOptions = configuration.getSaslOptions();
    mergedConfiguration = configureSaslMechanisms(saslOptions, isLocal(uri), mergedConfiguration);

    // Pass through any other SASL options from the ProtocolConnectionConfiguration
    // When we merge these, any pre-existing options already associated with the
    // AuthenticationConfiguration will take precedence.
    if (saslOptions != null) {
        saslOptions = new HashMap<>(saslOptions);
        // Drop SASL_DISALLOWED_MECHANISMS which we already handled
        saslOptions.remove(Options.SASL_DISALLOWED_MECHANISMS.getName());
        mergedConfiguration = mergedConfiguration.useMechanismProperties(saslOptions);
    }

    SSLContext sslContext = configuration.getSslContext();
    if (sslContext == null) {
        try {
            sslContext = AUTH_CONFIGURATION_CLIENT.getSSLContext(uri, captured);
        } catch (GeneralSecurityException e) {
            throw ProtocolLogger.ROOT_LOGGER.failedToConnect(uri, e);
        }
    }

    // WFCORE-2342 check for default SSL / TLS options
    final OptionMap.Builder builder = OptionMap.builder();
    OptionMap optionMap = configuration.getOptionMap();
    for (Option option : optionMap) {
        builder.set(option, optionMap.get(option));
    }
    if (optionMap.get(Options.SSL_ENABLED) == null)
        builder.set(Options.SSL_ENABLED, configuration.isSslEnabled());
    if (optionMap.get(Options.SSL_STARTTLS) == null)
        builder.set(Options.SSL_STARTTLS, configuration.isUseStartTLS());

    AuthenticationContext authenticationContext = AuthenticationContext.empty();
    authenticationContext = authenticationContext.with(MatchRule.ALL, mergedConfiguration);
    final SSLContext finalSslContext = sslContext;
    authenticationContext = authenticationContext.withSsl(MatchRule.ALL, () -> finalSslContext);

    if (clientBindAddress == null) {
        return endpoint.connect(uri, builder.getMap(), authenticationContext);
    } else {
        InetSocketAddress bindAddr = new InetSocketAddress(clientBindAddress, 0);
        return endpoint.connect(uri, bindAddr, builder.getMap(), authenticationContext);
    }
}
 
Example #28
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, ByteBufferPool bufferPool, OptionMap options) {
    return connect(bindAddress, uri, ioThread, null, bufferPool, options);
}
 
Example #29
Source File: UndertowClient.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IoFuture<ClientConnection> connect(final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
    return connect((InetSocketAddress) null, uri, ioThread, ssl, bufferPool, options);
}
 
Example #30
Source File: ProtocolConnectionUtils.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static IoFuture<Connection> connect(final ProtocolConnectionConfiguration configuration, CallbackHandler handler) throws IOException {
    final ProtocolConnectionConfiguration config = ProtocolConnectionConfiguration.copy(configuration);
    config.setCallbackHandler(handler);
    return connect(config);
}