Search in sources :

Example 6 with DataHandler

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

use of javax.activation.DataHandler 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)

Example 8 with DataHandler

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

the class DefaultHttpBinding method populateAttachments.

protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
    // check if there is multipart files, if so will put it into DataHandler
    Enumeration<?> names = request.getAttributeNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        Object object = request.getAttribute(name);
        LOG.trace("HTTP attachment {} = {}", name, object);
        if (object instanceof File) {
            String fileName = request.getParameter(name);
            message.addAttachment(fileName, new DataHandler(new CamelFileDataSource((File) object, fileName)));
        }
    }
}
Also used : DataHandler(javax.activation.DataHandler) File(java.io.File)

Example 9 with DataHandler

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

the class AttachmentHttpBinding method populateRequestParameters.

protected void populateRequestParameters(HttpServletRequest request, HttpMessage message) throws Exception {
    //we populate the http request parameters without checking the request method
    Map<String, Object> headers = message.getHeaders();
    Enumeration<?> names = request.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        if (message.getAttachment(name) != null) {
            DataHandler dh = message.getAttachment(name);
            Object value = dh;
            if (dh.getContentType() == null || dh.getContentType().startsWith("text/plain")) {
                value = request.getParameter(name);
            }
            if (getHeaderFilterStrategy() != null && !getHeaderFilterStrategy().applyFilterToExternalHeaders(name, value, message.getExchange())) {
                HttpHelper.appendHeader(headers, name, value);
            }
            continue;
        }
        // there may be multiple values for the same name
        String[] values = request.getParameterValues(name);
        LOG.trace("HTTP parameter {} = {}", name, values);
        if (values != null) {
            for (String value : values) {
                if (getHeaderFilterStrategy() != null && !getHeaderFilterStrategy().applyFilterToExternalHeaders(name, value, message.getExchange())) {
                    HttpHelper.appendHeader(headers, name, value);
                }
            }
        }
    }
}
Also used : DataHandler(javax.activation.DataHandler)

Example 10 with DataHandler

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

the class LdapRouteTest method testLdapRoutePreserveHeaderAndAttachments.

@Test
public void testLdapRoutePreserveHeaderAndAttachments() throws Exception {
    camel.addRoutes(createRouteBuilder("ldap:localhost:" + port + "?base=ou=system"));
    camel.start();
    final DataHandler dataHandler = new DataHandler("test", "text");
    Exchange out = template.request("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("(!(ou=test1))");
            exchange.getIn().setHeader("ldapTest", "Camel");
            exchange.getIn().addAttachment("ldapAttachment", dataHandler);
        }
    });
    Collection<SearchResult> searchResults = defaultLdapModuleOutAssertions(out);
    assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
    assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
    assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
    assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
    assertEquals("Camel", out.getOut().getHeader("ldapTest"));
    assertSame(dataHandler, out.getOut().getAttachment("ldapAttachment"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) SearchResult(javax.naming.directory.SearchResult) DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13