Search in sources :

Example 1 with NettyServerBootstrapConfiguration

use of org.apache.camel.component.netty4.NettyServerBootstrapConfiguration in project camel by apache.

the class DefaultServerInitializerFactory method createSSLContext.

private SSLContext createSSLContext(CamelContext camelContext, NettyServerBootstrapConfiguration configuration) throws Exception {
    if (!configuration.isSsl()) {
        return null;
    }
    SSLContext answer;
    // create ssl context once
    if (configuration.getSslContextParameters() != null) {
        answer = configuration.getSslContextParameters().createSSLContext(camelContext);
    } else {
        if (configuration.getKeyStoreFile() == null && configuration.getKeyStoreResource() == null) {
            LOG.debug("keystorefile is null");
        }
        if (configuration.getTrustStoreFile() == null && configuration.getTrustStoreResource() == null) {
            LOG.debug("truststorefile is null");
        }
        if (configuration.getPassphrase().toCharArray() == null) {
            LOG.debug("passphrase is null");
        }
        SSLEngineFactory sslEngineFactory;
        if (configuration.getKeyStoreFile() != null || configuration.getTrustStoreFile() != null) {
            sslEngineFactory = new SSLEngineFactory();
            answer = sslEngineFactory.createSSLContext(camelContext.getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), "file:" + configuration.getKeyStoreFile().getPath(), "file:" + configuration.getTrustStoreFile().getPath(), configuration.getPassphrase().toCharArray());
        } else {
            sslEngineFactory = new SSLEngineFactory();
            answer = sslEngineFactory.createSSLContext(camelContext.getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), configuration.getKeyStoreResource(), configuration.getTrustStoreResource(), configuration.getPassphrase().toCharArray());
        }
    }
    return answer;
}
Also used : SSLContext(javax.net.ssl.SSLContext) SSLEngineFactory(org.apache.camel.component.netty4.ssl.SSLEngineFactory)

Example 2 with NettyServerBootstrapConfiguration

use of org.apache.camel.component.netty4.NettyServerBootstrapConfiguration in project camel by apache.

