Search in sources :

Example 6 with URLDataSource

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
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) URLDataSource(javax.activation.URLDataSource) DataHandler(javax.activation.DataHandler) URI(java.net.URI) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File)

Example 7 with URLDataSource

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();
    }
}
Also used : InstrumentedDataSource(org.apache.axiom.testutils.activation.InstrumentedDataSource) URLDataSource(javax.activation.URLDataSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataSourceRegistration(org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration) Document(org.w3c.dom.Document)

Aggregations

URLDataSource (javax.activation.URLDataSource)7 DataHandler (javax.activation.DataHandler)4 DataSource (javax.activation.DataSource)3 File (java.io.File)2 URL (java.net.URL)2 FileDataSource (javax.activation.FileDataSource)2 Multipart (javax.mail.Multipart)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 InstrumentedDataSource (org.apache.axiom.testutils.activation.InstrumentedDataSource)2 DataSourceRegistration (org.apache.axiom.testutils.net.protocol.mem.DataSourceRegistration)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 BodyPart (javax.mail.BodyPart)1 JAXBElement (javax.xml.bind.JAXBElement)1 QName (javax.xml.namespace.QName)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 AttachmentPart (javax.xml.soap.AttachmentPart)1 MessageFactory (javax.xml.soap.MessageFactory)1 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)1