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