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);
}
}
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);
}
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;
}
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"));
}
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);
}
}
}
Aggregations