use of javax.activation.DataSource in project jodd by oblac.
the class SendMailTest method assertEmail.
// ---------------------------------------------------------------- util
private void assertEmail(Email email) throws MessagingException, IOException {
Message message = createMessage(email);
assertEquals(1, message.getFrom().length);
assertEquals("from@example.com", message.getFrom()[0].toString());
assertEquals(1, message.getRecipients(Message.RecipientType.TO).length);
assertEquals("to@example.com", message.getRecipients(Message.RecipientType.TO)[0].toString());
assertEquals("sub", message.getSubject());
// wrapper
MimeMultipart multipart = (MimeMultipart) message.getContent();
assertEquals(2, multipart.getCount());
// inner content #1
MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
assertEquals(2, mimeMultipart.getCount());
MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
assertEquals("Hello!", bodyPart.getContent());
// html message
bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1);
MimeMultipart htmlMessage = (MimeMultipart) bodyPart.getContent();
assertTrue(htmlMessage.getContentType().contains("multipart/related"));
assertEquals(2, htmlMessage.getCount());
// html - text
MimeBodyPart htmlMimeBodyPart = (MimeBodyPart) htmlMessage.getBodyPart(0);
assertEquals("<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>", htmlMimeBodyPart.getContent());
assertTrue(htmlMimeBodyPart.getDataHandler().getContentType().contains("text/html"));
// html - embedded
htmlMimeBodyPart = (MimeBodyPart) htmlMessage.getBodyPart(1);
DataSource dataSource = htmlMimeBodyPart.getDataHandler().getDataSource();
assertEquals("image/png", dataSource.getContentType());
assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, read(dataSource));
// inner content #2
mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(1);
dataSource = mimeBodyPart.getDataHandler().getDataSource();
assertEquals("application/zip", dataSource.getContentType());
assertArrayEquals(new byte[] { 11, 12, 13, 14, 15 }, read(dataSource));
}
use of javax.activation.DataSource in project nhin-d by DirectProject.
the class DocumentRepositoryAbstract method provideAndRegisterDocumentSet.
/**
* Handle an incoming ProvideAndRegisterDocumentSetRequestType object and
* transform to XDM or relay to another XDR endponit.
*
* @param prdst
* The incoming ProvideAndRegisterDocumentSetRequestType object
* @return a RegistryResponseType object
* @throws Exception
*/
protected RegistryResponseType provideAndRegisterDocumentSet(ProvideAndRegisterDocumentSetRequestType prdst) throws Exception {
RegistryResponseType resp = null;
try {
getHeaderData();
@SuppressWarnings("unused") InitialContext ctx = new InitialContext();
DirectDocuments documents = xdsDirectDocumentsTransformer.transform(prdst);
List<String> forwards = new ArrayList<String>();
// Get endpoints (first check direct:to header, then go to intendedRecipients)
if (StringUtils.isNotBlank(directTo))
forwards = Arrays.asList((new URI(directTo).getSchemeSpecificPart()));
else {
forwards = ParserHL7.parseRecipients(documents);
}
messageId = UUID.randomUUID().toString();
// TODO patID and subsetId
String patId = "PATID TBD";
String subsetId = "SUBSETID";
getAuditMessageGenerator().provideAndRegisterAudit(messageId, remoteHost, endpoint, to, thisHost, patId, subsetId, pid);
// Send to SMTP endpoints
if (getResolver().hasSmtpEndpoints(forwards)) {
// Get a reply address (first check direct:from header, then go to authorPerson)
if (StringUtils.isNotBlank(directFrom))
replyEmail = (new URI(directFrom)).getSchemeSpecificPart();
else {
replyEmail = documents.getSubmissionSet().getAuthorPerson();
replyEmail = StringUtils.splitPreserveAllTokens(replyEmail, "^")[0];
replyEmail = StringUtils.contains(replyEmail, "@") ? replyEmail : "nhindirect@nhindirect.org";
}
LOGGER.info("SENDING EMAIL TO " + getResolver().getSmtpEndpoints(forwards) + " with message id " + messageId);
// Construct message wrapper
DirectMessage message = new DirectMessage(replyEmail, getResolver().getSmtpEndpoints(forwards));
message.setSubject("XD* Originated Message");
message.setBody("Please find the attached XDM file.");
message.setDirectDocuments(documents);
// Send mail
MailClient mailClient = getMailClient();
mailClient.mail(message, messageId, suffix);
}
// Send to XD endpoints
for (String reqEndpoint : getResolver().getXdEndpoints(forwards)) {
String endpointUrl = getResolver().resolve(reqEndpoint);
String to = StringUtils.remove(endpointUrl, "?wsdl");
Long threadId = new Long(Thread.currentThread().getId());
LOGGER.info("THREAD ID " + threadId);
ThreadData threadData = new ThreadData(threadId);
threadData.setTo(to);
List<Document> docs = prdst.getDocument();
// Make a copy of the original documents
List<Document> originalDocs = new ArrayList<Document>();
for (Document d : docs) originalDocs.add(d);
// Clear document list
docs.clear();
// Re-add documents
for (Document d : originalDocs) {
Document doc = new Document();
doc.setId(d.getId());
DataHandler dh = d.getValue();
ByteArrayOutputStream buffOS = new ByteArrayOutputStream();
dh.writeTo(buffOS);
byte[] buff = buffOS.toByteArray();
DataSource source = new ByteArrayDataSource(buff, documents.getDocument(d.getId()).getMetadata().getMimeType());
DataHandler dhnew = new DataHandler(source);
doc.setValue(dhnew);
docs.add(doc);
}
LOGGER.info(" SENDING TO ENDPOINT " + to);
DocumentRepositoryProxy proxy = new DocumentRepositoryProxy(endpointUrl, new DirectSOAPHandlerResolver());
RegistryResponseType rrt = proxy.provideAndRegisterDocumentSetB(prdst);
String test = rrt.getStatus();
if (test.indexOf("Failure") >= 0) {
String error = "";
try {
error = rrt.getRegistryErrorList().getRegistryError().get(0).getCodeContext();
} catch (Exception x) {
}
throw new Exception("Failure Returned from XDR forward:" + error);
}
getAuditMessageGenerator().provideAndRegisterAuditSource(messageId, remoteHost, endpoint, to, thisHost, patId, subsetId, pid);
}
resp = getRepositoryProvideResponse(messageId);
relatesTo = messageId;
action = "urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-bResponse";
to = endpoint;
setHeaderData();
} catch (Exception e) {
e.printStackTrace();
throw (e);
}
return resp;
}
use of javax.activation.DataSource in project nhin-d by DirectProject.
the class DocumentRepositoryAbstract method provideAndRegisterDocumentSet.
/**
* Handle an incoming ProvideAndRegisterDocumentSetRequestType object and
* transform to XDM or relay to another XDR endponit.
*
* @param prdst
* The incoming ProvideAndRegisterDocumentSetRequestType object
* @return a RegistryResponseType object
* @throws Exception
*/
protected RegistryResponseType provideAndRegisterDocumentSet(ProvideAndRegisterDocumentSetRequestType prdst, SafeThreadData threadData) throws Exception {
RegistryResponseType resp = null;
try {
@SuppressWarnings("unused") InitialContext ctx = new InitialContext();
DirectDocuments documents = xdsDirectDocumentsTransformer.transform(prdst);
List<String> forwards = new ArrayList<String>();
// Get endpoints (first check direct:to header, then go to intendedRecipients)
if (StringUtils.isNotBlank(threadData.getDirectTo()))
forwards = Arrays.asList((new URI(threadData.getDirectTo()).getSchemeSpecificPart()));
else {
forwards = ParserHL7.parseDirectRecipients(documents);
}
// messageId = UUID.randomUUID().toString(); remove this , its is not righ,
//we should keep the message id of the original message for a lot of reasons vpl
// TODO patID and subsetId for atn
String patId = threadData.getMessageId();
String subsetId = threadData.getMessageId();
getAuditMessageGenerator().provideAndRegisterAudit(threadData.getMessageId(), threadData.getRemoteHost(), threadData.getRelatesTo(), threadData.getTo(), threadData.getThisHost(), patId, subsetId, threadData.getPid());
// Send to SMTP endpoints
if (getResolver().hasSmtpEndpoints(forwards)) {
String replyEmail;
// Get a reply address (first check direct:from header, then go to authorPerson)
if (StringUtils.isNotBlank(threadData.getDirectFrom()))
replyEmail = (new URI(threadData.getDirectFrom())).getSchemeSpecificPart();
else {
// replyEmail = documents.getSubmissionSet().getAuthorPerson();
replyEmail = documents.getSubmissionSet().getAuthorTelecommunication();
// replyEmail = StringUtils.splitPreserveAllTokens(replyEmail, "^")[0];
replyEmail = ParserHL7.parseXTN(replyEmail);
replyEmail = StringUtils.contains(replyEmail, "@") ? replyEmail : "nhindirect@nhindirect.org";
}
LOGGER.info("SENDING EMAIL TO " + getResolver().getSmtpEndpoints(forwards) + " with message id " + threadData.getMessageId());
// Construct message wrapper
DirectMessage message = new DirectMessage(replyEmail, getResolver().getSmtpEndpoints(forwards));
message.setSubject("XD* Originated Message");
message.setBody("Please find the attached XDM file.");
message.setDirectDocuments(documents);
// Send mail
MailClient mailClient = getMailClient();
String fileName = threadData.getMessageId().replaceAll("urn:uuid:", "");
mailClient.mail(message, fileName, threadData.getSuffix());
getAuditMessageGenerator().provideAndRegisterAuditSource(threadData.getMessageId(), threadData.getRemoteHost(), threadData.getRelatesTo(), threadData.getTo(), threadData.getThisHost(), patId, subsetId, threadData.getPid());
}
// Send to XD endpoints
for (String reqEndpoint : getResolver().getXdEndpoints(forwards)) {
String endpointUrl = getResolver().resolve(reqEndpoint);
String to = StringUtils.remove(endpointUrl, "?wsdl");
threadData.setTo(to);
threadData.setDirectTo(to);
threadData.save();
List<Document> docs = prdst.getDocument();
// Make a copy of the original documents
List<Document> originalDocs = new ArrayList<Document>();
for (Document d : docs) originalDocs.add(d);
// Clear document list
docs.clear();
// Re-add documents
for (Document d : originalDocs) {
Document doc = new Document();
doc.setId(d.getId());
DataHandler dh = d.getValue();
ByteArrayOutputStream buffOS = new ByteArrayOutputStream();
dh.writeTo(buffOS);
byte[] buff = buffOS.toByteArray();
DataSource source = new ByteArrayDataSource(buff, documents.getDocument(d.getId()).getMetadata().getMimeType());
DataHandler dhnew = new DataHandler(source);
doc.setValue(dhnew);
docs.add(doc);
}
LOGGER.info(" SENDING TO ENDPOINT " + to);
DocumentRepositoryProxy proxy = new DocumentRepositoryProxy(endpointUrl, new DirectSOAPHandlerResolver());
RegistryResponseType rrt = proxy.provideAndRegisterDocumentSetB(prdst);
String test = rrt.getStatus();
if (test.indexOf("Failure") >= 0) {
String error = "";
try {
error = rrt.getRegistryErrorList().getRegistryError().get(0).getCodeContext();
} catch (Exception x) {
}
throw new Exception("Failure Returned from XDR forward:" + error);
}
getAuditMessageGenerator().provideAndRegisterAuditSource(threadData.getMessageId(), threadData.getRemoteHost(), threadData.getRelatesTo(), threadData.getTo(), threadData.getThisHost(), patId, subsetId, threadData.getPid());
}
resp = getRepositoryProvideResponse(threadData.getMessageId());
String relatesTo = threadData.getRelatesTo();
threadData.setRelatesTo(threadData.getMessageId());
threadData.setAction("urn:ihe:iti:2007:ProvideAndRegisterDocumentSet-bResponse");
threadData.setTo(null);
threadData.save();
} catch (Exception e) {
e.printStackTrace();
throw (e);
}
return resp;
}
use of javax.activation.DataSource in project intellij-community by JetBrains.
the class Endpoint method getArtifactInfoByUri.
public static DataSource getArtifactInfoByUri(String uri) throws IOException, MalformedURLException {
DSDispatcher _dsDispatcher = new DSDispatcher();
UriBuilder _uriBuilder = new UriBuilder();
_uriBuilder.addPathSegment(uri);
String _url = _uriBuilder.buildUri(Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap());
DataSource _retVal = _dsDispatcher.doGET(_url, Collections.<String, Object>emptyMap(), "application/vnd.org.jfrog.artifactory.search.ArtifactSearchResult+json");
return _retVal;
}
use of javax.activation.DataSource in project camel by apache.
the class MimeMessageConsumeTest method populateMimeMessageBody.
/**
* Lets encode a multipart mime message
*/
protected void populateMimeMessageBody(MimeMessage message) throws MessagingException {
MimeBodyPart plainPart = new MimeBodyPart();
plainPart.setText(body);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText("<html><body>" + body + "</body></html>");
Multipart alt = new MimeMultipart("alternative");
alt.addBodyPart(plainPart);
alt.addBodyPart(htmlPart);
Multipart mixed = new MimeMultipart("mixed");
MimeBodyPart wrap = new MimeBodyPart();
wrap.setContent(alt);
mixed.addBodyPart(wrap);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
DataSource ds;
try {
File f = new File(getClass().getResource("/log4j2.properties").toURI());
ds = new FileDataSource(f);
} catch (URISyntaxException ex) {
ds = new URLDataSource(getClass().getResource("/log4j2.properties"));
}
DataHandler dh = new DataHandler(ds);
BodyPart attachmentBodyPart;
// Create another body part
attachmentBodyPart = new MimeBodyPart();
// Set the data handler to the attachment
attachmentBodyPart.setDataHandler(dh);
// Set the filename
attachmentBodyPart.setFileName(dh.getName());
// Set Disposition
attachmentBodyPart.setDisposition(Part.ATTACHMENT);
mixed.addBodyPart(plainPart);
mixed.addBodyPart(htmlPart);
// Add attachmentBodyPart to multipart
mixed.addBodyPart(attachmentBodyPart);
message.setContent(mixed);
}
Aggregations