Search in sources :

Example 6 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class Message method extractMimeEntity.

/**
	 * Gets a copy of this message without any non-mime headers.
	 * @returns A copy of this message without any non-mime headers.
	 */
@SuppressWarnings("unchecked")
public MimeEntity extractMimeEntity() {
    MimeEntity retVal = null;
    try {
        InternetHeaders headers = new InternetHeaders();
        if (this.headers.getAllHeaders().hasMoreElements()) {
            Enumeration<javax.mail.Header> hEnum = this.headers.getAllHeaders();
            while (hEnum.hasMoreElements()) {
                javax.mail.Header hdr = hEnum.nextElement();
                if (MimeStandard.startsWith(hdr.getName(), MimeStandard.HeaderPrefix))
                    headers.addHeader(hdr.getName(), hdr.getValue());
            }
            if (!headers.getAllHeaders().hasMoreElements()) {
                throw new MimeException(MimeError.InvalidMimeEntity);
            }
            retVal = new MimeEntity(headers, getContentAsBytes());
        }
    } catch (MessagingException e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
    return retVal;
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MessagingException(javax.mail.MessagingException)

Example 7 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class WrappedMessage method copyHeaders.

@SuppressWarnings("unchecked")
private static /*
	 * Copy the requested headers from the original message into a new header collection.
	 */
InternetHeaders copyHeaders(Message message, String[] headersToCopy) throws MessagingException {
    InternetHeaders retVal = new InternetHeaders();
    // InternetHeaders allow work on the raw message header
    Enumeration<String> hdEnum = message.getMatchingHeaderLines(headersToCopy);
    while (hdEnum.hasMoreElements()) retVal.addHeaderLine(hdEnum.nextElement());
    return retVal;
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders)

Example 8 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class NotificationMessage method createHeaders.

private static InternetHeaders createHeaders(String to, String from, Notification notification) throws MessagingException {
    InternetHeaders headers = new InternetHeaders();
    if (to != null && !to.isEmpty())
        headers.addHeader(MailStandard.Headers.To, to);
    if (from != null && !from.isEmpty())
        headers.addHeader(MailStandard.Headers.From, from);
    headers.addHeader(MimeStandard.VersionHeader, "1.0");
    // get the boundary
    ContentType type = new ContentType(notification.getAsMultipart().getContentType());
    String boundary = type.getParameter("boundary");
    headers.addHeader(MailStandard.Headers.ContentType, MDNStandard.MediaType.DispositionReport + "; boundary=\"" + boundary + "\"");
    return headers;
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) ContentType(javax.mail.internet.ContentType)

Example 9 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class CryptographerTest method testEncryptAndDecryptMultipartEntity.

private void testEncryptAndDecryptMultipartEntity(EncryptionAlgorithm encAlgo, boolean enforceStrongEncryption) throws Exception {
    X509Certificate cert = TestUtils.getExternalCert("user1");
    SMIMECryptographerImpl cryptographer = new SMIMECryptographerImpl();
    cryptographer.setEncryptionAlgorithm(encAlgo);
    cryptographer.setStrongEncryptionEnforced(enforceStrongEncryption);
    MimeEntity entityText = new MimeEntity();
    entityText.setText("Hello world.");
    entityText.setHeader(MimeStandard.ContentTypeHeader, "text/plain");
    entityText.setHeader(MimeStandard.ContentTransferEncodingHeader, "7bit");
    MimeEntity entityXML = new MimeEntity();
    entityXML.setText("<Test></Test>");
    entityXML.setHeader(MimeStandard.ContentTypeHeader, "text/xml");
    MimeMultipart mpEntity = new MimeMultipart();
    mpEntity.addBodyPart(entityText);
    mpEntity.addBodyPart(entityXML);
    MimeEntity encEntity = cryptographer.encrypt(mpEntity, cert);
    assertNotNull(encEntity);
    X509CertificateEx certex = TestUtils.getInternalCert("user1");
    MimeEntity decryEntity = cryptographer.decrypt(encEntity, certex);
    assertNotNull(decryEntity);
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    mpEntity.writeTo(oStream);
    InternetHeaders hdrs = new InternetHeaders();
    hdrs.addHeader(MimeStandard.ContentTypeHeader, mpEntity.getContentType());
    MimeEntity orgEntity = new MimeEntity(hdrs, oStream.toByteArray());
    byte[] decryEntityBytes = EntitySerializer.Default.serializeToBytes(decryEntity);
    byte[] entityBytes = EntitySerializer.Default.serializeToBytes(orgEntity);
    System.out.println("Original:\r\n" + new String(entityBytes));
    System.out.println("\r\n\r\n\r\nNew:\r\n" + new String(decryEntityBytes));
    assertTrue(Arrays.equals(decryEntityBytes, entityBytes));
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) SMIMECryptographerImpl(org.nhindirect.stagent.cryptography.SMIMECryptographerImpl) MimeMultipart(javax.mail.internet.MimeMultipart) X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) X509Certificate(java.security.cert.X509Certificate)

Example 10 with InternetHeaders

use of javax.mail.internet.InternetHeaders in project nhin-d by DirectProject.

the class MDNFactory_createTest method testCreate_withFailure.

public void testCreate_withFailure() throws Exception {
    final Disposition disp = new Disposition(NotificationType.Processed);
    final MdnGateway gateway = new MdnGateway("junitGateway");
    MimeMultipartReport report = MDNFactory.create("test", "junitUA", "junitProduct", "sender@send.com", "final@final.com", "12345", "", gateway, disp, "", "junit failure", new ArrayList<String>());
    assertNotNull(report);
    final InternetHeaders headers = getNotificationFieldsAsHeaders(report);
    assertEquals("junit failure", headers.getHeader("Failure", ","));
}
Also used : MimeMultipartReport(org.apache.mailet.base.mail.MimeMultipartReport) InternetHeaders(javax.mail.internet.InternetHeaders)

Aggregations

InternetHeaders (javax.mail.internet.InternetHeaders)51 MimeMultipart (javax.mail.internet.MimeMultipart)18 MessagingException (javax.mail.MessagingException)16 BodyPart (javax.mail.BodyPart)14 IOException (java.io.IOException)10 DispositionNotification (com.sun.mail.dsn.DispositionNotification)9 MimeMessage (javax.mail.internet.MimeMessage)8 MimeMultipartReport (org.apache.mailet.base.mail.MimeMultipartReport)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Header (javax.mail.Header)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 InputStream (java.io.InputStream)4 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 NHINDException (org.nhindirect.stagent.NHINDException)4 MimeException (org.nhindirect.stagent.mail.MimeException)4 X509Certificate (java.security.cert.X509Certificate)3 ContentType (javax.mail.internet.ContentType)3 ParseException (javax.mail.internet.ParseException)3 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)3