Search in sources :

Example 31 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project eclipselink by eclipse-ee4j.

the class BinaryDataSelfTestCases method getReadControlObject.

@Override
public Object getReadControlObject() {
    Employee emp = new Employee(123);
    byte[] bytes = new byte[] { 0, 1, 2, 3 };
    emp.setPhoto(bytes);
    ByteArrayDataSource ds = new ByteArrayDataSource(bytes, "application/octet-stream");
    DataHandler dh = new DataHandler(ds);
    emp.setData(dh);
    return emp;
}
Also used : DataHandler(jakarta.activation.DataHandler) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 32 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project Gene by Nervousync.

the class MailUtils method convert.

private static MimeMessage convert(Session session, MailObject mailObject) throws MessagingException {
    MimeMessage message = new MimeMessage(session);
    message.setSubject(mailObject.getSubject(), mailObject.getCharset());
    MimeMultipart mimeMultipart = new MimeMultipart();
    if (mailObject.getAttachFiles() != null) {
        for (String attachment : mailObject.getAttachFiles()) {
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            File file;
            try {
                file = FileUtils.getFile(attachment);
            } catch (FileNotFoundException e) {
                throw new MessagingException("Attachment file not found! ", e);
            }
            DataSource dataSource = new FileDataSource(file);
            mimeBodyPart.setFileName(StringUtils.getFilename(attachment));
            mimeBodyPart.setDataHandler(new DataHandler(dataSource));
            mimeMultipart.addBodyPart(mimeBodyPart, mimeMultipart.getCount());
        }
    }
    if (mailObject.getIncludeFiles() != null) {
        List<String> includeFiles = mailObject.getIncludeFiles();
        for (String filePath : includeFiles) {
            File file;
            MimeBodyPart mimeBodyPart;
            try {
                file = FileUtils.getFile(filePath);
                String fileName = StringUtils.getFilename(filePath);
                mimeBodyPart = new MimeBodyPart();
                DataHandler dataHandler = new DataHandler(new ByteArrayDataSource(file.toURI().toURL().openStream(), "application/octet-stream"));
                mimeBodyPart.setDataHandler(dataHandler);
                mimeBodyPart.setFileName(fileName);
                mimeBodyPart.setHeader("Content-ID", fileName);
            } catch (Exception e) {
                throw new MessagingException("Process include file error! ", e);
            }
            mimeMultipart.addBodyPart(mimeBodyPart, mimeMultipart.getCount());
        }
    }
    if (mailObject.getContent() != null) {
        String content = mailObject.getContent();
        if (mailObject.getContentMap() != null) {
            Map<String, String> argsMap = mailObject.getContentMap();
            for (Map.Entry<String, String> entry : argsMap.entrySet()) {
                content = StringUtils.replace(content, "###" + entry.getKey() + "###", entry.getValue());
            }
        }
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(content, mailObject.getContentType() + "; charset=" + mailObject.getCharset());
        mimeMultipart.addBodyPart(mimeBodyPart, mimeMultipart.getCount());
    }
    message.setContent(mimeMultipart);
    message.setFrom(new InternetAddress(mailObject.getSendAddress()));
    if (mailObject.getReceiveAddress() == null || mailObject.getReceiveAddress().isEmpty()) {
        throw new MessagingException("Unknown receive address");
    }
    StringBuilder receiveAddress = new StringBuilder();
    mailObject.getReceiveAddress().forEach(address -> receiveAddress.append(",").append(address));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiveAddress.substring(1)));
    if (mailObject.getCcAddress() != null && !mailObject.getCcAddress().isEmpty()) {
        StringBuilder ccAddress = new StringBuilder();
        mailObject.getCcAddress().forEach(address -> ccAddress.append(",").append(address));
        message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccAddress.substring(1)));
    }
    if (mailObject.getBccAddress() != null && !mailObject.getBccAddress().isEmpty()) {
        StringBuilder bccAddress = new StringBuilder();
        mailObject.getBccAddress().forEach(address -> bccAddress.append(",").append(address));
        message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccAddress.substring(1)));
    }
    if (mailObject.getReplyAddress() != null && !mailObject.getReplyAddress().isEmpty()) {
        StringBuilder replyAddress = new StringBuilder();
        mailObject.getReplyAddress().forEach(address -> replyAddress.append(",").append(address));
        message.setReplyTo(InternetAddress.parse(replyAddress.substring(1)));
    } else {
        message.setReplyTo(InternetAddress.parse(mailObject.getSendAddress()));
    }
    message.setSentDate(mailObject.getSendDate() == null ? new Date() : mailObject.getSendDate());
    return message;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) DataHandler(jakarta.activation.DataHandler) BuilderException(org.nervousync.exceptions.builder.BuilderException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) FileDataSource(jakarta.activation.FileDataSource) DataSource(jakarta.activation.DataSource) FileDataSource(jakarta.activation.FileDataSource) File(java.io.File) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource)

