use of javax.mail.util.ByteArrayDataSource in project camel by apache.
the class MimeMultipartDataFormatTest method roundtripWithBinaryAttachmentsAndBinaryContent.
@Test
public void roundtripWithBinaryAttachmentsAndBinaryContent() throws IOException {
String attContentType = "application/binary";
byte[] attText = { 0, 1, 2, 3, 4, 5, 6, 7 };
String attFileName = "Attachment File Name";
in.setBody("Body text");
DataSource ds = new ByteArrayDataSource(attText, attContentType);
in.addAttachment(attFileName, new DataHandler(ds));
Exchange result = template.send("direct:roundtripbinarycontent", exchange);
Message out = result.getOut();
assertEquals("Body text", out.getBody(String.class));
assertTrue(out.hasAttachments());
assertEquals(1, out.getAttachmentNames().size());
assertThat(out.getAttachmentNames(), hasItem(attFileName));
DataHandler dh = out.getAttachment(attFileName);
assertNotNull(dh);
assertEquals(attContentType, dh.getContentType());
InputStream is = dh.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os);
assertArrayEquals(attText, os.toByteArray());
}
use of javax.mail.util.ByteArrayDataSource in project camel by apache.
the class MimeMultipartDataFormatTest method roundtripWithBinaryAttachments.
@Test
public void roundtripWithBinaryAttachments() throws IOException {
String attContentType = "application/binary";
byte[] attText = { 0, 1, 2, 3, 4, 5, 6, 7 };
String attFileName = "Attachment File Name";
in.setBody("Body text");
DataSource ds = new ByteArrayDataSource(attText, attContentType);
in.addAttachment(attFileName, new DataHandler(ds));
Exchange result = template.send("direct:roundtrip", exchange);
Message out = result.getOut();
assertEquals("Body text", out.getBody(String.class));
assertTrue(out.hasAttachments());
assertEquals(1, out.getAttachmentNames().size());
assertThat(out.getAttachmentNames(), hasItem(attFileName));
DataHandler dh = out.getAttachment(attFileName);
assertNotNull(dh);
assertEquals(attContentType, dh.getContentType());
InputStream is = dh.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, os);
assertArrayEquals(attText, os.toByteArray());
}
use of javax.mail.util.ByteArrayDataSource in project nhin-d by DirectProject.
the class DirectDocuments method toProvideAndRegisterDocumentSetRequestType.
public ProvideAndRegisterDocumentSetRequestType toProvideAndRegisterDocumentSetRequestType() throws IOException {
ProvideAndRegisterDocumentSetRequestType request = new ProvideAndRegisterDocumentSetRequestType();
request.setSubmitObjectsRequest(this.getSubmitObjectsRequest());
for (DirectDocument2 document : documents) {
if (document.getData() != null) {
DataSource source = new ByteArrayDataSource(document.getData(), document.getMetadata().getMimeType());
DataHandler dhnew = new DataHandler(source);
Document pdoc = new Document();
pdoc.setValue(dhnew);
String id = document.getMetadata().getId();
pdoc.setId(id);
request.getDocument().add(pdoc);
}
}
return request;
}
use of javax.mail.util.ByteArrayDataSource in project nhin-d by DirectProject.
the class DefaultXdmXdsTransformer method transform.
/*
* (non-Javadoc)
*
* @see org.nhindirect.transform.XdmXdsTransformer#transform(java.io.File)
*/
@Override
public ProvideAndRegisterDocumentSetRequestType transform(File file) throws TransformationException {
LOGGER.trace("Begin transformation of XDM to XDS (file)");
String docId = null;
ZipFile zipFile = null;
String docName = getDocName(file);
if (docName != null) {
XDM_FILENAME_DATA = docName;
}
ProvideAndRegisterDocumentSetRequestType prsr = new ProvideAndRegisterDocumentSetRequestType();
try {
zipFile = new ZipFile(file, ZipFile.OPEN_READ);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
ZipEntry zipEntry = null;
// load the ZIP archive into memory
while (zipEntries.hasMoreElements()) {
zipEntry = zipEntries.nextElement();
String zname = zipEntry.getName();
LOGGER.trace("Processing a ZipEntry " + zname);
if (!zipEntry.isDirectory()) {
String subsetDirspec = getSubmissionSetDirspec(zipEntry.getName());
// Read metadata
if (matchName(zname, subsetDirspec, XDM_FILENAME_METADATA)) {
ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
SubmitObjectsRequest submitObjectRequest = (SubmitObjectsRequest) XmlUtils.unmarshal(byteArrayOutputStream.toString(), oasis.names.tc.ebxml_regrep.xsd.lcm._3.ObjectFactory.class);
prsr.setSubmitObjectsRequest(submitObjectRequest);
docId = getDocId(submitObjectRequest);
} else // Read data
if (matchName(zname, subsetDirspec, XDM_FILENAME_DATA)) {
ByteArrayOutputStream byteArrayOutputStream = readData(zipFile, zipEntry);
DataSource source = new ByteArrayDataSource(byteArrayOutputStream.toByteArray(), MimeType.APPLICATION_XML + "; charset=UTF-8");
DataHandler dhnew = new DataHandler(source);
Document pdoc = new Document();
pdoc.setValue(dhnew);
pdoc.setId(docId);
List<Document> docs = prsr.getDocument();
docs.add(pdoc);
}
}
if (!prsr.getDocument().isEmpty()) {
((Document) prsr.getDocument().get(0)).setId(zname);
}
}
zipFile.close();
} catch (Exception e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error("Unable to complete transformation.", e);
}
throw new TransformationException("Unable to complete transformation.", e);
}
return prsr;
}
use of javax.mail.util.ByteArrayDataSource in project nhin-d by DirectProject.
the class Notification method getNotificationFieldsAsHeaders.
/**
* Parses the notification part fields of a MDN MimeMessage message. The message is expected to conform to the MDN specification
* as described in RFC3798.
* @return The notification part fields as a set of Internet headers.
*/
public static InternetHeaders getNotificationFieldsAsHeaders(MimeMessage message) {
if (message == null)
throw new IllegalArgumentException("Message can not be null");
MimeMultipart mm = null;
try {
ByteArrayDataSource dataSource = new ByteArrayDataSource(message.getRawInputStream(), message.getContentType());
mm = new MimeMultipart(dataSource);
} catch (Exception e) {
throw new NHINDException("Failed to parse notification fields.", e);
}
return getNotificationFieldsAsHeaders(mm);
}
Aggregations