Search in sources :

Example 41 with DataSource

use of javax.activation.DataSource 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 42 with DataSource

use of javax.activation.DataSource in project tomee by apache.

the class AttachmentTest method testAttachmentViaWsInterface.

//END SNIPPET: setup    
/**
     * Create a webservice client using wsdl url
     *
     * @throws Exception
     */
//START SNIPPET: webservice
public void testAttachmentViaWsInterface() throws Exception {
    Service service = Service.create(new URL("http://localhost:" + port + "/webservice-attachments/AttachmentImpl?wsdl"), new QName("http://superbiz.org/wsdl", "AttachmentWsService"));
    assertNotNull(service);
    AttachmentWs ws = service.getPort(AttachmentWs.class);
    // retrieve the SOAPBinding
    SOAPBinding binding = (SOAPBinding) ((BindingProvider) ws).getBinding();
    binding.setMTOMEnabled(true);
    String request = "tsztelak@gmail.com";
    // Byte array
    String response = ws.stringFromBytes(request.getBytes());
    assertEquals(request, response);
    // Data Source
    DataSource source = new ByteArrayDataSource(request.getBytes(), "text/plain; charset=UTF-8");
    // not yet supported !
    //        response = ws.stringFromDataSource(source);
    //        assertEquals(request, response);
    // Data Handler
    response = ws.stringFromDataHandler(new DataHandler(source));
    assertEquals(request, response);
}
Also used : QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) SOAPBinding(javax.xml.ws.soap.SOAPBinding) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) URL(java.net.URL) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 43 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class AttachmentsTest method testDataHandlerStreaming.

/**
     * Tests that a call to {@link DataHandlerExt#readOnce()} on a {@link DataHandler} returned by
     * the {@link Attachments} object streams the content of the MIME part.
     * 
     * @throws Exception
     */
