Example usage for javax.mail BodyPart getNonMatchingHeaders

List of usage examples for javax.mail BodyPart getNonMatchingHeaders

Introduction

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

Prototype

public Enumeration<Header> getNonMatchingHeaders(String[] header_names) throws MessagingException;

Source Link

Document

Return non-matching headers from this envelope as an Enumeration of Header objects.

Usage

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

public void sign(String signingAlgorithm, String digestOID) throws Exception {
    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    mailetConfig.setInitParameter("algorithmAttribute", "signingAlgo");

    SMIMESign mailet = new SMIMESign();

    mailet.init(mailetConfig);//from w  w w. jav  a2  s.  c  o m

    MockMail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message.eml"));

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("m.brinkers@pobox.com"));

    mail.setRecipients(recipients);

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

    mail.setAttribute("signingAlgo", signingAlgorithm);

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    MailUtils.writeMessage(mail.getMessage(), new File(tempDir, "testDefaultSettings.eml"));

    assertEquals(SMIMEHeader.DETACHED_SIGNATURE_TYPE,
            SMIMEUtils.dissectSigned((Multipart) mail.getMessage().getContent())[1].getContentType());

    SMIMEInspector inspector = new SMIMEInspectorImpl(mail.getMessage(), null, "BC");

    assertEquals(SMIMEType.SIGNED, inspector.getSMIMEType());
    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(mail.getMessage()));
    assertEquals(3, inspector.getSignedInspector().getCertificates().size());
    assertEquals(1, inspector.getSignedInspector().getSigners().size());
    assertEquals(digestOID, inspector.getSignedInspector().getSigners().get(0).getDigestAlgorithmOID());

    // check that no headers are signed. Only a content-type header should be added to the part
    Multipart mp = (Multipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart part = mp.getBodyPart(0);

    Enumeration<?> e = part.getNonMatchingHeaders(new String[] { "content-type" });

    assertFalse(e.hasMoreElements());
}

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

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

    SMIMESign mailet = new SMIMESign();

    mailet.init(mailetConfig);/*w  w w. j av  a  2 s  . c  o  m*/

    MockMail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message.eml"));

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("m.brinkers@pobox.com"));

    mail.setRecipients(recipients);

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

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    MailUtils.writeMessage(mail.getMessage(), new File(tempDir, "testDefaultSettings.eml"));

    assertEquals(SMIMEHeader.DETACHED_SIGNATURE_TYPE,
            SMIMEUtils.dissectSigned((Multipart) mail.getMessage().getContent())[1].getContentType());

    SMIMEInspector inspector = new SMIMEInspectorImpl(mail.getMessage(), null, "BC");

    assertEquals(SMIMEType.SIGNED, inspector.getSMIMEType());
    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(mail.getMessage()));
    assertEquals(3, inspector.getSignedInspector().getCertificates().size());
    assertEquals("1C1C1CF46CC9233B23391A3B9BEF558969567091", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(0), Digest.SHA1));
    assertEquals("D8F8E5B92E651B1E3EF93B5493EACDE4C13AFEE0", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(1), Digest.SHA1));
    assertEquals("69D7FFAF26BD5E9E4F42083BCA077BFAA8398593", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(2), Digest.SHA1));
    assertEquals(1, inspector.getSignedInspector().getSigners().size());
    assertEquals(Digest.SHA1.getOID(),
            inspector.getSignedInspector().getSigners().get(0).getDigestAlgorithmOID());

    // check that no headers are signed. Only a content-type header should be added to the part
    Multipart mp = (Multipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart part = mp.getBodyPart(0);

    Enumeration<?> e = part.getNonMatchingHeaders(new String[] { "content-type" });

    assertFalse(e.hasMoreElements());
}

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

