use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentDeserializerTest method testDoesntReturnZero.
@Test
public void testDoesntReturnZero() throws Exception {
String contentType = "multipart/mixed;boundary=----=_Part_1";
byte[] messageBytes = ("------=_Part_1\n\n" + "JJJJ\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD1\r\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD2\r\n" + "------=_Part_1" + "\n\nContent-Transfer-Encoding: binary\n\n" + "ABCD3\r\n" + "------=_Part_1--").getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream in = new ByteArrayInputStream(messageBytes) {
public int read(byte[] b, int off, int len) {
return super.read(b, off, len >= 2 ? 2 : len);
}
};
Message message = new MessageImpl();
message.put(Message.CONTENT_TYPE, contentType);
message.setContent(InputStream.class, in);
message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System.getProperty("java.io.tmpdir"));
message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String.valueOf(AttachmentDeserializer.THRESHOLD));
AttachmentDeserializer ad = new AttachmentDeserializer(message, Collections.singletonList("multipart/mixed"));
ad.initializeAttachments();
String s = IOUtils.toString(message.getContent(InputStream.class));
assertEquals("JJJJ", s.trim());
int count = 1;
for (Attachment a : message.getAttachments()) {
s = IOUtils.toString(a.getDataHandler().getInputStream());
assertEquals("ABCD" + count++, s);
}
in.close();
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentDeserializerTest method testInvalidContentDispositionFilename.
@Test
public void testInvalidContentDispositionFilename() throws Exception {
StringBuilder sb = new StringBuilder(1000);
sb.append("SomeHeader: foo\n").append("------=_Part_34950_1098328613.1263781527359\n").append("Content-Type: text/xml; charset=UTF-8\n").append("Content-Transfer-Encoding: binary\n").append("Content-Id: <318731183421.1263781527359.IBM.WEBSERVICES@auhpap02>\n").append('\n').append("<envelope/>\n");
sb.append("------=_Part_34950_1098328613.1263781527359\n").append("Content-Type: text/xml\n").append("Content-Transfer-Encoding: binary\n").append("Content-Id: <b86a5f2d-e7af-4e5e-b71a-9f6f2307cab0>\n").append("Content-Disposition: attachment; filename=../../../../../../../../etc/passwd\n").append('\n').append("<message>\n").append("------=_Part_34950_1098328613.1263781527359--\n");
msg = new MessageImpl();
msg.setContent(InputStream.class, new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)));
msg.put(Message.CONTENT_TYPE, "multipart/related");
AttachmentDeserializer ad = new AttachmentDeserializer(msg);
ad.initializeAttachments();
// Force it to load the attachments
assertEquals(1, msg.getAttachments().size());
Attachment attachment = msg.getAttachments().iterator().next();
AttachmentDataSource dataSource = (AttachmentDataSource) attachment.getDataHandler().getDataSource();
assertEquals("passwd", dataSource.getName());
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentDeserializerTest method testDeserializerSwAWithoutBoundryInContentType.
@Test
public void testDeserializerSwAWithoutBoundryInContentType() throws Exception {
InputStream is = getClass().getResourceAsStream("swadata");
String ct = "multipart/related; type=\"text/xml\"; ";
msg.put(Message.CONTENT_TYPE, ct);
msg.setContent(InputStream.class, is);
AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
deserializer.initializeAttachments();
InputStream attBody = msg.getContent(InputStream.class);
assertTrue(attBody != is);
assertTrue(attBody instanceof DelegatingInputStream);
Collection<Attachment> atts = msg.getAttachments();
assertNotNull(atts);
Iterator<Attachment> itr = atts.iterator();
assertTrue(itr.hasNext());
Attachment a = itr.next();
assertNotNull(a);
InputStream attIs = a.getDataHandler().getInputStream();
// check the cached output stream
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
IOUtils.copy(attBody, out);
assertTrue(out.toString().startsWith("<?xml"));
}
// try streaming a character off the wire
assertTrue(attIs.read() == 'f');
assertTrue(attIs.read() == 'o');
assertTrue(attIs.read() == 'o');
assertTrue(attIs.read() == 'b');
assertTrue(attIs.read() == 'a');
assertTrue(attIs.read() == 'r');
assertTrue(attIs.read() == -1);
assertFalse(itr.hasNext());
is.close();
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class AttachmentSerializerTest method testMessageMTOM.
@Test
public void testMessageMTOM() throws Exception {
MessageImpl msg = new MessageImpl();
Collection<Attachment> atts = new ArrayList<>();
AttachmentImpl a = new AttachmentImpl("test.xml");
InputStream is = getClass().getResourceAsStream("my.wav");
ByteArrayDataSource ds = new ByteArrayDataSource(is, "application/octet-stream");
a.setDataHandler(new DataHandler(ds));
atts.add(a);
msg.setAttachments(atts);
// Set the SOAP content type
msg.put(Message.CONTENT_TYPE, "application/soap+xml");
ByteArrayOutputStream out = new ByteArrayOutputStream();
msg.setContent(OutputStream.class, out);
AttachmentSerializer serializer = new AttachmentSerializer(msg);
serializer.writeProlog();
String ct = (String) msg.get(Message.CONTENT_TYPE);
assertTrue(ct.indexOf("multipart/related;") == 0);
assertTrue(ct.indexOf("start=\"<root.message@cxf.apache.org>\"") > -1);
assertTrue(ct.indexOf("start-info=\"application/soap+xml\"") > -1);
out.write("<soap:Body/>".getBytes());
serializer.writeAttachments();
out.flush();
DataSource source = new ByteArrayDataSource(new ByteArrayInputStream(out.toByteArray()), ct);
MimeMultipart mpart = new MimeMultipart(source);
Session session = Session.getDefaultInstance(new Properties());
MimeMessage inMsg = new MimeMessage(session);
inMsg.setContent(mpart);
inMsg.addHeaderLine("Content-Type: " + ct);
MimeMultipart multipart = (MimeMultipart) inMsg.getContent();
MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(0);
assertEquals("application/xop+xml; charset=UTF-8; type=\"application/soap+xml\"", part.getHeader("Content-Type")[0]);
assertEquals("binary", part.getHeader("Content-Transfer-Encoding")[0]);
assertEquals("<root.message@cxf.apache.org>", part.getHeader("Content-ID")[0]);
InputStream in = part.getDataHandler().getInputStream();
ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
IOUtils.copy(in, bodyOut);
out.close();
in.close();
assertEquals("<soap:Body/>", bodyOut.toString());
MimeBodyPart part2 = (MimeBodyPart) multipart.getBodyPart(1);
assertEquals("application/octet-stream", part2.getHeader("Content-Type")[0]);
assertEquals("binary", part2.getHeader("Content-Transfer-Encoding")[0]);
assertEquals("<test.xml>", part2.getHeader("Content-ID")[0]);
}
use of org.apache.cxf.message.Attachment in project cxf by apache.
the class WrappedMessageContextTest method testPutAndGetJaxwsAttachments.
@Test
public void testPutAndGetJaxwsAttachments() throws Exception {
WrappedMessageContext context = new WrappedMessageContext(new HashMap<String, Object>(), null, Scope.APPLICATION);
DataHandler dh1 = new DataHandler(new ByteArrayDataSource("Hello world!".getBytes(), "text/plain"));
DataHandler dh2 = new DataHandler(new ByteArrayDataSource("Hola mundo!".getBytes(), "text/plain"));
DataHandler dh3 = new DataHandler(new ByteArrayDataSource("Bonjour tout le monde!".getBytes(), "text/plain"));
Map<String, DataHandler> jattachments = new HashMap<>();
context.put(MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS, jattachments);
jattachments.put("attachment-1", dh1);
Set<Attachment> cattachments = CastUtils.cast((Set<?>) context.get(Message.ATTACHMENTS));
assertNotNull(cattachments);
assertEquals(1, cattachments.size());
jattachments.put("attachment-2", dh2);
assertEquals(2, cattachments.size());
AttachmentImpl ca = new AttachmentImpl("attachment-3", dh3);
ca.setHeader("X-test", "true");
cattachments.add(ca);
assertEquals(3, jattachments.size());
assertEquals(3, cattachments.size());
for (Attachment a : cattachments) {
if ("attachment-1".equals(a.getId())) {
assertEquals("Hello world!", a.getDataHandler().getContent());
} else if ("attachment-2".equals(a.getId())) {
assertEquals("Hola mundo!", a.getDataHandler().getContent());
} else if ("attachment-3".equals(a.getId())) {
assertEquals("Bonjour tout le monde!", a.getDataHandler().getContent());
assertEquals("true", a.getHeader("X-test"));
} else {
fail("unknown attachment");
}
}
}
Aggregations