the class NettyHttpComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    NettyConfiguration config;
    if (getConfiguration() != null) {
        config = getConfiguration().copy();
    } else {
        config = new NettyHttpConfiguration();
    }
    HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
    // merge any custom bootstrap configuration on the config
    NettyServerBootstrapConfiguration bootstrapConfiguration = resolveAndRemoveReferenceParameter(parameters, "bootstrapConfiguration", NettyServerBootstrapConfiguration.class);
    if (bootstrapConfiguration != null) {
        Map<String, Object> options = new HashMap<String, Object>();
        if (IntrospectionSupport.getProperties(bootstrapConfiguration, options, null, false)) {
            IntrospectionSupport.setProperties(getCamelContext().getTypeConverter(), config, options);
        }
    }
    // any custom security configuration
    NettyHttpSecurityConfiguration securityConfiguration = resolveAndRemoveReferenceParameter(parameters, "securityConfiguration", NettyHttpSecurityConfiguration.class);
    Map<String, Object> securityOptions = IntrospectionSupport.extractProperties(parameters, "securityConfiguration.");
    NettyHttpBinding bindingFromUri = resolveAndRemoveReferenceParameter(parameters, "nettyHttpBinding", NettyHttpBinding.class);
    // are we using a shared http server?
    int sharedPort = -1;
    NettySharedHttpServer shared = resolveAndRemoveReferenceParameter(parameters, "nettySharedHttpServer", NettySharedHttpServer.class);
    if (shared != null) {
        // use port number from the shared http server
        LOG.debug("Using NettySharedHttpServer: {} with port: {}", shared, shared.getPort());
        sharedPort = shared.getPort();
    }
    // we must include the protocol in the remaining
    boolean hasProtocol = remaining.startsWith("http://") || remaining.startsWith("http:") || remaining.startsWith("https://") || remaining.startsWith("https:");
    if (!hasProtocol) {
        // http is the default protocol
        remaining = "http://" + remaining;
    }
    boolean hasSlash = remaining.startsWith("http://") || remaining.startsWith("https://");
    if (!hasSlash) {
        // must have double slash after protocol
        if (remaining.startsWith("http:")) {
            remaining = "http://" + remaining.substring(5);
        } else {
            remaining = "https://" + remaining.substring(6);
        }
    }
    LOG.debug("Netty http url: {}", remaining);
    // set port on configuration which is either shared or using default values
    if (sharedPort != -1) {
        config.setPort(sharedPort);
    } else if (config.getPort() == -1 || config.getPort() == 0) {
        if (remaining.startsWith("http:")) {
            config.setPort(80);
        } else if (remaining.startsWith("https:")) {
            config.setPort(443);
        }
    }
    if (config.getPort() == -1) {
        throw new IllegalArgumentException("Port number must be configured");
    }
    // configure configuration
    config = parseConfiguration(config, remaining, parameters);
    setProperties(config, parameters);
    // validate config
    config.validateConfiguration();
    // create the address uri which includes the remainder parameters (which
    // is not configuration parameters for this component)
    URI u = new URI(UnsafeUriCharactersEncoder.encodeHttpURI(remaining));
    String addressUri = URISupport.createRemainingURI(u, parameters).toString();
    NettyHttpEndpoint answer = new NettyHttpEndpoint(addressUri, this, config);
    // instance that can cause side-effects
    if (answer.getNettyHttpBinding() == null) {
        Object binding = null;
        if (bindingFromUri != null) {
            binding = bindingFromUri;
        } else {
            binding = getNettyHttpBinding();
        }
        if (binding instanceof RestNettyHttpBinding) {
            NettyHttpBinding copy = ((RestNettyHttpBinding) binding).copy();
            answer.setNettyHttpBinding(copy);
        } else if (binding instanceof DefaultNettyHttpBinding) {
            NettyHttpBinding copy = ((DefaultNettyHttpBinding) binding).copy();
            answer.setNettyHttpBinding(copy);
        }
    }
    if (headerFilterStrategy != null) {
        answer.setHeaderFilterStrategy(headerFilterStrategy);
    } else if (answer.getHeaderFilterStrategy() == null) {
        answer.setHeaderFilterStrategy(getHeaderFilterStrategy());
    }
    if (securityConfiguration != null) {
        answer.setSecurityConfiguration(securityConfiguration);
    } else if (answer.getSecurityConfiguration() == null) {
        answer.setSecurityConfiguration(getSecurityConfiguration());
    }
    // configure any security options
    if (securityOptions != null && !securityOptions.isEmpty()) {
        securityConfiguration = answer.getSecurityConfiguration();
        if (securityConfiguration == null) {
            securityConfiguration = new NettyHttpSecurityConfiguration();
            answer.setSecurityConfiguration(securityConfiguration);
        }
        setProperties(securityConfiguration, securityOptions);
        validateParameters(uri, securityOptions, null);
    }
    answer.setNettySharedHttpServer(shared);
    return answer;
}
Also used : HashMap(java.util.HashMap) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) NettyServerBootstrapConfiguration(org.apache.camel.component.netty4.NettyServerBootstrapConfiguration) URI(java.net.URI) Endpoint(org.apache.camel.Endpoint) NettyConfiguration(org.apache.camel.component.netty4.NettyConfiguration)

Example 3 with NettyServerBootstrapConfiguration

use of org.apache.camel.component.netty4.NettyServerBootstrapConfiguration in project camel by apache.

the class NettyHttpTwoRoutesBootstrapConfigurationTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();
    // create NettyServerBootstrapConfiguration instance where we can configure the bootstrap
    // option we want to use in our Camel routes. This allows us to configure this once,
    // and also explicit
    bootstrapConfiguration = new NettyServerBootstrapConfiguration();
    bootstrapConfiguration.setBacklog(200);
    bootstrapConfiguration.setConnectTimeout(5000);
    bootstrapConfiguration.setKeepAlive(true);
    bootstrapConfiguration.setWorkerCount(4);
    // register the configuration in the registry with this key
    jndi.bind("myBootstrapOptions", bootstrapConfiguration);
    return jndi;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) NettyServerBootstrapConfiguration(org.apache.camel.component.netty4.NettyServerBootstrapConfiguration)

Example 4 with NettyServerBootstrapConfiguration

use of org.apache.camel.component.netty4.NettyServerBootstrapConfiguration in project camel by apache.

the class DefaultNettySharedHttpServer method doStart.

