Example usage for javax.mail BodyPart isMimeType

List of usage examples for javax.mail BodyPart isMimeType

Introduction

In this page you can find the example usage for javax.mail BodyPart isMimeType.

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

Usage

From source file:mitm.application.djigzo.ca.PFXMailBuilderTest.java

@Test
public void testReplacePFXSendSMSTrue() throws Exception {
    byte[] pfx = IOUtils.toByteArray(new FileInputStream(testPFX));

    PFXMailBuilder builder = new PFXMailBuilder(IOUtils.toString(new FileInputStream(templateFile)),
            templateBuilder);// w  w  w .j a v a  2  s .co m

    String from = "123@test.com";

    builder.setFrom(new InternetAddress(from, "test user"));
    builder.setPFX(pfx);
    builder.addProperty("sendSMS", true);
    builder.addProperty("phoneNumberAnonymized", "1234***");
    builder.addProperty("id", "0987");

    MimeMessage message = builder.createMessage();

    assertNotNull(message);

    MailUtils.writeMessage(message, new File(tempDir, "testReplacePFXSendSMSTrue.eml"));

    Multipart mp;

    mp = (Multipart) message.getContent();

    BodyPart textPart = mp.getBodyPart(0);

    assertTrue(textPart.isMimeType("text/plain"));

    String body = (String) textPart.getContent();

    assertTrue(body.contains("was sent to you by SMS"));

    /*
     * Check if the PFX has really been replaced
     */
    byte[] newPFX = getPFX(message);

    KeyStore keyStore = SecurityFactoryFactory.getSecurityFactory().createKeyStore("PKCS12");

    keyStore.load(new ByteArrayInputStream(newPFX), "test".toCharArray());

    assertEquals(22, keyStore.size());
}

From source file:gov.nih.nci.cacis.nav.DefaultNotificationValidator.java

private void validateTextBodyPart(BodyPart bodyPart) throws NotificationValidationException {
    try {/*from   w ww.ja v a 2s . co  m*/
        if (!bodyPart.isMimeType("text/plain")) {
            throw new NotificationValidationException(ERR_INVALID_TEXT_PART_MSG);
        }
    } catch (MessagingException e) {
        throw new NotificationValidationException(ERR_INVALID_MULTIPART_MSG, e);
    }
}

From source file:io.lavagna.service.MailTicketService.java

private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException {
    String result = "";
    int count = mimeMultipart.getCount();
    for (int i = 0; i < count; i++) {
        BodyPart bodyPart = mimeMultipart.getBodyPart(i);
        if (bodyPart.isMimeType("text/plain")) {
            result = result + "\n" + bodyPart.getContent();
            break;
        } else if (bodyPart.isMimeType("text/html")) {
            String html = (String) bodyPart.getContent();
            result = result + "\n" + Jsoup.parse(html).text();
        } else if (bodyPart.getContent() instanceof MimeMultipart) {
            result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
        }//w  w w  .j  a va2 s .  com
    }
    return result;
}

From source file:mitm.application.djigzo.relay.RelayHandler.java

private void extractMessageParts(MimeMessage message) throws MessagingException, IOException {
    /*/*from   w ww  .  j a va  2 s  .  c  om*/
     * Fast fail. Only multipart mixed messages are supported. 
     */
    if (!message.isMimeType("multipart/mixed")) {
        return;
    }

    Multipart mp;

    try {
        mp = (Multipart) message.getContent();
    } catch (IOException e) {
        throw new MessagingException("Error getting message content.", e);
    }

    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (part.isMimeType("message/rfc822")) {
            relayMessage = BodyPartUtils.extractFromRFC822(part);
        } else if (part.isMimeType("text/xml")) {
            metaPart = part;
        }

        if (metaPart != null && relayMessage != null) {
            break;
        }
    }
}

From source file:gov.nih.nci.cacis.nav.DefaultNotificationValidator.java

private void validateAttachmentBodyPart(BodyPart bodyPart) throws NotificationValidationException {
    try {/*  w  w  w  .  j  av  a  2  s .  c  o  m*/
        final String disposition = bodyPart.getDisposition();
        if (StringUtils.isEmpty(disposition) || !disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
            throw new NotificationValidationException(ERR_INVALID_ATTMNT_PART_MSG);
        }

        if (!bodyPart.isMimeType("application/xml")) {
            throw new NotificationValidationException(ERR_INVALID_ATTMNT_MIMETYPE_MSG);
        }
        validateAttachmentFileName(bodyPart.getFileName());
    } catch (MessagingException e) {
        throw new NotificationValidationException(ERR_INVALID_MULTIPART_MSG, e);
    }
}

From source file:mitm.application.djigzo.ca.PFXMailBuilder.java

