Search in sources :

Example 21 with HeaderFilterStrategy

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

the class SqsEndpoint method createExchange.

private Exchange createExchange(ExchangePattern pattern, com.amazonaws.services.sqs.model.Message msg) {
    Exchange exchange = super.createExchange(pattern);
    Message message = exchange.getIn();
    message.setBody(msg.getBody());
    message.setHeaders(new HashMap<String, Object>(msg.getAttributes()));
    message.setHeader(SqsConstants.MESSAGE_ID, msg.getMessageId());
    message.setHeader(SqsConstants.MD5_OF_BODY, msg.getMD5OfBody());
    message.setHeader(SqsConstants.RECEIPT_HANDLE, msg.getReceiptHandle());
    message.setHeader(SqsConstants.ATTRIBUTES, msg.getAttributes());
    message.setHeader(SqsConstants.MESSAGE_ATTRIBUTES, msg.getMessageAttributes());
    //Need to apply the SqsHeaderFilterStrategy this time
    HeaderFilterStrategy headerFilterStrategy = getHeaderFilterStrategy();
    //the Sqs class MessageAttributeValue will not leak to the client
    for (Entry<String, MessageAttributeValue> entry : msg.getMessageAttributes().entrySet()) {
        String header = entry.getKey();
        Object value = translateValue(entry.getValue());
        if (!headerFilterStrategy.applyFilterToExternalHeaders(header, value, exchange)) {
            message.setHeader(header, value);
        }
    }
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue)

Example 22 with HeaderFilterStrategy

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

the class DirectVmHeaderFilterStrategyTest method testPropertiesPropagatedOrNot.

public void testPropertiesPropagatedOrNot() throws Exception {
    context.getRegistry(JndiRegistry.class).bind("headerFilterStrategy", new HeaderFilterStrategy() {

        @Override
        public boolean applyFilterToExternalHeaders(String headerName, Object headerValue, Exchange exchange) {
            return headerName.equals("Header2");
        }

        @Override
        public boolean applyFilterToCamelHeaders(String headerName, Object headerValue, Exchange exchange) {
            return headerName.equals("Header1");
        }
    });
    Exchange response = template.request("direct-vm:start.filter?headerFilterStrategy=#headerFilterStrategy", exchange -> {
        exchange.getIn().setBody("Hello World");
        exchange.getIn().setHeader("Header1", "Value1");
    });
    assertNull(response.getException());
    assertNull(response.getOut().getHeader("Header2"));
    response = template.request("direct-vm:start.nofilter", exchange -> {
        exchange.getIn().setBody("Hello World");
        exchange.getIn().setHeader("Header1", "Value1");
    });
    assertNull(response.getException());
    assertEquals("Value2", response.getOut().getHeader("Header2", String.class));
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) Exchange(org.apache.camel.Exchange) ContextTestSupport(org.apache.camel.ContextTestSupport) RouteBuilder(org.apache.camel.builder.RouteBuilder) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) Exchange(org.apache.camel.Exchange) JndiRegistry(org.apache.camel.impl.JndiRegistry) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy)

Example 23 with HeaderFilterStrategy

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

the class CxfRsHeaderFilterStrategyTest method testFilterCamelHeaders.

@Test
public void testFilterCamelHeaders() throws Exception {
    HeaderFilterStrategy filter = new CxfRsHeaderFilterStrategy();
    assertTrue("Get a wrong filtered result", filter.applyFilterToCamelHeaders(Exchange.CHARSET_NAME, "just a test", null));
    assertTrue("Get a wrong filtered result", filter.applyFilterToCamelHeaders(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, "just a test", null));
    assertTrue("Get a wrong filtered result", filter.applyFilterToCamelHeaders("org.apache.camel.such.Header", "just a test", null));
    assertFalse("Get a wrong filtered result", filter.applyFilterToCamelHeaders("camel.result", "just a test", null));
}
Also used : HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) Test(org.junit.Test)

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