Search in sources :

Example 11 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class NotificationTest method testCreateNotification_AssertGetParts.

public void testCreateNotification_AssertGetParts() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    ArrayList<MimeEntity> entities = (ArrayList<MimeEntity>) noti.getParts();
    MimeEntity entity = entities.get(0);
    assertEquals("Your message was successfully processed.", entity.getContent().toString());
    entity = entities.get(1);
    assertTrue(entity.getContentType().startsWith("message/disposition-notification"));
    DispositionNotification notification = (DispositionNotification) entity.getContent();
    assertEquals(notification.getNotifications().getHeader("disposition", ","), "automatic-action/MDN-sent-automatically;processed");
}
Also used : DispositionNotification(com.sun.mail.dsn.DispositionNotification) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 12 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method encrypt.

/** 
     * Encrypts an entity using the provided certificates.
     * @param entity The entity that will be encrypted.
     * @param encryptingCertificate The public certificates that will be used to encrypt the message.
     * @return A MimeEntity containing the encrypted part.
     */
public MimeEntity encrypt(MimeEntity entity, Collection<X509Certificate> encryptingCertificates) {
    if (entity == null) {
        throw new IllegalArgumentException();
    }
    MimeBodyPart partToEncrypt = entity;
    MimeBodyPart encryptedPart = this.encrypt(partToEncrypt, encryptingCertificates);
    MimeEntity encryptedEntity = null;
    try {
        byte[] encBytes = EntitySerializer.Default.serializeToBytes(encryptedPart);
        ByteArrayInputStream inStream = new ByteArrayInputStream(EntitySerializer.Default.serializeToBytes(encryptedPart));
        encryptedEntity = new MimeEntity(inStream);
        if (LOGGER.isDebugEnabled()) {
            writePostEncypt(encBytes);
        }
        encryptedEntity.setHeader(MimeStandard.ContentTypeHeader, SMIMEStandard.EncryptedContentTypeHeaderValue);
    } catch (Exception e) {
        throw new MimeException(MimeError.Unexpected, e);
    }
    return encryptedEntity;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) MimeBodyPart(javax.mail.internet.MimeBodyPart) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException)

Example 13 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method decrypt.

/**
     * Decrypts an entity with the provided certificate's private key.
     * @param encryptedEntity The entity that will be decrypted.
     * @param decryptingCertificate The certificate whose private key will be used to decrypt the message.
     * @return A MimeEntity containing the decrypted part.
     */
public MimeEntity decrypt(MimeEntity encryptedEntity, X509CertificateEx decryptingCertificate) {
    if (encryptedEntity == null || decryptingCertificate == null) {
        throw new IllegalArgumentException();
    }
    if (!decryptingCertificate.hasPrivateKey()) {
        throw new IllegalArgumentException("Certificate has no private key");
    }
    encryptedEntity.verifyContentType(SMIMEStandard.EncryptedContentTypeHeaderValue);
    encryptedEntity.verifyTransferEncoding(MimeStandard.TransferEncodingBase64);
    Collection<X509CertificateEx> certs = new ArrayList<X509CertificateEx>();
    certs.add(decryptingCertificate);
    MimeEntity retVal = this.decrypt(encryptedEntity, certs);
    //
    return retVal;
}
Also used : X509CertificateEx(org.nhindirect.stagent.cert.X509CertificateEx) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList)

Example 14 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class SMIMECryptographerImpl method encrypt.

/**
     * Encrypts a mulit part MIME entity using the provided certificates.
     * @param entity The entity that will be encrypted.
     * @param encryptingCertificates The public certificates that will be used to encrypt the message.
     * @return A MimeEntity containing the encrypted part.
     */
public MimeEntity encrypt(MimeMultipart mmEntity, Collection<X509Certificate> encryptingCertificates) {
    MimeEntity entToEncrypt = null;
    ByteArrayOutputStream oStream = new ByteArrayOutputStream();
    try {
        mmEntity.writeTo(oStream);
        oStream.flush();
        InternetHeaders headers = new InternetHeaders();
        headers.addHeader(MimeStandard.ContentTypeHeader, mmEntity.getContentType());
        entToEncrypt = new MimeEntity(headers, oStream.toByteArray());
        IOUtils.closeQuietly(oStream);
    } catch (Exception e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
    return this.encrypt(entToEncrypt, encryptingCertificates);
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessagingException(javax.mail.MessagingException) MimeException(org.nhindirect.stagent.mail.MimeException) NHINDException(org.nhindirect.stagent.NHINDException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException) SignatureValidationException(org.nhindirect.stagent.SignatureValidationException)

Example 15 with MimeEntity

use of org.nhindirect.stagent.mail.MimeEntity in project nhin-d by DirectProject.

the class Notification method getParts.

/**
	 * Returns a collection of body parts of the multipart report for this notification.
	 * @return A collection of body parts of the multipart report for this notification.
	 */
public Collection<MimeEntity> getParts() {
    if (report == null)
        updateReport();
    Collection<MimeEntity> retVal = new ArrayList<MimeEntity>();
    try {
        for (int i = 0; i < report.getCount(); ++i) {
            ByteArrayOutputStream oStream = null;
            try {
                oStream = new ByteArrayOutputStream();
                report.getBodyPart(i).writeTo(oStream);
                oStream.flush();
            } catch (MessagingException e) {
            } catch (IOException e) {
            }
            InputStream str = new ByteArrayInputStream(oStream.toByteArray());
            retVal.add(new MimeEntity(str));
        }
    } catch (MessagingException e) {
    /* */
    }
    return retVal;
}
Also used : MessagingException(javax.mail.MessagingException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

MimeEntity (org.nhindirect.stagent.mail.MimeEntity)34 X509Certificate (java.security.cert.X509Certificate)20 X509CertificateEx (org.nhindirect.stagent.cert.X509CertificateEx)18 SMIMECryptographerImpl (org.nhindirect.stagent.cryptography.SMIMECryptographerImpl)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 MessagingException (javax.mail.MessagingException)9 IOException (java.io.IOException)8 MimeMultipart (javax.mail.internet.MimeMultipart)8 SignedEntity (org.nhindirect.stagent.cryptography.SignedEntity)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Message (org.nhindirect.stagent.mail.Message)6 MimeException (org.nhindirect.stagent.mail.MimeException)6 ArrayList (java.util.ArrayList)5 InternetHeaders (javax.mail.internet.InternetHeaders)5 SMIMEEnveloped (org.bouncycastle.mail.smime.SMIMEEnveloped)5 NHINDException (org.nhindirect.stagent.NHINDException)5 SignatureValidationException (org.nhindirect.stagent.SignatureValidationException)5 KeyStore (java.security.KeyStore)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMessage (javax.mail.internet.MimeMessage)3