Example 33 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project sling-org-apache-sling-clam by apache.

the class MimeMessageParser method createDataSource.

/**
 * Parses the MimePart to create a DataSource.
 *
 * @param parent the parent multi-part
 * @param part   the current part to be processed
 * @return the DataSource
 * @throws MessagingException creating the DataSource failed
 * @throws IOException        creating the DataSource failed
 */
protected DataSource createDataSource(final Multipart parent, final MimePart part) throws MessagingException, IOException {
    final DataHandler dataHandler = part.getDataHandler();
    final DataSource dataSource = dataHandler.getDataSource();
    final String contentType = getBaseMimeType(dataSource.getContentType());
    final byte[] content = this.getContent(dataSource.getInputStream());
    final ByteArrayDataSource result = new ByteArrayDataSource(content, contentType);
    final String dataSourceName = getDataSourceName(part, dataSource);
    result.setName(dataSourceName);
    return result;
}
Also used : DataHandler(jakarta.activation.DataHandler) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) DataSource(jakarta.activation.DataSource)

Example 34 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project resteasy by resteasy.

the class HeaderFlushedOutputStreamTest method testPost.

/**
 * @tpTestDetails Loopback to examine form-data
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testPost() throws Exception {
    // prepare file
    File file = new File(testFilePath);
    Assert.assertTrue("File " + testFilePath + " doesn't exists", file.exists());
    // test logic
    MultipartOutput mpo = new MultipartOutput();
    mpo.addPart("This is Value 1", MediaType.TEXT_PLAIN_TYPE);
    mpo.addPart("This is Value 2", MediaType.TEXT_PLAIN_TYPE);
    mpo.addPart(file, MediaType.TEXT_PLAIN_TYPE);
    Response response = client.target(TEST_URI).request().post(Entity.entity(mpo, MediaType.MULTIPART_FORM_DATA_TYPE));
    BufferedInputStream in = new BufferedInputStream(response.readEntity(InputStream.class));
    String contentType = response.getHeaderString("content-type");
    ByteArrayDataSource ds = new ByteArrayDataSource(in, contentType);
    MimeMultipart mimeMultipart = new MimeMultipart(ds);
    Assert.assertEquals("Wrong count of parts of response", mimeMultipart.getCount(), 3);
    response.close();
}
Also used : MultipartOutput(org.jboss.resteasy.plugins.providers.multipart.MultipartOutput) Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) BufferedInputStream(java.io.BufferedInputStream) MimeMultipart(jakarta.mail.internet.MimeMultipart) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) File(java.io.File) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Example 35 with ByteArrayDataSource

use of jakarta.mail.util.ByteArrayDataSource in project resteasy by resteasy.

the class HeaderFlushedOutputStreamTest method testGet.

/**
 * @tpTestDetails Test get method
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testGet() throws Exception {
    Response response = client.target(TEST_URI).request().get();
    Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    BufferedInputStream in = new BufferedInputStream(response.readEntity(InputStream.class));
    String contentType = response.getHeaderString("content-type");
    ByteArrayDataSource ds = new ByteArrayDataSource(in, contentType);
    MimeMultipart mimeMultipart = new MimeMultipart(ds);
    Assert.assertEquals("Wrong count of parts of response", mimeMultipart.getCount(), 1);
    BodyPart part = mimeMultipart.getBodyPart(0);
    InputStream is = part.getInputStream();
    Assert.assertEquals("Wrong count of parts of response", 3, part.getSize());
    char[] output = new char[3];
    output[0] = (char) is.read();
    output[1] = (char) is.read();
    output[2] = (char) is.read();
    String str = new String(output);
    Assert.assertEquals("Wrong content of first part of response", "bla", str);
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) BodyPart(jakarta.mail.BodyPart) BufferedInputStream(java.io.BufferedInputStream) MimeMultipart(jakarta.mail.internet.MimeMultipart) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ByteArrayDataSource(jakarta.mail.util.ByteArrayDataSource) Test(org.junit.Test)

Aggregations

ByteArrayDataSource (jakarta.mail.util.ByteArrayDataSource)35 Test (org.junit.Test)16 MimeMultipart (jakarta.mail.internet.MimeMultipart)11 DataHandler (jakarta.activation.DataHandler)8 InputStream (java.io.InputStream)7 MimeMessage (jakarta.mail.internet.MimeMessage)6 DataSource (jakarta.activation.DataSource)5 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)5 BufferedInputStream (java.io.BufferedInputStream)5 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)4 Response (jakarta.ws.rs.core.Response)4 Email (org.simplejavamail.api.email.Email)4 MessagingException (jakarta.mail.MessagingException)3 File (java.io.File)3 IOException (java.io.IOException)3 NamedDataSource (org.simplejavamail.internal.util.NamedDataSource)3 BodyPart (jakarta.mail.BodyPart)2 Multipart (jakarta.mail.Multipart)2 InternetAddress (jakarta.mail.internet.InternetAddress)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2