Example usage for javax.mail.internet MimeBodyPart getContentID

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

Introduction

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

Prototype

@Override
public String getContentID() throws MessagingException 

Source Link

Document

Returns the value of the "Content-ID" header field.

Usage

From source file:org.xwiki.contrib.mailarchive.xwiki.internal.XWikiPersistence.java

private int addAttachmentsToMailPage(final XWikiDocument doc1, final ArrayList<MimeBodyPart> bodyparts,
        final HashMap<String, String> attachmentsMap) throws MessagingException {
    int nb = 0;/*  w  ww. ja va 2s .c om*/
    for (MimeBodyPart bodypart : bodyparts) {
        String fileName = bodypart.getFileName();
        String cid = bodypart.getContentID();

        try {
            // replace by correct name if filename was renamed (multiple attachments with same name)
            if (attachmentsMap.containsKey(cid)) {
                fileName = attachmentsMap.get(cid);
            }
            logger.debug("Treating attachment: " + fileName + " with contentid " + cid);
            if (fileName == null) {
                fileName = "fichier.doc";
            }
            if (fileName.equals("oledata.mso") || fileName.endsWith(".wmz") || fileName.endsWith(".emz")) {
                logger.debug("Not treating Microsoft crap !");
            } else {
                String disposition = bodypart.getDisposition();
                String contentType = bodypart.getContentType().toLowerCase();

                logger.debug("Treating attachment of type: " + contentType);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                OutputStream out = new BufferedOutputStream(baos);
                // We can't just use p.writeTo() here because it doesn't
                // decode the attachment. Instead we copy the input stream
                // onto the output stream which does automatically decode
                // Base-64, quoted printable, and a variety of other formats.
                InputStream ins = new BufferedInputStream(bodypart.getInputStream());
                int b = ins.read();
                while (b != -1) {
                    out.write(b);
                    b = ins.read();
                }
                out.flush();
                out.close();
                ins.close();

                logger.debug("Treating attachment step 3: " + fileName);

                byte[] data = baos.toByteArray();
                logger.debug("Ready to attach attachment: " + fileName);
                addAttachmentToPage(doc1, fileName, data);
                nb++;
            } // end if
        } catch (Exception e) {
            logger.warn("Attachment " + fileName + " could not be treated", e);
        }
    } // end for all attachments
    return nb;
}

From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactoryTest.java

@Test
public void createAttachmentBodyPart() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));

    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");

    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment,
            Collections.<String, Object>emptyMap());

    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=attachment8219195155963823979.tmp) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify the format of the generated temporary file containing our attachment data
    assertTrue(part.getFileName().matches("attachment.*\\.tmp"));

    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}

From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactoryTest.java

@Test
public void createAttachmentBodyPartWithHeader() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));

    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");

    Map<String, Object> parameters = Collections.singletonMap("headers",
            (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable"));

    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters);

    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=attachment8219195155963823979.tmp) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify the format of the generated temporary file containing our attachment data
    assertTrue(part.getFileName().matches("attachment.*\\.tmp"));

    assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding"));

    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}

From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactoryTest.java

@Test
public void createAttachmentBodyPart() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));

    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");

    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment,
            Collections.<String, Object>emptyMap());

    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify that the Content-Disposition has the correct file namr
    assertTrue(part.getFileName().matches("image\\.png"));

    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}

From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactoryTest.java

@Test
public void createAttachmentBodyPartWithHeader() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));

    Attachment attachment = mock(Attachment.class);
    when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
    when(attachment.getFilename()).thenReturn("image.png");
    when(attachment.getMimeType()).thenReturn("image/png");

    Map<String, Object> parameters = Collections.singletonMap("headers",
            (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable"));

    MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters);

    assertEquals("<image.png>", part.getContentID());
    // JavaMail adds some extra params to the content-type header
    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
    assertTrue(part.getContentType().startsWith("image/png"));
    // We verify that the Content-Disposition has the correct file namr
    assertTrue(part.getFileName().matches("image\\.png"));

    assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding"));

    assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}