Search in sources :

Example 36 with FileDataSource

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

the class MimeMultipartAlternativeTest method sendMultipartEmail.

private void sendMultipartEmail(boolean useInlineattachments) throws Exception {
    Mailbox.clearAll();
    // create an exchange with a normal body and attachment to be produced as email
    MailEndpoint endpoint = context.getEndpoint("smtp://ryan@mymailserver.com?password=secret", MailEndpoint.class);
    endpoint.getConfiguration().setUseInlineAttachments(useInlineattachments);
    endpoint.getConfiguration().setAlternativeBodyHeader(MailConstants.MAIL_ALTERNATIVE_BODY);
    // create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
    Exchange exchange = endpoint.createExchange();
    Message in = exchange.getIn();
    in.setBody(htmlBody);
    in.setHeader(MAIL_ALTERNATIVE_BODY, alternativeBody);
    in.addAttachment("cid:0001", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
    // create a producer that can produce the exchange (= send the mail)
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.message(0).header(MailConstants.MAIL_ALTERNATIVE_BODY).isNull();
    context.createProducerTemplate().send(endpoint, exchange);
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler)

Example 37 with FileDataSource

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

the class MimeMultipartAlternativeWithLongerFilenameTest method sendMultipartEmail.

private void sendMultipartEmail(boolean useInlineattachments) throws Exception {
    Mailbox.clearAll();
    // create an exchange with a normal body and attachment to be produced as email
    MailEndpoint endpoint = context.getEndpoint("smtp://ryan@mymailserver.com?password=secret", MailEndpoint.class);
    endpoint.getConfiguration().setUseInlineAttachments(useInlineattachments);
    endpoint.getConfiguration().setAlternativeBodyHeader(MailConstants.MAIL_ALTERNATIVE_BODY);
    // create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
    Exchange exchange = endpoint.createExchange();
    Message in = exchange.getIn();
    in.setBody(htmlBody);
    in.setHeader(MAIL_ALTERNATIVE_BODY, alternativeBody);
    in.addAttachment("cid:myCoolLogo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
    // create a producer that can produce the exchange (= send the mail)
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.message(0).header(MailConstants.MAIL_ALTERNATIVE_BODY).isNull();
    context.createProducerTemplate().send(endpoint, exchange);
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler)

Example 38 with FileDataSource

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

the class BeanWithAttachmentAnnotationTest method testBeanWithAnnotationAndExchangeTest.

public void testBeanWithAnnotationAndExchangeTest() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("attachment");
    template.send("direct:in", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message m = exchange.getIn();
            m.addAttachmentObject("attachment", new DefaultAttachment(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
        }
    });
    mock.assertIsSatisfied();
}
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)

Example 39 with FileDataSource

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

the class DefaultCxfBindingTest method testPopupalteExchangeFromCxfRequest.

@Test
public void testPopupalteExchangeFromCxfRequest() {
    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);
    org.apache.cxf.message.Message cxfMessage = new org.apache.cxf.message.MessageImpl();
    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"));
    headers.put("soapAction", Arrays.asList("urn:hello:world"));
    headers.put("myfruitheader", Arrays.asList("peach"));
    headers.put("mybrewheader", Arrays.asList("cappuccino", "espresso"));
    cxfMessage.put(org.apache.cxf.message.Message.PROTOCOL_HEADERS, headers);
    Set<Attachment> attachments = new HashSet<Attachment>();
    AttachmentImpl attachment = new AttachmentImpl("att-1", new DataHandler(new FileDataSource("pom.xml")));
    attachment.setHeader("attachment-header", "value 1");
    attachments.add(attachment);
    cxfMessage.setAttachments(attachments);
    cxfExchange.setInMessage(cxfMessage);
    cxfBinding.populateExchangeFromCxfRequest(cxfExchange, exchange);
    Map<String, Object> camelHeaders = exchange.getIn().getHeaders();
    assertNotNull(camelHeaders);
    assertEquals("urn:hello:world", camelHeaders.get("soapaction"));
    assertEquals("urn:hello:world", camelHeaders.get("SoapAction"));
    assertEquals("text/xml;charset=UTF-8", camelHeaders.get("content-type"));
    assertEquals("241", camelHeaders.get("content-length"));
    assertEquals("peach", camelHeaders.get("MyFruitHeader"));
    assertEquals(Arrays.asList("cappuccino", "espresso"), camelHeaders.get("MyBrewHeader"));
    Map<String, org.apache.camel.Attachment> camelAttachments = exchange.getIn().getAttachmentObjects();
    assertNotNull(camelAttachments);
    assertNotNull(camelAttachments.get("att-1"));
    assertEquals("value 1", camelAttachments.get("att-1").getHeader("attachment-header"));
}
Also used : 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 40 with FileDataSource

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

the class DefaultCxfBindingTest method testPopulateCxfRequestFromExchange.

@Test
public void testPopulateCxfRequestFromExchange() {
    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> requestContext = new HashMap<String, Object>();
    exchange.getIn().setHeader("soapAction", "urn:hello:world");
    exchange.getIn().setHeader("MyFruitHeader", "peach");
    exchange.getIn().setHeader("MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    exchange.getIn().addAttachment("att-1", new DataHandler(new FileDataSource("pom.xml")));
    exchange.getIn().getAttachmentObject("att-1").setHeader("attachment-header", "value 1");
    cxfBinding.populateCxfRequestFromExchange(cxfExchange, exchange, requestContext);
    // check the protocol headers
    Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) requestContext.get(Message.PROTOCOL_HEADERS));
    assertNotNull(headers);
    assertEquals(3, headers.size());
    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");
    verifyHeader(headers, "MyBrewHeader", Arrays.asList("cappuccino", "espresso"));
    Set<Attachment> attachments = CastUtils.cast((Set<?>) requestContext.get(CxfConstants.CAMEL_CXF_ATTACHMENTS));
    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) HashMap(java.util.HashMap) 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) FileDataSource(javax.activation.FileDataSource) List(java.util.List) Test(org.junit.Test)

Aggregations

FileDataSource (javax.activation.FileDataSource)55 DataHandler (javax.activation.DataHandler)46 File (java.io.File)22 DataSource (javax.activation.DataSource)18 Exchange (org.apache.camel.Exchange)18 MimeBodyPart (javax.mail.internet.MimeBodyPart)17 Test (org.junit.Test)16 Message (org.apache.camel.Message)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)12 MimeMultipart (javax.mail.internet.MimeMultipart)11 BodyPart (javax.mail.BodyPart)9 MimeMessage (javax.mail.internet.MimeMessage)8 Producer (org.apache.camel.Producer)7 URL (java.net.URL)6 MessagingException (javax.mail.MessagingException)6 Multipart (javax.mail.Multipart)6 InternetAddress (javax.mail.internet.InternetAddress)6 Endpoint (org.apache.camel.Endpoint)6 Processor (org.apache.camel.Processor)6 QName (javax.xml.namespace.QName)5