Search in sources :

Example 1 with HeaderFilterStrategy

use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.

the class ServletComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    // must extract well known parameters before we create the endpoint
    Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, "throwExceptionOnFailure", Boolean.class);
    Boolean transferException = getAndRemoveParameter(parameters, "transferException", Boolean.class);
    Boolean bridgeEndpoint = getAndRemoveParameter(parameters, "bridgeEndpoint", Boolean.class);
    HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
    Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, "matchOnUriPrefix", Boolean.class);
    String servletName = getAndRemoveParameter(parameters, "servletName", String.class, getServletName());
    String httpMethodRestrict = getAndRemoveParameter(parameters, "httpMethodRestrict", String.class);
    HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters, "headerFilterStrategy", HeaderFilterStrategy.class);
    Boolean async = getAndRemoveParameter(parameters, "async", Boolean.class);
    Boolean attachmentMultipartBinding = getAndRemoveParameter(parameters, "attachmentMultipartBinding", Boolean.class);
    Boolean disableStreamCache = getAndRemoveParameter(parameters, "disableStreamCache", Boolean.class);
    if (lenientContextPath()) {
        // the uri must have a leading slash for the context-path matching to work with servlet, and it can be something people
        // forget to add and then the servlet consumer cannot match the context-path as would have been expected
        String scheme = ObjectHelper.before(uri, ":");
        String after = ObjectHelper.after(uri, ":");
        // rebuild uri to have exactly one leading slash
        while (after.startsWith("/")) {
            after = after.substring(1);
        }
        after = "/" + after;
        uri = scheme + ":" + after;
    }
    // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
    URI httpUri = URISupport.createRemainingURI(new URI(UnsafeUriCharactersEncoder.encodeHttpURI(uri)), parameters);
    ServletEndpoint endpoint = createServletEndpoint(uri, this, httpUri);
    endpoint.setServletName(servletName);
    if (async != null) {
        endpoint.setAsync(async);
    }
    if (headerFilterStrategy != null) {
        endpoint.setHeaderFilterStrategy(headerFilterStrategy);
    } else {
        setEndpointHeaderFilterStrategy(endpoint);
    }
    // prefer to use endpoint configured over component configured
    if (binding == null) {
        // fallback to component configured
        binding = getHttpBinding();
    }
    if (binding != null) {
        endpoint.setBinding(binding);
    }
    // should we use an exception for failed error codes?
    if (throwExceptionOnFailure != null) {
        endpoint.setThrowExceptionOnFailure(throwExceptionOnFailure);
    }
    // should we transfer exception as serialized object
    if (transferException != null) {
        endpoint.setTransferException(transferException);
    }
    if (bridgeEndpoint != null) {
        endpoint.setBridgeEndpoint(bridgeEndpoint);
    }
    if (matchOnUriPrefix != null) {
        endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
    }
    if (httpMethodRestrict != null) {
        endpoint.setHttpMethodRestrict(httpMethodRestrict);
    }
    if (attachmentMultipartBinding != null) {
        endpoint.setAttachmentMultipartBinding(attachmentMultipartBinding);
    } else {
        endpoint.setAttachmentMultipartBinding(isAttachmentMultipartBinding());
    }
    if (disableStreamCache != null) {
        endpoint.setDisableStreamCache(disableStreamCache);
    }
    // turn off stream caching if in attachment mode
    if (endpoint.isAttachmentMultipartBinding()) {
        if (disableStreamCache == null) {
            // disableStreamCache not explicit configured so we can automatic change it
            LOG.info("Disabling stream caching as attachmentMultipartBinding is enabled");
            endpoint.setDisableStreamCache(true);
        } else if (!disableStreamCache) {
            throw new IllegalArgumentException("The options attachmentMultipartBinding=true and disableStreamCache=false cannot work together." + " Remove disableStreamCache to use AttachmentMultipartBinding");
        }
    }
    setProperties(endpoint, parameters);
    return endpoint;
}
Also used : HttpBinding(org.apache.camel.http.common.HttpBinding) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) URI(java.net.URI)

