Search in sources :

Example 1 with DefaultHeaderFilterStrategy

use of org.apache.camel.impl.DefaultHeaderFilterStrategy in project camel by apache.

the class CxfHeaderHelperTest method testPropagateCamelToCxf.

@Test
public void testPropagateCamelToCxf() {
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setHeader("soapAction", "urn:hello:world");
    exchange.getIn().setHeader("MyFruitHeader", "peach");
    exchange.getIn().setHeader("MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/xml");
    exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, "200");
    exchange.getIn().setHeader(Exchange.HTTP_URI, "/hello/cxf");
    exchange.getIn().setHeader(Exchange.HTTP_METHOD, "GET");
    exchange.getIn().setHeader(Exchange.HTTP_PATH, "/hello/cxf");
    Map<String, Object> requestContext = Collections.singletonMap("request", "true");
    Map<String, Object> responseContext = Collections.singletonMap("response", "true");
    exchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);
    exchange.getIn().setHeader(Client.RESPONSE_CONTEXT, responseContext);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    CxfHeaderHelper.propagateCamelToCxf(new DefaultHeaderFilterStrategy(), exchange.getIn().getHeaders(), cxfMessage, exchange);
    assertEquals("text/xml", cxfMessage.get(Message.CONTENT_TYPE));
    assertEquals("200", cxfMessage.get(Message.RESPONSE_CODE));
    assertEquals(requestContext, cxfMessage.get(Client.REQUEST_CONTEXT));
    assertEquals(responseContext, cxfMessage.get(Client.RESPONSE_CONTEXT));
    assertNull(cxfMessage.get(Exchange.HTTP_RESPONSE_CODE));
    // check the protocol headers
    Map<String, List<String>> cxfHeaders = CastUtils.cast((Map<?, ?>) cxfMessage.get(org.apache.cxf.message.Message.PROTOCOL_HEADERS));
    assertNotNull(cxfHeaders);
    assertTrue(cxfHeaders.size() == 7);
    verifyHeader(cxfHeaders, "soapaction", "urn:hello:world");
    verifyHeader(cxfHeaders, "SoapAction", "urn:hello:world");
    verifyHeader(cxfHeaders, "SOAPAction", "urn:hello:world");
    verifyHeader(cxfHeaders, "myfruitheader", "peach");
    verifyHeader(cxfHeaders, "myFruitHeader", "peach");
    verifyHeader(cxfHeaders, "MYFRUITHEADER", "peach");
    verifyHeader(cxfHeaders, "MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    verifyHeader(cxfHeaders, Message.CONTENT_TYPE, "text/xml");
    verifyHeader(cxfHeaders, Message.REQUEST_URI, "/hello/cxf");
    verifyHeader(cxfHeaders, Message.HTTP_REQUEST_METHOD, "GET");
    verifyHeader(cxfHeaders, Message.PATH_INFO, "/hello/cxf");
    assertNull(cxfHeaders.get(Exchange.HTTP_RESPONSE_CODE));
    assertNull(cxfHeaders.get(Exchange.HTTP_URI));
    assertNull(cxfHeaders.get(Exchange.HTTP_METHOD));
    assertNull(cxfHeaders.get(Exchange.HTTP_PATH));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.cxf.message.Message) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) List(java.util.List) Test(org.junit.Test)

Example 2 with DefaultHeaderFilterStrategy

use of org.apache.camel.impl.DefaultHeaderFilterStrategy in project camel by apache.

the class CxfHeaderHelperTest method testPropagateCxfToCamel.

@Test
public void testPropagateCxfToCamel() {
    Exchange exchange = new DefaultExchange(context);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    Map<String, List<String>> cxfHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    cxfHeaders.put("Content-Length", Arrays.asList("241"));
    cxfHeaders.put("soapAction", Arrays.asList("urn:hello:world"));
    cxfHeaders.put("myfruitheader", Arrays.asList("peach"));
    cxfHeaders.put("mybrewheader", Arrays.asList("cappuccino", "espresso"));
    cxfHeaders.put(Message.CONTENT_TYPE, Arrays.asList("text/xml"));
    cxfHeaders.put(Message.ENCODING, Arrays.asList("UTF-8"));
    // Ignored
    cxfHeaders.put(Message.RESPONSE_CODE, Arrays.asList("201"));
    cxfHeaders.put(Message.REQUEST_URI, Arrays.asList("/base/hello/cxf"));
    cxfHeaders.put(Message.HTTP_REQUEST_METHOD, Arrays.asList("GET"));
    cxfHeaders.put(Message.PATH_INFO, Arrays.asList("/base/hello/cxf"));
    cxfHeaders.put(Message.BASE_PATH, Arrays.asList("/base"));
    cxfMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, cxfHeaders);
    cxfMessage.put(Message.RESPONSE_CODE, "200");
    Map<String, Object> requestContext = Collections.singletonMap("request", "true");
    Map<String, Object> responseContext = Collections.singletonMap("response", "true");
    cxfMessage.put(Client.REQUEST_CONTEXT, requestContext);
    cxfMessage.put(Client.RESPONSE_CONTEXT, responseContext);
    CxfHeaderHelper.propagateCxfToCamel(new DefaultHeaderFilterStrategy(), cxfMessage, exchange.getIn(), exchange);
    Map<String, Object> camelHeaders = exchange.getIn().getHeaders();
    assertEquals("urn:hello:world", camelHeaders.get("soapaction"));
    assertEquals("urn:hello:world", camelHeaders.get("SoapAction"));
    assertEquals("241", camelHeaders.get("content-length"));
    assertEquals("peach", camelHeaders.get("MyFruitHeader"));
    assertEquals(Arrays.asList("cappuccino", "espresso"), camelHeaders.get("MyBrewHeader"));
    assertEquals("text/xml; charset=UTF-8", camelHeaders.get(Exchange.CONTENT_TYPE));
    assertEquals("/base/hello/cxf", camelHeaders.get(Exchange.HTTP_URI));
    assertEquals("GET", camelHeaders.get(Exchange.HTTP_METHOD));
    assertEquals("/hello/cxf", camelHeaders.get(Exchange.HTTP_PATH));
    assertEquals("200", camelHeaders.get(Exchange.HTTP_RESPONSE_CODE));
    assertEquals(requestContext, camelHeaders.get(Client.REQUEST_CONTEXT));
    assertEquals(responseContext, camelHeaders.get(Client.RESPONSE_CONTEXT));
    assertNull(camelHeaders.get(Message.RESPONSE_CODE));
    assertNull(camelHeaders.get(Message.REQUEST_URI));
    assertNull(camelHeaders.get(Message.HTTP_REQUEST_METHOD));
    assertNull(camelHeaders.get(Message.PATH_INFO));
    assertNull(camelHeaders.get(Message.RESPONSE_CODE));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.cxf.message.Message) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) TreeMap(java.util.TreeMap) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) List(java.util.List) Test(org.junit.Test)

