Search in sources :

Example 1 with OptionMap

use of org.xnio.OptionMap in project camel by apache.

the class UndertowEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    if (sslContextParameters != null) {
        sslContext = sslContextParameters.createSSLContext(getCamelContext());
    }
    // create options map
    if (options != null && !options.isEmpty()) {
        // favor to use the classloader that loaded the user application
        ClassLoader cl = getComponent().getCamelContext().getApplicationContextClassLoader();
        if (cl == null) {
            cl = Options.class.getClassLoader();
        }
        OptionMap.Builder builder = OptionMap.builder();
        for (Map.Entry<String, Object> entry : options.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null && value != null) {
                // upper case and dash as underscore
                key = key.toUpperCase(Locale.ENGLISH).replace('-', '_');
                // must be field name
                key = Options.class.getName() + "." + key;
                Option option = Option.fromString(key, cl);
                value = option.parseValue(value.toString(), cl);
                LOG.trace("Parsed option {}={}", option.getName(), value);
                builder.set(option, value);
            }
        }
        optionMap = builder.getMap();
    } else {
        // use an empty map
        optionMap = OptionMap.EMPTY;
    }
    // and then configure these default options if they have not been explicit configured
    if (keepAlive != null && !optionMap.contains(Options.KEEP_ALIVE)) {
        // rebuild map
        OptionMap.Builder builder = OptionMap.builder();
        builder.addAll(optionMap).set(Options.KEEP_ALIVE, keepAlive);
        optionMap = builder.getMap();
    }
    if (tcpNoDelay != null && !optionMap.contains(Options.TCP_NODELAY)) {
        // rebuild map
        OptionMap.Builder builder = OptionMap.builder();
        builder.addAll(optionMap).set(Options.TCP_NODELAY, tcpNoDelay);
        optionMap = builder.getMap();
    }
    if (reuseAddresses != null && !optionMap.contains(Options.REUSE_ADDRESSES)) {
        // rebuild map
        OptionMap.Builder builder = OptionMap.builder();
        builder.addAll(optionMap).set(Options.REUSE_ADDRESSES, tcpNoDelay);
        optionMap = builder.getMap();
    }
}
Also used : Options(org.xnio.Options) OptionMap(org.xnio.OptionMap) Option(org.xnio.Option) OptionMap(org.xnio.OptionMap) Map(java.util.Map)

Example 2 with OptionMap

use of org.xnio.OptionMap in project wildfly by wildfly.

the class ListenerService method start.

@Override
public void start(StartContext context) throws StartException {
    started = true;
    preStart(context);
    serverService.getValue().registerListener(this);
    try {
        openListener = createOpenListener();
        HttpHandler handler = serverService.getValue().getRoot();
        for (HandlerWrapper wrapper : listenerHandlerWrappers) {
            handler = wrapper.wrap(handler);
        }
        openListener.setRootHandler(handler);
        if (enabled) {
            final InetSocketAddress socketAddress = binding.getValue().getSocketAddress();
            final ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
            startListening(worker.getValue(), socketAddress, acceptListener);
        }
        registerBinding();
    } catch (IOException e) {
        cleanFailedStart();
        if (e instanceof BindException) {
            final StringBuilder sb = new StringBuilder().append(e.getLocalizedMessage());
            final InetSocketAddress socketAddress = binding.getValue().getSocketAddress();
            if (socketAddress != null)
                sb.append(" ").append(socketAddress);
            throw new StartException(sb.toString());
        } else {
            throw UndertowLogger.ROOT_LOGGER.couldNotStartListener(name, e);
        }
    }
    statisticsChangeListener = (enabled) -> {
        OptionMap options = openListener.getUndertowOptions();
        OptionMap.Builder builder = OptionMap.builder().addAll(options);
        builder.set(UndertowOptions.ENABLE_STATISTICS, enabled);
        openListener.setUndertowOptions(builder.getMap());
    };
    getUndertowService().registerStatisticsListener(statisticsChangeListener);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) InetSocketAddress(java.net.InetSocketAddress) OptionMap(org.xnio.OptionMap) BindException(java.net.BindException) StartException(org.jboss.msc.service.StartException) IOException(java.io.IOException) HandlerWrapper(io.undertow.server.HandlerWrapper) AcceptingChannel(org.xnio.channels.AcceptingChannel)