private void replacePFX(MimeMessage message) throws MessagingException {
    Multipart mp;//from  ww  w  .ja  va2s  .c o m

    try {
        mp = (Multipart) message.getContent();
    } catch (IOException e) {
        throw new MessagingException("Error getting message content.", e);
    }

    BodyPart pfxPart = null;

    /*
     * Fallback in case the template does not contain a DjigzoHeader.MARKER
     */
    BodyPart octetStreamPart = null;

    /*
     * Try to find the first attachment with X-Djigzo-Marker attachment header which should be the attachment
     * we should replace (we should replace the content and keep the headers)
     */
    for (int i = 0; i < mp.getCount(); i++) {
        BodyPart part = mp.getBodyPart(i);

        if (ArrayUtils.contains(part.getHeader(DjigzoHeader.MARKER), DjigzoHeader.ATTACHMENT_MARKER_VALUE)) {
            pfxPart = part;

            break;
        }

        /*
         * Fallback scanning for octet-stream in case the template does not contain a DjigzoHeader.MARKER
         */
        if (part.isMimeType("application/octet-stream")) {
            octetStreamPart = part;
        }
    }

    if (pfxPart == null) {
        if (octetStreamPart != null) {
            logger.info("Marker not found. Using ocet-stream instead.");

            /*
             * Use the octet-stream part
             */
            pfxPart = octetStreamPart;
        } else {
            throw new MessagingException("Unable to find the attachment part in the template.");
        }
    }

    pfxPart.setDataHandler(new DataHandler(new ByteArrayDataSource(pfx, "application/octet-stream")));
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testInvalidEncoding() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(
            new File("test/resources/templates/blackberry-smime-adapter-invalid-encoding.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");

    mailet.init(mailetConfig);/*w ww. j  a va  2  s .co m*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

    assertTrue(bp.isMimeType("text/plain"));

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("x-rimdevicesmime.p7m", bp.getFileName());
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testSMIMEAdaptMessage() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(new File("resources/templates/blackberry-smime-adapter.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");

    mailet.init(mailetConfig);/*from   www  . j a v a2  s.c om*/

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testSMIMEAdaptMessage", mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

    assertTrue(bp.isMimeType("text/plain"));

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("x-rimdevicesmime.p7m", bp.getFileName());
}

From source file:mitm.application.djigzo.james.mailets.BlackberrySMIMEAdapterTest.java

@Test
public void testSMIMEAdaptMessageOverDirectLimit() throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SendMailEventListenerImpl listener = new SendMailEventListenerImpl();

    mailetConfig.getMailetContext().setSendMailEventListener(listener);

    Mailet mailet = new BlackberrySMIMEAdapter();

    String template = FileUtils.readFileToString(new File("resources/templates/blackberry-smime-adapter.ftl"));

    autoTransactDelegator.setProperty("test@EXAMPLE.com", "pdfTemplate", template);

    mailetConfig.setInitParameter("log", "starting");
    /* use some dummy template because we must specify the default template */
    mailetConfig.setInitParameter("template", "sms.ftl");
    mailetConfig.setInitParameter("templateProperty", "pdfTemplate");
    mailetConfig.setInitParameter("directSizeLimit", "100");

    mailet.init(mailetConfig);//from  w w w  . ja va  2s.c  o m

    MockMail mail = new MockMail();

    mail.setState(Mail.DEFAULT);

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/signed-opaque-validcertificate.eml"));

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("123@EXAMPLE.com"));

    mail.setRecipients(recipients);

    mail.setSender(new MailAddress("sender@example.com"));

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    TestUtils.saveMessages(tempDir, "testSMIMEAdaptMessageOverDirectLimit", mail.getMessage());

    assertTrue(mail.getMessage().isMimeType("multipart/mixed"));
    assertFalse(mail.getMessage() == message);

    assertEquals("test 1,test 2", mail.getMessage().getHeader("X-Test", ","));

    MimeMultipart mp = (MimeMultipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart bp = mp.getBodyPart(0);

    assertTrue(bp.isMimeType("text/plain"));

    bp = mp.getBodyPart(1);

    assertTrue(bp.isMimeType("application/octet-stream"));
    assertEquals("attachment.smime", bp.getFileName());
}

From source file:com.glaf.mail.business.MailBean.java

/**
 * ??//from   ww w . j  a  v  a 2 s. c  o m
 * 
 * @param part
 * @return
 * @throws MessagingException
 * @throws IOException
 */
public boolean isContainAttch(Part part) throws MessagingException, IOException {
    boolean flag = false;
    if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        int count = multipart.getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bodypart = multipart.getBodyPart(i);
            String dispostion = bodypart.getDisposition();
            if ((dispostion != null)
                    && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) {
                flag = true;
            } else if (bodypart.isMimeType("multipart/*")) {
                flag = isContainAttch(bodypart);
            } else {
                String contentType = bodypart.getContentType();
                if (contentType.toLowerCase().indexOf("appliaction") != -1) {
                    flag = true;
                }
                if (contentType.toLowerCase().indexOf("name") != -1) {
                    flag = true;
                }
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        flag = isContainAttch((Part) part.getContent());
    }

    return flag;
}