Search in sources :

Example 1 with NettyConfiguration

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

the class DefaultClientInitializerFactory 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 : SSLContext(javax.net.ssl.SSLContext) SSLEngineFactory(org.apache.camel.component.netty4.ssl.SSLEngineFactory)

Example 2 with NettyConfiguration

use of org.apache.camel.component.netty4.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.");
    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 NettyConfiguration

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

the class ClientChannelHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel closed: {}", ctx.channel());
    }
    Exchange exchange = getExchange(ctx);
    AsyncCallback callback = getAsyncCallback(ctx);
    // remove state
    producer.removeState(ctx.channel());
    // to keep track of open sockets
    producer.getAllChannels().remove(ctx.channel());
    // this channel is maybe closing graceful and the exchange is already done
    // and if so we should not trigger an exception
    boolean doneUoW = exchange.getUnitOfWork() == null;
    NettyConfiguration configuration = producer.getConfiguration();
    if (configuration.isSync() && !doneUoW && !messageReceived && !exceptionHandled) {
        // To avoid call the callback.done twice
        exceptionHandled = true;
        // session was closed but no message received. This could be because the remote server had an internal error
        // and could not return a response. We should count down to stop waiting for a response            
        String address = configuration != null ? configuration.getAddress() : "";
        if (LOG.isDebugEnabled()) {
            LOG.debug("Channel closed but no message received from address: {}", address);
        }
        // don't fail the exchange if we actually specify to disconnect
        if (!configuration.isDisconnect()) {
            exchange.setException(new CamelExchangeException("No response received from remote server: " + address, exchange));
        }
        // signal callback
        callback.done(false);
    }
    // make sure the event can be processed by other handlers
    super.channelInactive(ctx);
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) AsyncCallback(org.apache.camel.AsyncCallback) NettyConfiguration(org.apache.camel.component.netty4.NettyConfiguration)

Example 4 with NettyConfiguration

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

the class HttpClientInitializerFactory 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.netty4.NettyConfiguration) SSLContext(javax.net.ssl.SSLContext) SSLEngineFactory(org.apache.camel.component.netty4.ssl.SSLEngineFactory)

Aggregations

NettyConfiguration (org.apache.camel.component.netty4.NettyConfiguration)3 SSLContext (javax.net.ssl.SSLContext)2 SSLEngineFactory (org.apache.camel.component.netty4.ssl.SSLEngineFactory)2 URI (java.net.URI)1 HashMap (java.util.HashMap)1 AsyncCallback (org.apache.camel.AsyncCallback)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 NettyServerBootstrapConfiguration (org.apache.camel.component.netty4.NettyServerBootstrapConfiguration)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1