Search in sources :

Example 1 with FileDataSource

use of javax.activation.FileDataSource 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 2 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class DefaultCxfBindingTest method testPopupalteCxfResponseFromExchange.

@Test
public void testPopupalteCxfResponseFromExchange() {
    DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
    cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
    Exchange exchange = new DefaultExchange(context, ExchangePattern.InOut);
    org.apache.cxf.message.Exchange cxfExchange = new org.apache.cxf.message.ExchangeImpl();
    exchange.setProperty(CxfConstants.DATA_FORMAT_PROPERTY, DataFormat.PAYLOAD);
    exchange.getOut().setHeader("soapAction", "urn:hello:world");
    exchange.getOut().setHeader("MyFruitHeader", "peach");
    exchange.getOut().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
    exchange.getOut().getAttachmentObject("att-1").setHeader("attachment-header", "value 1");
    IMocksControl control = EasyMock.createNiceControl();
    Endpoint endpoint = control.createMock(Endpoint.class);
    Binding binding = control.createMock(Binding.class);
    EasyMock.expect(endpoint.getBinding()).andReturn(binding);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    EasyMock.expect(binding.createMessage()).andReturn(cxfMessage);
    cxfExchange.put(Endpoint.class, endpoint);
    control.replay();
    cxfBinding.populateCxfResponseFromExchange(exchange, cxfExchange);
    cxfMessage = cxfExchange.getOutMessage();
    assertNotNull(cxfMessage);
    Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) cxfMessage.get(Message.PROTOCOL_HEADERS));
    assertNotNull(headers);
    assertTrue(headers.size() == 2);
    verifyHeader(headers, "soapaction", "urn:hello:world");
    verifyHeader(headers, "SoapAction", "urn:hello:world");
    verifyHeader(headers, "SOAPAction", "urn:hello:world");
    verifyHeader(headers, "myfruitheader", "peach");
    verifyHeader(headers, "myFruitHeader", "peach");
    verifyHeader(headers, "MYFRUITHEADER", "peach");
    Collection<Attachment> attachments = cxfMessage.getAttachments();
    assertNotNull(attachments);
    assertNotNull(attachments.size() == 1);
    Attachment att = attachments.iterator().next();
    assertEquals("att-1", att.getId());
    assertEquals("value 1", att.getHeader("attachment-header"));
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Binding(org.apache.cxf.binding.Binding) Message(org.apache.cxf.message.Message) DefaultHeaderFilterStrategy(org.apache.camel.impl.DefaultHeaderFilterStrategy) Attachment(org.apache.cxf.message.Attachment) DataHandler(javax.activation.DataHandler) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) IMocksControl(org.easymock.IMocksControl) Endpoint(org.apache.cxf.endpoint.Endpoint) FileDataSource(javax.activation.FileDataSource) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 3 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class BeanMethodWithExchangeTest method testBeanWithAnnotationAndExchangeTest.

public void testBeanWithAnnotationAndExchangeTest() throws Exception {
    Exchange result = template.request("direct:start1", new Processor() {

        public void process(Exchange exchange) throws Exception {
            Message m = exchange.getIn();
            m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
        }
    });
    assertTrue(result.getOut().getAttachmentObjects().containsKey("attachment2"));
    assertTrue(result.getOut().getAttachments().containsKey("attachment1"));
    assertEquals("attachmentValue1", result.getOut().getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1"));
    assertFalse(result.getOut().getAttachments().containsKey("attachment"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler)

Example 4 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class MessageWithAttachmentRedeliveryIssueTest method testMessageWithAttachmentRedeliveryIssue.

public void testMessageWithAttachmentRedeliveryIssue() throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.send("direct:start", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World");
            exchange.getIn().addAttachment("message1.xml", new DataHandler(new FileDataSource(new File("src/test/data/message1.xml"))));
            exchange.getIn().addAttachmentObject("message2.xml", new DefaultAttachment(new FileDataSource(new File("src/test/data/message2.xml"))));
        }
    });
    assertMockEndpointsSatisfied();
    Message msg = getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn();
    assertNotNull(msg);
    assertEquals("Hello World", msg.getBody());
    assertTrue(msg.hasAttachments());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) File(java.io.File)

Example 5 with FileDataSource

use of javax.activation.FileDataSource in project camel by apache.

the class ExpressionClauseTest method testAttachments.

public void testAttachments() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    mock.expectedBodiesReceivedInAnyOrder("log4j2.properties", "jndi-example.properties");
    template.send("direct:begin", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            Message m = exchange.getIn();
            m.setBody("Hello World");
            m.addAttachmentObject("log4j", new DefaultAttachment(new FileDataSource("src/test/resources/log4j2.properties")));
            m.addAttachment("jndi-example", new DataHandler(new FileDataSource("src/test/resources/jndi-example.properties")));
        }
    });
    assertMockEndpointsSatisfied();
    Map<String, Attachment> attachments = mock.getExchanges().get(0).getIn().getAttachmentObjects();
    assertTrue(attachments == null || attachments.size() == 0);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) DataHandler(javax.activation.DataHandler)

Aggregations

FileDataSource (javax.activation.FileDataSource)41 DataHandler (javax.activation.DataHandler)33 Exchange (org.apache.camel.Exchange)18 Message (org.apache.camel.Message)15 Test (org.junit.Test)15 File (java.io.File)12 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)11 MimeMultipart (javax.mail.internet.MimeMultipart)7 Producer (org.apache.camel.Producer)7 DataSource (javax.activation.DataSource)6 BodyPart (javax.mail.BodyPart)6 Multipart (javax.mail.Multipart)6 Endpoint (org.apache.camel.Endpoint)6 Processor (org.apache.camel.Processor)6 MessagingException (javax.mail.MessagingException)5 InternetAddress (javax.mail.internet.InternetAddress)5 MimeMessage (javax.mail.internet.MimeMessage)5 List (java.util.List)4 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)4