Search in sources :

Example 1 with NettyConfiguration

use of org.apache.camel.component.netty.NettyConfiguration 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.");
    // 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);
    answer.setTimer(getTimer());
    // must use a copy of the binding on the endpoint to avoid sharing same instance that can cause side-effects
    if (answer.getNettyHttpBinding() == null) {
        Object 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.netty.NettyServerBootstrapConfiguration) URI(java.net.URI) Endpoint(org.apache.camel.Endpoint) NettyConfiguration(org.apache.camel.component.netty.NettyConfiguration)

Example 2 with NettyConfiguration

use of org.apache.camel.component.netty.NettyConfiguration in project camel by apache.

the class HttpClientPipelineFactory method createSSLContext.

private SSLContext createSSLContext(NettyProducer producer) throws Exception {
    NettyConfiguration configuration = producer.getConfiguration();
    if (!configuration.isSsl()) {
        return null;
    }
    SSLContext answer;
    // create ssl context once
    if (configuration.getSslContextParameters() != null) {
        answer = configuration.getSslContextParameters().createSSLContext(producer.getContext());
    } 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(producer.getContext().getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), "file:" + configuration.getKeyStoreFile().getPath(), "file:" + configuration.getTrustStoreFile().getPath(), configuration.getPassphrase().toCharArray());
        } else {
            sslEngineFactory = new SSLEngineFactory();
            answer = sslEngineFactory.createSSLContext(producer.getContext().getClassResolver(), configuration.getKeyStoreFormat(), configuration.getSecurityProvider(), configuration.getKeyStoreResource(), configuration.getTrustStoreResource(), configuration.getPassphrase().toCharArray());
        }
    }
    return answer;
}
Also used : NettyConfiguration(org.apache.camel.component.netty.NettyConfiguration) SSLContext(javax.net.ssl.SSLContext) SSLEngineFactory(org.apache.camel.component.netty.ssl.SSLEngineFactory)

Aggregations

NettyConfiguration (org.apache.camel.component.netty.NettyConfiguration)2 URI (java.net.URI)1 HashMap (java.util.HashMap)1 SSLContext (javax.net.ssl.SSLContext)1 Endpoint (org.apache.camel.Endpoint)1 NettyServerBootstrapConfiguration (org.apache.camel.component.netty.NettyServerBootstrapConfiguration)1 SSLEngineFactory (org.apache.camel.component.netty.ssl.SSLEngineFactory)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1