Example 3 with OptionMap

use of org.xnio.OptionMap in project wildfly by wildfly.

the class HttpsListenerAdd method createService.

@Override
ListenerService createService(String name, final String serverName, final OperationContext context, ModelNode model, OptionMap listenerOptions, OptionMap socketOptions) throws OperationFailedException {
    OptionMap.Builder builder = OptionMap.builder().addAll(socketOptions);
    HttpsListenerResourceDefinition.VERIFY_CLIENT.resolveOption(context, model, builder);
    ModelNode value = HttpsListenerResourceDefinition.ENABLED_CIPHER_SUITES.resolveModelAttribute(context, model);
    String cipherSuites = value.isDefined() ? value.asString() : null;
    HttpsListenerResourceDefinition.ENABLED_PROTOCOLS.resolveOption(context, model, builder);
    HttpsListenerResourceDefinition.SSL_SESSION_CACHE_SIZE.resolveOption(context, model, builder);
    HttpsListenerResourceDefinition.SSL_SESSION_TIMEOUT.resolveOption(context, model, builder);
    OptionMap.Builder listenerBuilder = OptionMap.builder().addAll(listenerOptions);
    HttpsListenerResourceDefinition.ENABLE_HTTP2.resolveOption(context, model, listenerBuilder);
    HttpListenerAdd.handleHttp2Options(context, model, listenerBuilder);
    HttpListenerResourceDefinition.REQUIRE_HOST_HTTP11.resolveOption(context, model, listenerBuilder);
    final boolean certificateForwarding = HttpListenerResourceDefinition.CERTIFICATE_FORWARDING.resolveModelAttribute(context, model).asBoolean();
    final boolean proxyAddressForwarding = HttpListenerResourceDefinition.PROXY_ADDRESS_FORWARDING.resolveModelAttribute(context, model).asBoolean();
    return new HttpsListenerService(name, serverName, listenerBuilder.getMap(), cipherSuites, builder.getMap(), certificateForwarding, proxyAddressForwarding);
}
Also used : OptionMap(org.xnio.OptionMap) ModelNode(org.jboss.dmr.ModelNode)

Example 4 with OptionMap

use of org.xnio.OptionMap in project wildfly by wildfly.

the class ModClusterService method start.

