Search in sources :

Example 11 with Message

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

the class NHINDSecurityAndTrustMailet_service_Test method testService_RejectRecipients_AssertRejectedList.

@SuppressWarnings("unused")
public void testService_RejectRecipients_AssertRejectedList() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            usedRecipients.get(0).setStatus(TrustEnforcementStatus.Failed);
            usedRecipients.get(1).setStatus(TrustEnforcementStatus.Success);
            usedSender = (NHINDAddress) invocation.getArguments()[2];
            MyMessageEnvelope env = new MyMessageEnvelope(new Message(mimeMsg), usedRecipients, usedSender);
            env.setAgent(new MockNHINDAgent(Arrays.asList("cerner.com")));
            env.categorizeRecipients(TrustEnforcementStatus.Success);
            NHINDAddressCollection rejectedRecips = env.getRejectedRecipients();
            return new MessageProcessResult(env, null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    mockMail.setRecipients(Arrays.asList(new MailAddress("you@cerner.com"), new MailAddress("they@cerner.com")));
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertEquals(1, mockMail.getRecipients().size());
}
Also used : MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) NHINDAddress(org.nhindirect.stagent.NHINDAddress) MockNHINDAgent(org.nhindirect.stagent.MockNHINDAgent) Mail(org.apache.mailet.Mail) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 12 with Message

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

the class NHINDSecurityAndTrustMailet_service_Test method testService_UseToHeader_AssertRecipientsUsed.

public void testService_UseToHeader_AssertRecipientsUsed() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            return new MessageProcessResult(new DefaultMessageEnvelope(new Message(mimeMsg)), null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(null);
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertNotNull(usedRecipients);
    assertEquals(1, usedRecipients.size());
    assertEquals("externUser1@starugh-stateline.com", usedRecipients.iterator().next().toString());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult)

Example 13 with Message

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

the class DefaultNHINDAgent_bindAddressesIncomingMessageTest method buildIncomingMessage.

protected IncomingMessage buildIncomingMessage(String fileName) throws Exception {
    final String testMessage = TestUtils.readResource(fileName);
    final Message incoming = new Message(new ByteArrayInputStream(testMessage.getBytes("ASCII")));
    return new IncomingMessage(incoming);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 14 with Message

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

the class DefaultNHINDAgent_bindAddressesOutgoingMessageTest method buildOutgoingMessage.

protected OutgoingMessage buildOutgoingMessage(String fileName) throws Exception {
    final String testMessage = TestUtils.readResource(fileName);
    final Message incoming = new Message(new ByteArrayInputStream(testMessage.getBytes("ASCII")));
    return new OutgoingMessage(incoming);
}
Also used : Message(org.nhindirect.stagent.mail.Message) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 15 with Message

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

the class DefaultNHINDAgent method signAndEncryptMessage.

//
// First sign, THEN encrypt the message
//
@SuppressWarnings("unchecked")
protected void signAndEncryptMessage(OutgoingMessage message) {
    SignedEntity signedEntity = cryptographer.sign(message.getMessage(), message.getSender().getCertificates());
    try {
        if (encryptionEnabled) {
            MimeEntity encryptedEntity = cryptographer.encrypt(signedEntity.getMimeMultipart(), message.getRecipients().getCertificates());
            //
            // Alter message content to contain encrypted data
            //
            InternetHeaders headers = new InternetHeaders();
            Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            eHeaders = encryptedEntity.getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = (Header) eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            Message msg = new Message(headers, encryptedEntity.getContentAsBytes());
            message.setMessage(msg);
        } else {
            InternetHeaders headers = new InternetHeaders();
            Enumeration<Header> eHeaders = message.getMessage().getAllHeaders();
            while (eHeaders.hasMoreElements()) {
                Header hdr = eHeaders.nextElement();
                headers.setHeader(hdr.getName(), hdr.getValue());
            }
            headers.setHeader(MimeStandard.ContentTypeHeader, signedEntity.getMimeMultipart().getContentType());
            Message msg = new Message(headers, signedEntity.getEntityBodyAsBytes());
            message.setMessage(msg);
        }
    } catch (Exception e) {
        throw new MimeException(MimeError.InvalidMimeEntity, e);
    }
}
Also used : InternetHeaders(javax.mail.internet.InternetHeaders) Header(javax.mail.Header) WrappedMessage(org.nhindirect.stagent.mail.WrappedMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeEntity(org.nhindirect.stagent.mail.MimeEntity) MimeException(org.nhindirect.stagent.mail.MimeException) SignedEntity(org.nhindirect.stagent.cryptography.SignedEntity) MessagingException(javax.mail.MessagingException) TrustException(org.nhindirect.stagent.trust.TrustException) MimeException(org.nhindirect.stagent.mail.MimeException) PolicyRequiredException(org.nhindirect.policy.PolicyRequiredException) PolicyParseException(org.nhindirect.policy.PolicyParseException) IOException(java.io.IOException) PolicyProcessException(org.nhindirect.policy.PolicyProcessException)

Aggregations

Message (org.nhindirect.stagent.mail.Message)25 MimeMessage (javax.mail.internet.MimeMessage)17 ByteArrayInputStream (java.io.ByteArrayInputStream)14 MessagingException (javax.mail.MessagingException)7 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)7 ArrayList (java.util.ArrayList)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 NHINDAddress (org.nhindirect.stagent.NHINDAddress)5 MimeEntity (org.nhindirect.stagent.mail.MimeEntity)5 IOException (java.io.IOException)4 X509Certificate (java.security.cert.X509Certificate)4 MailAddress (org.apache.mailet.MailAddress)4 NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)4 Header (javax.mail.Header)3 InternetHeaders (javax.mail.internet.InternetHeaders)3 Mail (org.apache.mailet.Mail)3 CMSSignedData (org.bouncycastle.cms.CMSSignedData)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 MessageProcessResult (org.nhindirect.gateway.smtp.MessageProcessResult)3 SmtpAgent (org.nhindirect.gateway.smtp.SmtpAgent)3