Example 3 with DefaultHeaderFilterStrategy

use of org.apache.camel.impl.DefaultHeaderFilterStrategy in project camel by apache.

the class DefaultCxfBindingTest method testPopulateCxfSoapHeaderRequestFromExchange.

@Test
public void testPopulateCxfSoapHeaderRequestFromExchange() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
    Exchange exchange = new DefaultExchange(context);
    org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
    Map<String, Object> requestContext = new HashMap<>();
    String expectedSoapActionHeader = "urn:hello:world";
    exchange.getIn().setHeader("soapAction", expectedSoapActionHeader);
    cxfBinding.populateCxfRequestFromExchange(cxfExchange, exchange, requestContext);
    String actualSoapActionHeader = (String) requestContext.get(SoapBindingConstants.SOAP_ACTION);
    assertEquals(expectedSoapActionHeader, actualSoapActionHeader);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) HashMap(java.util.HashMap) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Test(org.junit.Test)

Example 4 with DefaultHeaderFilterStrategy

use of org.apache.camel.impl.DefaultHeaderFilterStrategy in project camel by apache.

the class DefaultCxfBindingTest method testPopupalteExchangeFromCxfResponse.

@Test
public void testPopupalteExchangeFromCxfResponse() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
    Exchange exchange = new DefaultExchange(context);
    org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
    Map<String, Object> responseContext = new HashMap<String, Object>();
    responseContext.put(org.apache.cxf.message.Message.RESPONSE_CODE, Integer.valueOf(200));
    Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
    headers.put("content-type", Arrays.asList("text/xml;charset=UTF-8"));
    headers.put("Content-Length", Arrays.asList("241"));
    responseContext.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    cxfExchange.setInMessage(cxfMessage);
    Set<Attachment> attachments = new HashSet<Attachment>();
    AttachmentImpl attachment = new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml")));
    attachment.setHeader("additional-header", "value 1");
    attachments.add(attachment);
    cxfMessage.setAttachments(attachments);
    cxfBinding.populateExchangeFromCxfResponse(exchange, cxfExchange, responseContext);
    Map<String, Object> camelHeaders = exchange.getOut().getHeaders();
    assertNotNull(camelHeaders);
    assertEquals(responseContext, camelHeaders.get(Client.RESPONSE_CONTEXT));
    Map<String, org.apache.camel.Attachment> camelAttachments = exchange.getOut().getAttachmentObjects();
    assertNotNull(camelAttachments);
    assertNotNull(camelAttachments.get("att-1"));
    assertEquals("value 1", camelAttachments.get("att-1").getHeader("additional-header"));
}
Also used : HashMap(java.util.HashMap) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) FileDataSource(javax.activation.FileDataSource) List(java.util.List) HashSet(java.util.HashSet) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.cxf.message.Message) TreeMap(java.util.TreeMap) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 5 with DefaultHeaderFilterStrategy

use of org.apache.camel.impl.DefaultHeaderFilterStrategy in project camel by apache.

the class DefaultCxfBindingTest method testSetGetHeaderFilterStrategy.

@Test
public void testSetGetHeaderFilterStrategy() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    HeaderFilterStrategy hfs = new DefaultHeaderFilterStrategy();
    cxfBinding.setHeaderFilterStrategy(hfs);
    assertSame("The header filter strategy is set", hfs, cxfBinding.getHeaderFilterStrategy());
}
Also used : DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Test(org.junit.Test)

Aggregations

DefaultHeaderFilterStrategy (org.apache.camel.impl.DefaultHeaderFilterStrategy)15 Test (org.junit.Test)14 DefaultExchange (org.apache.camel.impl.DefaultExchange)13 Exchange (org.apache.camel.Exchange)12 List (java.util.List)10 Message (org.apache.cxf.message.Message)8 TreeMap (java.util.TreeMap)6 MessageImpl (org.apache.cxf.message.MessageImpl)6 HashMap (java.util.HashMap)5 DataHandler (javax.activation.DataHandler)4 FileDataSource (javax.activation.FileDataSource)4 Attachment (org.apache.cxf.message.Attachment)4 HashSet (java.util.HashSet)2 Message (org.apache.camel.Message)2 DefaultMessage (org.apache.camel.impl.DefaultMessage)2 AttachmentImpl (org.apache.cxf.attachment.AttachmentImpl)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 CamelContext (org.apache.camel.CamelContext)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1