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;
}
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;
}
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;
}
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();
}
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);
}
Aggregations