protected void doStart() throws Exception {
    ObjectHelper.notNull(configuration, "setNettyServerBootstrapConfiguration() must be called with a NettyServerBootstrapConfiguration instance", this);
    // port must be set
    if (configuration.getPort() <= 0) {
        throw new IllegalArgumentException("Port must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    // hostname must be set
    if (ObjectHelper.isEmpty(configuration.getHost())) {
        throw new IllegalArgumentException("Host must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    LOG.debug("NettySharedHttpServer using configuration: {}", configuration);
    // force using tcp as the underlying transport
    configuration.setProtocol("tcp");
    channelFactory = new HttpServerMultiplexChannelHandler();
    channelFactory.init(configuration.getPort());
    ChannelInitializer<Channel> pipelineFactory = new HttpServerSharedInitializerFactory(configuration, channelFactory, classResolver);
    // thread factory and pattern
    String port = Matcher.quoteReplacement("" + configuration.getPort());
    String pattern = threadPattern;
    pattern = pattern.replaceFirst("#port#", port);
    ThreadFactory tf = new CamelThreadFactory(pattern, "NettySharedHttpServer", true);
    // create bootstrap factory and disable compatible check as its shared among the consumers
    bootstrapFactory = new HttpServerBootstrapFactory(channelFactory, false);
    bootstrapFactory.init(tf, configuration, pipelineFactory);
    ServiceHelper.startServices(channelFactory);
    if (startServer) {
        LOG.info("Starting NettySharedHttpServer on {}:{}", configuration.getHost(), configuration.getPort());
        ServiceHelper.startServices(bootstrapFactory);
    }
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) Channel(io.netty.channel.Channel) HttpServerMultiplexChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler)

Example 5 with NettyServerBootstrapConfiguration

use of org.apache.camel.component.netty4.NettyServerBootstrapConfiguration in project camel by apache.

the class HttpServerInitializerFactory method createSSLContext.

private SSLContext createSSLContext(CamelContext camelContext, NettyServerBootstrapConfiguration configuration) throws Exception {
    if (!configuration.isSsl()) {
        return null;
    }
    SSLContext answer;
    // create ssl context once
    if (configuration.getSslContextParameters() != null) {
        answer = configuration.getSslContextParameters().createSSLContext(camelContext);
    } else {
        if (configuration.getKeyStoreFile() == null && configuration.getKeyStoreResource() == null) {
            LOG.debug("keystorefile is null");
        }
        if (configuration.getTrustStoreFile() == null && configuration.getTrustStoreResource() == null) {
            LOG.debug("truststorefile is null");
        }
        if (configuration.getPassphrase().toCharArray() == null) {
            LOG.debug("passphrase is null");
        }
        SSLEngineFactory sslEngineFactory;
        if (configuration.getKeyStoreFile() != null || configuration.getTrustStoreFile() != null) {
            sslEngineFactory = new SSLEngineFactory();
            answer = sslEngineFactory.createSSLContext(camelContext.getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), "file:" + configuration.getKeyStoreFile().getPath(), "file:" + configuration.getTrustStoreFile().getPath(), configuration.getPassphrase().toCharArray());
        } else {
            sslEngineFactory = new SSLEngineFactory();
            answer = sslEngineFactory.createSSLContext(camelContext.getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), configuration.getKeyStoreResource(), configuration.getTrustStoreResource(), configuration.getPassphrase().toCharArray());
        }
    }
    return answer;
}
Also used : SSLContext(javax.net.ssl.SSLContext) SSLEngineFactory(org.apache.camel.component.netty4.ssl.SSLEngineFactory)

Aggregations

SSLContext (javax.net.ssl.SSLContext)2 NettyServerBootstrapConfiguration (org.apache.camel.component.netty4.NettyServerBootstrapConfiguration)2 SSLEngineFactory (org.apache.camel.component.netty4.ssl.SSLEngineFactory)2 Channel (io.netty.channel.Channel)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 Endpoint (org.apache.camel.Endpoint)1 NettyConfiguration (org.apache.camel.component.netty4.NettyConfiguration)1 HttpServerMultiplexChannelHandler (org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler)1 JndiRegistry (org.apache.camel.impl.JndiRegistry)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1 CamelThreadFactory (org.apache.camel.util.concurrent.CamelThreadFactory)1