Search in sources :

Example 26 with DataHandler

use of javax.activation.DataHandler in project quickstarts by jboss-switchyard.

the class InternalResponseProcessor method process.

@Override
public void process(Exchange exchange) throws Exception {
    String newFileName = "internal-resized-switchyard.jpeg";
    Image input = exchange.getIn().getBody(Image.class);
    exchange.getOut().addAttachment(newFileName, new DataHandler(input, "image/jpeg"));
    exchange.getOut().setBody(newFileName);
}
Also used : DataHandler(javax.activation.DataHandler) Image(java.awt.Image)

Example 27 with DataHandler

use of javax.activation.DataHandler in project orientdb by orientechnologies.

the class OMailPlugin method addAttachment.

/**
   * Adds a file as an attachment to the email's content
   *
   * @param multipart
   * @param filePath
   * @throws MessagingException
   */
private void addAttachment(final Multipart multipart, final String filePath) throws MessagingException {
    MimeBodyPart attachPart = new MimeBodyPart();
    DataSource source = new FileDataSource(filePath);
    attachPart.setDataHandler(new DataHandler(source));
    attachPart.setFileName(new File(filePath).getName());
    multipart.addBodyPart(attachPart);
}
Also used : FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 28 with DataHandler

use of javax.activation.DataHandler in project spring-framework by spring-projects.

the class MimeMessageHelper method addInline.

/**
	 * Add an inline element to the MimeMessage, taking the content from a
	 * {@code javax.activation.DataSource}.
	 * <p>Note that the InputStream returned by the DataSource implementation
	 * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke
	 * {@code getInputStream()} multiple times.
	 * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText};
	 * else, mail readers might not be able to resolve inline references correctly.
	 * @param contentId the content ID to use. Will end up as "Content-ID" header
	 * in the body part, surrounded by angle brackets: e.g. "myId" -> "&lt;myId&gt;".
	 * Can be referenced in HTML source via src="cid:myId" expressions.
	 * @param dataSource the {@code javax.activation.DataSource} to take
	 * the content from, determining the InputStream and the content type
	 * @throws MessagingException in case of errors
	 * @see #addInline(String, java.io.File)
	 * @see #addInline(String, org.springframework.core.io.Resource)
	 */
public void addInline(String contentId, DataSource dataSource) throws MessagingException {
    Assert.notNull(contentId, "Content ID must not be null");
    Assert.notNull(dataSource, "DataSource must not be null");
    MimeBodyPart mimeBodyPart = new MimeBodyPart();
    mimeBodyPart.setDisposition(MimeBodyPart.INLINE);
    // We're using setHeader here to remain compatible with JavaMail 1.2,
    // rather than JavaMail 1.3's setContentID.
    mimeBodyPart.setHeader(HEADER_CONTENT_ID, "<" + contentId + ">");
    mimeBodyPart.setDataHandler(new DataHandler(dataSource));
    getMimeMultipart().addBodyPart(mimeBodyPart);
}
Also used : DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 29 with DataHandler

use of javax.activation.DataHandler in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(BinaryObject.class);
    marshaller.setMtomEnabled(true);
    marshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.convertToXopPackage()).willReturn(true);
    byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
    BinaryObject object = new BinaryObject(bytes, dataHandler);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, new StreamResult(writer), mimeContainer);
    assertTrue("No XML written", writer.toString().length() > 0);
    verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.Test)

Example 30 with DataHandler

use of javax.activation.DataHandler in project camel by apache.

the class BasicMessageFilterTest method producerWithAttachment.

@Test
public void producerWithAttachment() throws Exception {
    exchange.getIn().addAttachment("testAttachment", new DataHandler(this.getClass().getResource("/sampleAttachment.txt")));
    filter.filterProducer(exchange, message);
    Assertions.assertThat(message.getAttachments()).isNotNull().isNotEmpty();
    Assertions.assertThat(message.getAttachment("testAttachment")).isNotNull();
}
Also used : DataHandler(javax.activation.DataHandler) Test(org.junit.Test)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13