Example usage for javax.mail.internet MimeBodyPart getInputStream

List of usage examples for javax.mail.internet MimeBodyPart getInputStream

Introduction

In this page you can find the example usage for javax.mail.internet MimeBodyPart getInputStream.

Prototype

@Override
public InputStream getInputStream() throws IOException, MessagingException 

Source Link

Document

Return a decoded input stream for this body part's "content".

Usage

From source file:com.adaptris.util.text.mime.PartIteratorCase.java

protected static String toString(MimeBodyPart p) throws Exception {
    StringWriter out = new StringWriter();
    StreamUtil.copyAndClose(p.getInputStream(), out);
    return out.toString();
}

From source file:org.jevis.emaildatasource.EMailManager.java

private static InputStream toInputStream(MimeBodyPart part) {
    byte[] bytes = null;
    try {//w ww .  j a  v  a  2  s .c om
        bytes = IOUtils.toByteArray(part.getInputStream());

    } catch (IOException | MessagingException ex) {
        Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE, "Unable to pack a file in inputstream",
                ex);
    }
    InputStream answer = new ByteArrayInputStream(bytes);
    //        InputStream inputStream = new ByteArrayInputStream(bytes);
    //InputStream answer = new BufferedInputStream(inputStream);
    return answer;
}

From source file:com.adaptris.core.mail.RawMailConsumerTest.java

private void compare(AdaptrisMessage msg, String expected) throws Exception {
    try (InputStream msgIn = msg.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        BodyPartIterator mime = MimeHelper.createBodyPartIterator(msg);
        MimeBodyPart part = (MimeBodyPart) mime.next();
        try (InputStream partIn = part.getInputStream(); OutputStream bout = out) {
            IOUtils.copy(partIn, bout);/*from ww w.  j  av  a  2  s. co m*/
        }
        assertEquals(expected, out.toString());
    }

}

From source file:com.adaptris.core.services.splitter.MimePartSplitter.java

private void copy(MimeBodyPart src, AdaptrisMessage dest) throws IOException, MessagingException {
    StreamUtil.copyAndClose(src.getInputStream(), dest.getOutputStream());
    copyHeaders(src, dest);/*www  .ja v  a  2s  . c  o  m*/
}

From source file:org.apache.james.transport.mailets.MimeDecodingMailet.java

private Optional<byte[]> extractContent(Object rawMime) throws MessagingException {
    try {/*from  w w w. j  a va  2s  .  co m*/
        MimeBodyPart mimeBodyPart = new MimeBodyPart(new ByteArrayInputStream((byte[]) rawMime));
        return Optional.fromNullable(IOUtils.toByteArray(mimeBodyPart.getInputStream()));
    } catch (IOException e) {
        log("Error while extracting content from mime part", e);
        return Optional.absent();
    } catch (ClassCastException e) {
        log("Invalid map attribute types.", e);
        return Optional.absent();
    }
}

From source file:com.adaptris.core.services.dynamic.MimeServiceExtractor.java

@Override
public InputStream getInputStream(AdaptrisMessage m) throws ServiceException, IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = null;/*w  w w  .  j  a  va 2s  . com*/
    if (selector == null) {
        throw new ServiceException("No Selector, impossible to extract service");
    }
    try {
        MimeBodyPart part = selector.select(MimeHelper.create(m, false));
        if (part != null) {
            out = new ByteArrayOutputStream();
            in = part.getInputStream();
            IOUtils.copy(in, out);
        } else {
            throw new ServiceException("Could not select a part");
        }
    } catch (MessagingException e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
    return new ByteArrayInputStream(out.toByteArray());
}

From source file:org.apache.james.transport.mailets.AmqpForwardAttribute.java

private byte[] extractContent(byte[] rawMime) throws IOException {
    try {/*from  w ww  .  j  a v  a 2s  . co m*/
        MimeBodyPart mimeBodyPart = new MimeBodyPart(new ByteArrayInputStream(rawMime));
        return IOUtils.toByteArray(mimeBodyPart.getInputStream());
    } catch (MessagingException e) {
        throw new IOException(e);
    }
}

From source file:com.adaptris.util.TestMultipartInput.java

public void testSingleMultipartSelectBodyPartByContentId() throws Exception {
    MultiPartOutput output = new MultiPartOutput(guid.getUUID());
    String contentId = guid.getUUID();
    byte[] payload = buildPayload();
    output.addPart(payload, ENCODING_BASE64, contentId);
    byte[] mimePayload = output.getBytes();

    MultiPartInput input = new MultiPartInput(mimePayload);
    assertEquals(NUMBER_OF_PARTS, input.size(), 1);

    MimeBodyPart bodyPart = input.getBodyPart(contentId);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamUtil.copyStream(bodyPart.getInputStream(), out);
    verifyProperties(new ByteArrayInputStream(out.toByteArray()));
}

From source file:com.adaptris.util.TestMultipartInput.java

public void testMultiPartInputSelectBodyPartByPosition() throws Exception {
    MultiPartOutput output = new MultiPartOutput(guid.getUUID());
    String contentId = guid.getUUID();
    byte[] payload = buildPayload();
    output.addPart(payload, ENCODING_BASE64, contentId);
    String contentId2 = guid.getUUID();
    output.addPart(PAYLOAD_1, ENCODING_8BIT, contentId2);

    MultiPartInput input = new MultiPartInput(output.getBytes(), false);
    assertEquals(NUMBER_OF_PARTS, input.size(), 2);

    MimeBodyPart bodyPart = input.getBodyPart(0);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamUtil.copyStream(bodyPart.getInputStream(), out);
    verifyProperties(new ByteArrayInputStream(out.toByteArray()));

    bodyPart = input.getBodyPart(1);/*  w w  w.ja  v  a  2  s  .  c o  m*/
    out = new ByteArrayOutputStream();
    StreamUtil.copyStream(bodyPart.getInputStream(), out);
    out.flush();
    assertEquals(TXT_COMPARE_PAYLOADS, PAYLOAD_1, out.toString());
    assertTrue(input.getBodyPart(10) == null);
}

From source file:com.adaptris.util.TestMultipartInput.java

public void testMultiPartInputSelectBodyPartByContentID() throws Exception {
    MultiPartOutput output = new MultiPartOutput(guid.getUUID());
    String contentId = guid.getUUID();
    byte[] payload = buildPayload();
    output.addPart(payload, ENCODING_BASE64, contentId);
    String contentId2 = guid.getUUID();
    output.addPart(PAYLOAD_1, ENCODING_8BIT, contentId2);

    MultiPartInput input = new MultiPartInput(output.getBytes(), false);
    assertEquals(NUMBER_OF_PARTS, input.size(), 2);

    MimeBodyPart bodyPart = input.getBodyPart(contentId);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamUtil.copyStream(bodyPart.getInputStream(), out);
    verifyProperties(new ByteArrayInputStream(out.toByteArray()));

    bodyPart = input.getBodyPart(contentId2);
    out = new ByteArrayOutputStream();
    StreamUtil.copyStream(bodyPart.getInputStream(), out);
    out.flush();/*from  w ww  .  ja v a  2 s  . c o m*/
    assertEquals(TXT_COMPARE_PAYLOADS, PAYLOAD_1, out.toString());
}