Search in sources :

Example 6 with Attachment

use of org.apache.camel.Attachment in project camel by apache.

the class AttachmentHttpBinding method populateAttachments.

@Override
protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
    try {
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            DataSource ds = new PartDataSource(part);
            Attachment attachment = new DefaultAttachment(ds);
            for (String headerName : part.getHeaderNames()) {
                for (String headerValue : part.getHeaders(headerName)) {
                    attachment.addHeader(headerName, headerValue);
                }
            }
            message.addAttachmentObject(part.getName(), attachment);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException("Cannot populate attachments", e);
    }
}
Also used : Part(javax.servlet.http.Part) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Example 7 with Attachment

use of org.apache.camel.Attachment 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 Attachment

use of org.apache.camel.Attachment in project camel by apache.

the class MimeMultipartDataFormatTest method unmarshalAndCheckAttachmentName.

private Attachment unmarshalAndCheckAttachmentName(String matcher) throws IOException, UnsupportedEncodingException {
    Exchange intermediate = template.send("direct:unmarshalonlyinlineheaders", exchange);
    assertNotNull(intermediate.getOut());
    String bodyStr = intermediate.getOut().getBody(String.class);
    assertNotNull(bodyStr);
    assertThat(bodyStr, startsWith("25"));
    assertEquals(1, intermediate.getOut().getAttachmentNames().size());
    assertThat(intermediate.getOut().getAttachmentNames().iterator().next(), containsString(matcher));
    Attachment att = intermediate.getOut().getAttachmentObject(intermediate.getOut().getAttachmentNames().iterator().next());
    DataHandler dh = att.getDataHandler();
    assertNotNull(dh);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    dh.writeTo(bos);
    String attachmentString = new String(bos.toByteArray(), "UTF-8");
    assertThat(attachmentString, startsWith("Old MacDonald had a farm"));
    return att;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with Attachment

use of org.apache.camel.Attachment in project camel by apache.

the class MimeMultipartDataFormatTest method roundtripWithTextAttachments.

@Test
public void roundtripWithTextAttachments() throws IOException {
    String attContentType = "text/plain";
    String attText = "Attachment Text";
    String attFileName = "Attachment File Name";
    in.setBody("Body text");
    in.setHeader(Exchange.CONTENT_TYPE, "text/plain;charset=iso8859-1;other-parameter=true");
    in.setHeader(Exchange.CONTENT_ENCODING, "UTF8");
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Description", "Sample Attachment Data");
    headers.put("X-AdditionalData", "additional data");
    addAttachment(attContentType, attText, attFileName, headers);
    Exchange result = template.send("direct:roundtrip", exchange);
    Message out = result.getOut();
    assertEquals("Body text", out.getBody(String.class));
    assertThat(out.getHeader(Exchange.CONTENT_TYPE, String.class), startsWith("text/plain"));
    assertEquals("UTF8", out.getHeader(Exchange.CONTENT_ENCODING));
    assertTrue(out.hasAttachments());
    assertEquals(1, out.getAttachmentNames().size());
    assertThat(out.getAttachmentNames(), hasItem(attFileName));
    Attachment att = out.getAttachmentObject(attFileName);
    DataHandler dh = att.getDataHandler();
    assertNotNull(dh);
    assertEquals(attContentType, dh.getContentType());
    InputStream is = dh.getInputStream();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    IOHelper.copyAndCloseInput(is, os);
    assertEquals(attText, new String(os.toByteArray()));
    assertEquals("Sample Attachment Data", att.getHeader("content-description"));
    assertEquals("additional data", att.getHeader("X-AdditionalData"));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Message(org.apache.camel.Message) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.apache.camel.Attachment) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) StringContains.containsString(org.hamcrest.core.StringContains.containsString) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 10 with Attachment

use of org.apache.camel.Attachment in project camel by apache.

the class AttachmentHttpBinding method populateAttachments.

@Override
protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
    Object object = request.getAttribute("org.eclipse.jetty.servlet.MultiPartFile.multiPartInputStream");
    if (object instanceof MultiPartInputStreamParser) {
        MultiPartInputStreamParser parser = (MultiPartInputStreamParser) object;
        Collection<Part> parts;
        try {
            parts = parser.getParts();
            for (Part part : parts) {
                DataSource ds = new PartDataSource(part);
                Attachment attachment = new DefaultAttachment(ds);
                for (String headerName : part.getHeaderNames()) {
                    for (String headerValue : part.getHeaders(headerName)) {
                        attachment.addHeader(headerName, headerValue);
                    }
                }
                message.addAttachmentObject(part.getName(), attachment);
            }
        } catch (Exception e) {
            throw new RuntimeCamelException("Cannot populate attachments", e);
        }
    }
}
Also used : Part(javax.servlet.http.Part) MultiPartInputStreamParser(org.eclipse.jetty.util.MultiPartInputStreamParser) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Aggregations

Attachment (org.apache.camel.Attachment)12 DefaultAttachment (org.apache.camel.impl.DefaultAttachment)9 DataHandler (javax.activation.DataHandler)5 Exchange (org.apache.camel.Exchange)5 Message (org.apache.camel.Message)5 Test (org.junit.Test)4 Map (java.util.Map)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 StringContains.containsString (org.hamcrest.core.StringContains.containsString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 BodyPart (javax.mail.BodyPart)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 Part (javax.servlet.http.Part)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2