@Test
public void testSignBuildPath() throws Exception {
    AutoTransactDelegator proxy = AutoTransactDelegator.createProxy();

    /*/*w ww .j  av  a  2s . co m*/
     * Find a certificate with critical EMAILPROTECTION extension
     */
    X509CertSelector selector = new X509CertSelector();

    selector.setSerialNumber(BigIntegerUtils.hexDecode("1178C3B653829E895ACB7100EB1F627"));
    selector.setIssuer("EMAILADDRESS=ca@example.com, CN=MITM Test CA, L=Amsterdam, ST=NH, C=NL");

    List<KeyAndCertificate> keyAndCertificates = proxy.getKeyAndCertificates(selector);

    assertEquals(1, keyAndCertificates.size());

    proxy.setUserSigningKeyAndCertificate("test@example.com", keyAndCertificates.get(0));

    MockMailetConfig mailetConfig = new MockMailetConfig("test");

    SMIMESign mailet = new SMIMESign();

    mailet.init(mailetConfig);

    MockMail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message.eml"));

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("recipient@example.com"));

    mail.setRecipients(recipients);

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

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    MailUtils.writeMessage(mail.getMessage(), new File(tempDir, "testDefaultSettings.eml"));

    assertEquals(SMIMEHeader.DETACHED_SIGNATURE_TYPE,
            SMIMEUtils.dissectSigned((Multipart) mail.getMessage().getContent())[1].getContentType());

    SMIMEInspector inspector = new SMIMEInspectorImpl(mail.getMessage(), null, "BC");

    assertEquals(SMIMEType.SIGNED, inspector.getSMIMEType());
    assertEquals(SMIMEHeader.Type.CLEAR_SIGNED, SMIMEHeader.getSMIMEContentType(mail.getMessage()));
    assertEquals(3, inspector.getSignedInspector().getCertificates().size());
    assertEquals("F18CC8973F9AB82A6C47448282849A72416B6DAB", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(0), Digest.SHA1));
    assertEquals("D8F8E5B92E651B1E3EF93B5493EACDE4C13AFEE0", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(1), Digest.SHA1));
    assertEquals("69D7FFAF26BD5E9E4F42083BCA077BFAA8398593", X509CertificateInspector
            .getThumbprint(inspector.getSignedInspector().getCertificates().get(2), Digest.SHA1));
    assertEquals(1, inspector.getSignedInspector().getSigners().size());
    assertEquals(Digest.SHA1.getOID(),
            inspector.getSignedInspector().getSigners().get(0).getDigestAlgorithmOID());

    // check that no headers are signed. Only a content-type header should be added to the part
    Multipart mp = (Multipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart part = mp.getBodyPart(0);

    Enumeration<?> e = part.getNonMatchingHeaders(new String[] { "content-type" });

    assertFalse(e.hasMoreElements());
}

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

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

    SMIMESign mailet = new SMIMESign();

    mailetConfig.setInitParameter("protectedHeader", "subject,to,from");

    mailet.init(mailetConfig);//from  w w w  .  j  av  a 2s. co m

    MockMail mail = new MockMail();

    MimeMessage message = MailUtils.loadMessage(new File(testBase, "mail/simple-text-message.eml"));

    mail.setMessage(message);

    Set<MailAddress> recipients = new HashSet<MailAddress>();

    recipients.add(new MailAddress("m.brinkers@pobox.com"));

    mail.setRecipients(recipients);

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

    mailet.service(mail);

    MailUtils.validateMessage(mail.getMessage());

    MailUtils.writeMessage(mail.getMessage(), new File(tempDir, "testProtectedHeaders.eml"));

    assertEquals(SMIMEHeader.DETACHED_SIGNATURE_TYPE,
            SMIMEUtils.dissectSigned((Multipart) mail.getMessage().getContent())[1].getContentType());

    // check if some headers are signed
    Multipart mp = (Multipart) mail.getMessage().getContent();

    assertEquals(2, mp.getCount());

    BodyPart part = mp.getBodyPart(0);

    // there should be 3 non content-type headers
    Enumeration<?> e = part.getNonMatchingHeaders(new String[] { "content-type" });

    int count = 0;
    while (e.hasMoreElements()) {
        e.nextElement();
        count++;
    }
    assertEquals(3, count);

    assertEquals("test simple message", StringUtils.join(part.getHeader("subject"), ","));
    assertEquals("test@example.com", StringUtils.join(part.getHeader("from"), ","));
    assertEquals("test@example.com", StringUtils.join(part.getHeader("to"), ","));
}