public void testDataHandlerStreaming() throws Exception {
    // Note: We are only interested in the MimeMultipart, but we need to create a
    //       MimeMessage to be able to calculate the correct content type
    MimeMessage message = new MimeMessage((Session) null);
    final MimeMultipart mp = new MimeMultipart("related");
    // Prepare the "SOAP" part
    MimeBodyPart bp1 = new MimeBodyPart();
    // Obviously this is not SOAP, but this is irrelevant for this test
    bp1.setText("<root/>", "utf-8", "xml");
    bp1.addHeader("Content-Transfer-Encoding", "binary");
    bp1.addHeader("Content-ID", "part1@apache.org");
    mp.addBodyPart(bp1);
    // Create an attachment that is larger than the maximum heap
    DataSource dataSource = new RandomDataSource((int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE));
    MimeBodyPart bp2 = new MimeBodyPart();
    bp2.setDataHandler(new DataHandler(dataSource));
    bp2.addHeader("Content-Transfer-Encoding", "binary");
    bp2.addHeader("Content-ID", "part2@apache.org");
    mp.addBodyPart(bp2);
    message.setContent(mp);
    // Compute the correct content type
    message.saveChanges();
    // We use a pipe (with a producer running in a separate thread) because obviously we can't
    // store the multipart in memory.
    final PipedOutputStream pipeOut = new PipedOutputStream();
    PipedInputStream pipeIn = new PipedInputStream(pipeOut);
    Thread producerThread = new Thread(new Runnable() {

        public void run() {
            try {
                try {
                    mp.writeTo(pipeOut);
                } finally {
                    pipeOut.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    producerThread.start();
    try {
        // We configure Attachments to buffer MIME parts in memory. If the part content is not
        // streamed, then this will result in an OOM error.
        Attachments attachments = new Attachments(pipeIn, message.getContentType());
        DataHandlerExt dh = (DataHandlerExt) attachments.getDataHandler("part2@apache.org");
        IOTestUtils.compareStreams(dataSource.getInputStream(), dh.readOnce());
    } finally {
        pipeIn.close();
    }
}
Also used : RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) PipedOutputStream(java.io.PipedOutputStream) DataHandlerExt(org.apache.axiom.attachments.lifecycle.DataHandlerExt) DataHandler(javax.activation.DataHandler) PipedInputStream(java.io.PipedInputStream) MIMEException(org.apache.axiom.mime.MIMEException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SizeAwareDataSource(org.apache.axiom.ext.activation.SizeAwareDataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 44 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class AttachmentsTest method testGetSizeOnDataSource.

private void testGetSizeOnDataSource(boolean useFiles) throws Exception {
    InputStream in = MTOMSample.SAMPLE1.getInputStream();
    try {
        Attachments attachments;
        if (useFiles) {
            attachments = new Attachments(in, MTOMSample.SAMPLE1.getContentType(), true, getAttachmentsDir(), "4096");
        } else {
            attachments = new Attachments(in, MTOMSample.SAMPLE1.getContentType());
        }
        DataHandler dh = attachments.getDataHandler("2.urn:uuid:A3ADBAEE51A1A87B2A11443668160994@apache.org");
        DataSource ds = dh.getDataSource();
        assertTrue(ds instanceof SizeAwareDataSource);
        assertEquals(13887, ((SizeAwareDataSource) ds).getSize());
    } finally {
        in.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) ExceptionInputStream(org.apache.axiom.testutils.io.ExceptionInputStream) InputStream(java.io.InputStream) SizeAwareDataSource(org.apache.axiom.ext.activation.SizeAwareDataSource) DataHandler(javax.activation.DataHandler) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) SizeAwareDataSource(org.apache.axiom.ext.activation.SizeAwareDataSource) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 45 with DataSource

use of javax.activation.DataSource in project webservices-axiom by apache.

the class TestReadAttachmentBeforeRootPartComplete method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    // Programmatically create the message
    OMElement orgRoot = factory.createOMElement("root", null);
    OMElement orgChild1 = factory.createOMElement("child1", null, orgRoot);
    DataSource ds = new RandomDataSource(54321, 4096);
    orgChild1.addChild(factory.createOMText(new DataHandler(ds), true));
    // Create a child with a large text content and insert it after the binary node.
    // If we don't do this, then the root part may be buffered entirely by the parser,
    // and the test would not be effective.
    OMElement orgChild2 = factory.createOMElement("child2", null, orgRoot);
    String s = RandomUtils.randomString(128 * 1024);
    orgChild2.setText(s);
    // Serialize the message
    OMOutputFormat format = new OMOutputFormat();
    format.setDoOptimize(true);
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    orgRoot.serialize(out, format);
    out.close();
    // Parse the message
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.NON_COALESCING, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(format.getContentType()).build());
    OMElement root = builder.getDocumentElement();
    OMElement child1 = (OMElement) root.getFirstOMChild();
    OMText text = (OMText) child1.getFirstOMChild();
    assertTrue(text.isBinary());
    // Access the DataHandler
    DataHandler dh = text.getDataHandler();
    IOTestUtils.compareStreams(ds.getInputStream(), dh.getInputStream());
    OMElement child2 = (OMElement) child1.getNextOMSibling();
    assertFalse(child2.isComplete());
    assertEquals(s, child2.getText());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Aggregations

DataSource (javax.activation.DataSource)53 DataHandler (javax.activation.DataHandler)31 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)16 InputStream (java.io.InputStream)12 MimeMultipart (javax.mail.internet.MimeMultipart)11 OMFactory (org.apache.axiom.om.OMFactory)11 RandomDataSource (org.apache.axiom.testutils.activation.RandomDataSource)11 MimeBodyPart (javax.mail.internet.MimeBodyPart)10 IOException (java.io.IOException)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 FileDataSource (javax.activation.FileDataSource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 OMElement (org.apache.axiom.om.OMElement)7 File (java.io.File)6 Reader (java.io.Reader)5 QName (javax.xml.namespace.QName)5 MimeHandlerException (com.zimbra.cs.mime.MimeHandlerException)4 Document (ihe.iti.xds_b._2007.ProvideAndRegisterDocumentSetRequestType.Document)4 URI (java.net.URI)4 Multipart (javax.mail.Multipart)4