use of javax.activation.URLDataSource in project adempiere by adempiere.
the class EMail method setContent.
// addAttachment
/**
* Set the message content
* @throws MessagingException
* @throws IOException
*/
private void setContent() throws MessagingException, IOException {
// Local Character Set
String charSetName = Ini.getCharset().name();
if (charSetName == null || charSetName.length() == 0)
// WebEnv.ENCODING - alternative iso-8859-1
charSetName = "iso-8859-1";
//
m_msg.setSubject(getSubject(), charSetName);
// Simple Message
if (m_attachments == null || m_attachments.size() == 0) {
if (m_messageHTML == null || m_messageHTML.length() == 0)
m_msg.setText(getMessageCRLF(), charSetName);
else
m_msg.setDataHandler(new DataHandler(new ByteArrayDataSource(m_messageHTML, charSetName, "text/html")));
//
log.fine("(simple) " + getSubject());
} else // Multi part message ***************************************
{
// First Part - Message
MimeBodyPart mbp_1 = new MimeBodyPart();
mbp_1.setText("");
if (m_messageHTML == null || m_messageHTML.length() == 0)
mbp_1.setText(getMessageCRLF(), charSetName);
else
mbp_1.setDataHandler(new DataHandler(new ByteArrayDataSource(m_messageHTML, charSetName, "text/html")));
// Create Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp_1);
log.fine("(multi) " + getSubject() + " - " + mbp_1);
// for all attachments
for (int i = 0; i < m_attachments.size(); i++) {
Object attachment = m_attachments.get(i);
DataSource ds = null;
if (attachment instanceof File) {
File file = (File) attachment;
if (file.exists())
ds = new FileDataSource(file);
else {
log.log(Level.WARNING, "File does not exist: " + file);
continue;
}
} else if (attachment instanceof URI) {
URI url = (URI) attachment;
ds = new URLDataSource(url.toURL());
} else if (attachment instanceof DataSource)
ds = (DataSource) attachment;
else {
log.log(Level.WARNING, "Attachement type unknown: " + attachment);
continue;
}
// Attachment Part
MimeBodyPart mbp_2 = new MimeBodyPart();
mbp_2.setDataHandler(new DataHandler(ds));
mbp_2.setFileName(ds.getName());
log.fine("Added Attachment " + ds.getName() + " - " + mbp_2);
mp.addBodyPart(mbp_2);
}
// Add to Message
m_msg.setContent(mp);
}
// multi=part
}
use of javax.activation.URLDataSource in project webservices-axiom by apache.
the class TestParseURI method runTest.
protected void runTest() throws Throwable {
InstrumentedDataSource ds = new InstrumentedDataSource(new URLDataSource(XMLSample.SIMPLE.getUrl()));
DataSourceRegistration registration = DataSourceRegistry.registerDataSource(ds);
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(registration.getURL().toExternalForm());
assertThat(document.getDocumentElement().getLocalName()).isEqualTo("root");
assertThat(ds.getOpenStreamCount()).isEqualTo(0);
} finally {
registration.unregister();
}
}
Aggregations