@Override
public synchronized void start(StartContext context) throws StartException {
    super.start(context);
    SSLContext sslContext = this.sslContext.getOptionalValue();
    if (sslContext == null) {
        SecurityRealm realm = securityRealm.getOptionalValue();
        if (realm != null) {
            sslContext = realm.getSSLContext();
        }
    }
    //TODO: SSL support for the client
    //TODO: wire up idle timeout when new version of undertow arrives
    final ModCluster.Builder modClusterBuilder;
    final XnioWorker worker = workerInjectedValue.getValue();
    if (sslContext == null) {
        modClusterBuilder = ModCluster.builder(worker);
    } else {
        OptionMap.Builder builder = OptionMap.builder();
        builder.set(Options.USE_DIRECT_BUFFERS, true);
        OptionMap combined = builder.getMap();
        XnioSsl xnioSsl = new UndertowXnioSsl(worker.getXnio(), combined, sslContext);
        modClusterBuilder = ModCluster.builder(worker, UndertowClient.getInstance(), xnioSsl);
    }
    modClusterBuilder.setMaxRetries(maxRetries).setClientOptions(clientOptions).setHealthCheckInterval(healthCheckInterval).setMaxRequestTime(maxRequestTime).setCacheConnections(cachedConnections).setQueueNewRequests(requestQueueSize > 0).setRequestQueueSize(requestQueueSize).setRemoveBrokenNodes(removeBrokenNodes).setTtl(connectionIdleTimeout).setMaxConnections(connectionsPerThread).setUseAlias(useAlias);
    if (FailoverStrategy.DETERMINISTIC.equals(failoverStrategy)) {
        modClusterBuilder.setDeterministicFailover(true);
    }
    modCluster = modClusterBuilder.build();
    MCMPConfig.Builder builder = MCMPConfig.builder();
    final SocketBinding advertiseBinding = advertiseSocketBinding.getOptionalValue();
    if (advertiseBinding != null) {
        InetAddress multicastAddress = advertiseBinding.getMulticastAddress();
        if (multicastAddress == null) {
            throw UndertowLogger.ROOT_LOGGER.advertiseSocketBindingRequiresMulticastAddress();
        }
        if (advertiseFrequency > 0) {
            builder.enableAdvertise().setAdvertiseAddress(advertiseBinding.getSocketAddress().getAddress().getHostAddress()).setAdvertiseGroup(multicastAddress.getHostAddress()).setAdvertisePort(advertiseBinding.getMulticastPort()).setAdvertiseFrequency(advertiseFrequency).setPath(advertisePath).setProtocol(advertiseProtocol).setSecurityKey(securityKey);
        }
    }
    builder.setManagementHost(managementSocketBinding.getValue().getSocketAddress().getHostString());
    builder.setManagementPort(managementSocketBinding.getValue().getSocketAddress().getPort());
    config = builder.build();
    if (advertiseBinding != null && advertiseFrequency > 0) {
        try {
            modCluster.advertise(config);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    modCluster.start();
}
Also used : SocketBinding(org.jboss.as.network.SocketBinding) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) XnioWorker(org.xnio.XnioWorker) SecurityRealm(org.jboss.as.domain.management.SecurityRealm) MCMPConfig(io.undertow.server.handlers.proxy.mod_cluster.MCMPConfig) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) ModCluster(io.undertow.server.handlers.proxy.mod_cluster.ModCluster) OptionMap(org.xnio.OptionMap) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) InetAddress(java.net.InetAddress)

Example 5 with OptionMap

use of org.xnio.OptionMap in project undertow by undertow-io.

the class ChunkedRequestTransferCodingTestCase method testMaxRequestSizeChunkedRequest.

@Test
@Ignore("sometimes the client attempts to re-use the same connection after the failure, but the server has already closed it")
public void testMaxRequestSizeChunkedRequest() throws IOException {
    connection = null;
    OptionMap existing = DefaultServer.getUndertowOptions();
    HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
    post.setHeader(HttpHeaders.CONNECTION, "close");
    TestHttpClient client = new TestHttpClient();
    try {
        generateMessage(1);
        post.setEntity(new StringEntity(message) {

            @Override
            public long getContentLength() {
                return -1;
            }
        });
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_ENTITY_SIZE, 3L));
        HttpResponse result = client.execute(post);
        Assert.assertEquals(StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        connection = null;
        DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.MAX_ENTITY_SIZE, (long) message.length()));
        result = client.execute(post);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
    } finally {
        DefaultServer.setUndertowOptions(existing);
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) OptionMap(org.xnio.OptionMap) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

OptionMap (org.xnio.OptionMap)34 InetSocketAddress (java.net.InetSocketAddress)10 Test (org.junit.Test)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 HttpResponse (org.apache.http.HttpResponse)8 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)7 ChannelListener (org.xnio.ChannelListener)7 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)6 IOException (java.io.IOException)6 ModelNode (org.jboss.dmr.ModelNode)5 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)4 HttpHandler (io.undertow.server.HttpHandler)4 HttpGet (org.apache.http.client.methods.HttpGet)4 StreamConnection (org.xnio.StreamConnection)4 XnioSsl (org.xnio.ssl.XnioSsl)4 AjpIgnore (io.undertow.testutils.AjpIgnore)3 HttpPost (org.apache.http.client.methods.HttpPost)3 StringEntity (org.apache.http.entity.StringEntity)3 ServiceName (org.jboss.msc.service.ServiceName)3 XnioWorker (org.xnio.XnioWorker)3