Example 2 with HeaderFilterStrategy

use of org.apache.camel.spi.HeaderFilterStrategy 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 3 with HeaderFilterStrategy

use of org.apache.camel.spi.HeaderFilterStrategy 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 4 with HeaderFilterStrategy

use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.

the class SqsProducerTest method setup.

@Before
public void setup() throws Exception {
    underTest = new SqsProducer(sqsEndpoint);
    sendMessageResult = new SendMessageResult().withMD5OfMessageBody(MESSAGE_MD5).withMessageId(MESSAGE_ID);
    sqsConfiguration = new SqsConfiguration();
    HeaderFilterStrategy headerFilterStrategy = new SqsHeaderFilterStrategy();
    sqsConfiguration.setDelaySeconds(Integer.valueOf(0));
    when(sqsEndpoint.getClient()).thenReturn(amazonSQSClient);
    when(sqsEndpoint.getConfiguration()).thenReturn(sqsConfiguration);
    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class))).thenReturn(sendMessageResult);
    when(exchange.getOut()).thenReturn(outMessage);
    when(exchange.getIn()).thenReturn(inMessage);
    when(exchange.getPattern()).thenReturn(ExchangePattern.InOnly);
    when(inMessage.getBody(String.class)).thenReturn(SAMPLE_MESSAGE_BODY);
    when(sqsEndpoint.getQueueUrl()).thenReturn(QUEUE_URL);
    when(sqsEndpoint.getHeaderFilterStrategy()).thenReturn(headerFilterStrategy);
}
Also used : HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) SendMessageResult(com.amazonaws.services.sqs.model.SendMessageResult) SendMessageRequest(com.amazonaws.services.sqs.model.SendMessageRequest) Before(org.junit.Before)

Example 5 with HeaderFilterStrategy

use of org.apache.camel.spi.HeaderFilterStrategy in project camel by apache.

the class SqsProducer method translateAttributes.

private Map<String, MessageAttributeValue> translateAttributes(Map<String, Object> headers, Exchange exchange) {
    Map<String, MessageAttributeValue> result = new HashMap<String, MessageAttributeValue>();
    HeaderFilterStrategy headerFilterStrategy = getEndpoint().getHeaderFilterStrategy();
    for (Entry<String, Object> entry : headers.entrySet()) {
        // only put the message header which is not filtered into the message attribute
        if (!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(), entry.getValue(), exchange)) {
            Object value = entry.getValue();
            if (value instanceof String) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("String");
                mav.withStringValue((String) value);
                result.put(entry.getKey(), mav);
            } else if (value instanceof ByteBuffer) {
                MessageAttributeValue mav = new MessageAttributeValue();
                mav.setDataType("Binary");
                mav.withBinaryValue((ByteBuffer) value);
                result.put(entry.getKey(), mav);
            } else {
                // cannot translate the message header to message attribute value
                LOG.warn("Cannot put the message header key={}, value={} into Sqs MessageAttribute", entry.getKey(), entry.getValue());
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) ByteBuffer(java.nio.ByteBuffer) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue)

Aggregations

HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)23 URI (java.net.URI)8 HashMap (java.util.HashMap)8 Message (org.apache.camel.Message)7 Exchange (org.apache.camel.Exchange)6 HttpBinding (org.apache.camel.http.common.HttpBinding)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Endpoint (org.apache.camel.Endpoint)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 HttpRestHeaderFilterStrategy (org.apache.camel.http.common.HttpRestHeaderFilterStrategy)3 UrlRewrite (org.apache.camel.http.common.UrlRewrite)3 Test (org.junit.Test)3 MessageAttributeValue (com.amazonaws.services.sqs.model.MessageAttributeValue)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ByteBuffer (java.nio.ByteBuffer)2 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)2 HttpProtocolHeaderFilterStrategy (org.apache.camel.http.common.HttpProtocolHeaderFilterStrategy)2