Search in sources :

Example 71 with DataSource

use of javax.activation.DataSource in project scout.rt by eclipse.

the class MailHelperTest method testDataSourceWithoutFileExtension.

@Test
public void testDataSourceWithoutFileExtension() throws IOException, MessagingException {
    final byte[] sampleData = new byte[] { 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
    final String fileName = "test.file";
    DataSource ds = BEANS.get(MailHelper.class).createDataSource(new ByteArrayInputStream(sampleData), fileName, null);
    assertNotNull(ds);
    assertEquals(fileName, ds.getName());
    assertTrue(ds instanceof ByteArrayDataSource);
    ByteArrayDataSource bds = (ByteArrayDataSource) ds;
    assertEquals("application/octet-stream", bds.getContentType());
    try (InputStream in = bds.getInputStream()) {
        byte[] data = IOUtility.readBytes(in);
        assertArrayEquals(sampleData, data);
    }
    new MailMessage().withBodyPlainText("test").withAttachment(new MailAttachment(ds));
    MimeMessage message = BEANS.get(MailHelper.class).createMimeMessage(new MailMessage().withBodyPlainText("test"));
    message.writeTo(new ByteArrayOutputStream());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource) Test(org.junit.Test)

Example 72 with DataSource

use of javax.activation.DataSource in project scout.rt by eclipse.

the class MailHelper method addAttachmentsToMimeMessage.

/**
 * Adds the provided attachments to the existing mime message.
 * <p>
 * When working with {@link BinaryResource}, use {@link #addResourcesAsAttachments(MimeMessage, List)} instead.
 *
 * @param msg
 *          Mime message to attach files to
 * @param attachments
 *          List of attachments (files).
 * @since 4.1
 */
public void addAttachmentsToMimeMessage(MimeMessage msg, List<File> attachments) {
    if (CollectionUtility.isEmpty(attachments)) {
        return;
    }
    try {
        Multipart multiPart = prepareMessageForAttachments(msg);
        for (File attachment : attachments) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachment);
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText(attachment.getName(), "UTF-8", null));
            multiPart.addBodyPart(bodyPart);
        }
        msg.saveChanges();
    } catch (MessagingException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    } catch (IOException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 73 with DataSource

use of javax.activation.DataSource in project scout.rt by eclipse.

the class MailHelper method addResourcesAsAttachments.

/**
 * Adds the provided attachments to the existing mime message.
 * <p>
 * When working with {@link File}, use {@link #addAttachmentsToMimeMessage(MimeMessage, List)} instead.
 *
 * @param msg
 *          Mime message to attach files to
 * @param attachments
 *          List of attachments (binary resources).
 * @since 6.0
 */
public void addResourcesAsAttachments(MimeMessage msg, List<BinaryResource> attachments) {
    if (CollectionUtility.isEmpty(attachments)) {
        return;
    }
    try {
        Multipart multiPart = prepareMessageForAttachments(msg);
        for (BinaryResource attachment : attachments) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            DataSource source = new BinaryResourceDataSource(attachment);
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText(attachment.getFilename(), "UTF-8", null));
            multiPart.addBodyPart(bodyPart);
        }
        msg.saveChanges();
    } catch (MessagingException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    } catch (IOException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 74 with DataSource

use of javax.activation.DataSource in project jbossws-cxf by jbossws.

the class JBWS2259TestCase method testCall.

@Test
@RunAsClient
public void testCall() throws Exception {
    URL wsdlURL = new URL(baseURL + "?wsdl");
    QName serviceName = new QName("http://ws.jboss.org/jbws2259", "EndpointService");
    Service service = Service.create(wsdlURL, serviceName);
    Endpoint port = service.getPort(Endpoint.class);
    BindingProvider bindingProvider = (BindingProvider) port;
    SOAPBinding soapBinding = (SOAPBinding) bindingProvider.getBinding();
    soapBinding.setMTOMEnabled(true);
    File sharkFile = getResourceFile("jaxws/jbws2259/attach.jpeg");
    DataSource ds = new FileDataSource(sharkFile);
    DataHandler handler = new DataHandler(ds);
    String expectedContentType = "image/jpeg";
    Photo p = new Photo();
    p.setCaption("JBWS2259 Smile :-)");
    p.setExpectedContentType(expectedContentType);
    p.setImage(handler);
    Photo reponse = port.echo(p);
    DataHandler dhResponse = reponse.getImage();
    String contentType = dhResponse.getContentType();
    assertEquals("content-type", expectedContentType, contentType);
}
Also used : QName(javax.xml.namespace.QName) FileDataSource(javax.activation.FileDataSource) Service(javax.xml.ws.Service) SOAPBinding(javax.xml.ws.soap.SOAPBinding) BindingProvider(javax.xml.ws.BindingProvider) DataHandler(javax.activation.DataHandler) File(java.io.File) URL(java.net.URL) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Test(org.junit.Test) JBossWSTest(org.jboss.wsf.test.JBossWSTest)

Example 75 with DataSource

use of javax.activation.DataSource in project mule by mulesoft.

the class TransformerUtils method checkTransformerReturnClass.

/**
 * Checks whether a given value is a valid output for a transformer.
 *
 * @param transformer the transformer used to validate
 * @param value the output value
 * @throws TransformerException if the output value is of a unexpected type.
 */
public static void checkTransformerReturnClass(Transformer transformer, Object value) throws TransformerException {
    if (value == null && (transformer instanceof AbstractTransformer && ((AbstractTransformer) transformer).isAllowNullReturn())) {
        return;
    }
    if (transformer.getReturnDataType() != null) {
        DataTypeParamsBuilder dtBuilder = DataType.builder().fromObject(value);
        if (!(value instanceof DataHandler) && !(value instanceof DataSource)) {
            // To avoid getting an error because the DataType was constructed with a default mediaType
            dtBuilder = dtBuilder.mediaType(transformer.getReturnDataType().getMediaType());
        }
        DataType dt = dtBuilder.build();
        if (!transformer.getReturnDataType().isCompatibleWith(dt)) {
            throw new TransformerException(transformUnexpectedType(dt, transformer.getReturnDataType()), transformer);
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("The transformed value is of expected type. Type is: " + ClassUtils.getSimpleName(value.getClass()));
    }
}
Also used : AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) DataType(org.mule.runtime.api.metadata.DataType) DataHandler(javax.activation.DataHandler) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) DataSource(javax.activation.DataSource)

Aggregations

DataSource (javax.activation.DataSource)196 DataHandler (javax.activation.DataHandler)114 FileDataSource (javax.activation.FileDataSource)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)53 MimeMultipart (javax.mail.internet.MimeMultipart)48 MimeMessage (javax.mail.internet.MimeMessage)39 InputStream (java.io.InputStream)37 IOException (java.io.IOException)36 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)35 ByteArrayInputStream (java.io.ByteArrayInputStream)33 File (java.io.File)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)30 InternetAddress (javax.mail.internet.InternetAddress)27 Properties (java.util.Properties)26 MessagingException (javax.mail.MessagingException)25 Multipart (javax.mail.Multipart)24 Session (javax.mail.Session)21 BodyPart (javax.mail.BodyPart)19 ArrayList (java.util.ArrayList)16