Search in sources :

Example 1 with MailAddress

use of org.apache.mailet.MailAddress in project nhin-d by DirectProject.

the class RecipAndSenderIsNotLocal method match.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public Collection<MailAddress> match(Mail mail) throws MessagingException {
    String domain = "";
    if (mail.getSender() != null && mail.getSender().getDomain() != null)
        domain = mail.getSender().getDomain().toUpperCase(Locale.getDefault());
    else {
        Address[] senderAddr = mail.getMessage().getFrom();
        if (senderAddr != null && senderAddr.length > 0) {
            domain = NHINDAddress.getHost((InternetAddress) senderAddr[0]).toUpperCase(Locale.getDefault());
        }
    }
    if (!domains.contains(domain)) {
        // this is from a remote domain... this auto qualifies all recipients
        LOGGER.debug("Sender is remote.  Return all recipients as matching");
        return mail.getRecipients();
    }
    // the sender is local, so only return recipients that are not local
    LOGGER.debug("Sender is local.  Matching non local recipients.");
    Collection<MailAddress> matching = new Vector<MailAddress>();
    for (MailAddress addr : (Collection<MailAddress>) mail.getRecipients()) {
        if (!domains.contains(addr.getDomain().toUpperCase(Locale.getDefault()))) {
            LOGGER.debug("Matched recipient " + addr.toString());
            matching.add(addr);
        }
    }
    return matching;
}
Also used : MailAddress(org.apache.mailet.MailAddress) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Collection(java.util.Collection) Vector(java.util.Vector)

Example 2 with MailAddress

use of org.apache.mailet.MailAddress in project nhin-d by DirectProject.

the class AbstractNotificationAwareMailet method getMailRecipients.

/**
	 * Get the recipients of Mail message by retrieving the recipient list from the SMTP envelope first, then falling back to the recipients
	 * in the message if the recipients cannot be retrieved from the SMTP envelope.
	 * @param mail The mail object that contains information from the SMTP envelope.
	 * @return Collection of message recipients.
	 * @throws MessagingException
	 */
@SuppressWarnings("unchecked")
protected NHINDAddressCollection getMailRecipients(Mail mail) throws MessagingException {
    final NHINDAddressCollection recipients = new NHINDAddressCollection();
    // uses the RCPT TO commands
    final Collection<MailAddress> recips = mail.getRecipients();
    if (recips == null || recips.size() == 0) {
        // fall back to the mime message list of recipients
        final Address[] recipsAddr = mail.getMessage().getAllRecipients();
        for (Address addr : recipsAddr) {
            recipients.add(new NHINDAddress(addr.toString(), (AddressSource) null));
        }
    } else {
        for (MailAddress addr : recips) {
            recipients.add(new NHINDAddress(addr.toString(), (AddressSource) null));
        }
    }
    return recipients;
}
Also used : NHINDAddress(org.nhindirect.stagent.NHINDAddress) MailAddress(org.apache.mailet.MailAddress) AddressSource(org.nhindirect.stagent.AddressSource) Address(javax.mail.Address) MailAddress(org.apache.mailet.MailAddress) InternetAddress(javax.mail.internet.InternetAddress) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection)

Example 3 with MailAddress

use of org.apache.mailet.MailAddress in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_functionalTest method testProcessOutgoingMessageEndToEnd_tamperedRoutingHeaders_rejectPolicyOn_assertRejected.

public void testProcessOutgoingMessageEndToEnd_tamperedRoutingHeaders_rejectPolicyOn_assertRejected() throws Exception {
    new TestPlan() {

        protected String getMessageToProcess() throws Exception {
            return TestUtils.readMessageResource("PlainOutgoingMessage.txt");
        }

        @Override
        protected Mailet getMailet(String configurationFileName) throws Exception {
            Mailet retVal = null;
            String configfile = TestUtils.getTestConfigFile(configurationFileName);
            Map<String, String> params = new HashMap<String, String>();
            if (configurationFileName.startsWith("http"))
                params.put("ConfigURL", ConfigServiceRunner.getConfigServiceURL());
            else
                params.put("ConfigURL", "file://" + configfile);
            params.put(SecurityAndTrustMailetOptions.REJECT_ON_ROUTING_TAMPER, "true");
            retVal = new NHINDSecurityAndTrustMailet();
            MailetConfig mailetConfig = new MockMailetConfig(params, "NHINDSecurityAndTrustMailet");
            retVal.init(mailetConfig);
            return retVal;
        }

        protected void performInner() throws Exception {
            // encrypt
            String originalMessage = getMessageToProcess();
            MimeMessage msg = EntitySerializer.Default.deserialize(originalMessage);
            // add an MDN request
            msg.setHeader(MDNStandard.Headers.DispositionNotificationTo, msg.getHeader(MailStandard.Headers.From, ","));
            MockMail theMessage = new MockMail(msg);
            Mailet theMailet = getMailet("ValidConfig.xml");
            theMailet.service(theMessage);
            assertNotNull(theMessage);
            assertNotNull(theMessage.getMessage());
            msg = theMessage.getMessage();
            assertTrue(SMIMEStandard.isEncrypted(msg));
            assertEquals(theMessage.getState(), Mail.TRANSPORT);
            // decrypt
            theMailet = getMailet("ValidConfigStateLine.txt");
            theMessage = new MockMail(msg);
            final MailAddress validAddress = new MailAddress(msg.getRecipients(RecipientType.TO)[0].toString());
            final MailAddress injectedAttackAddress = new MailAddress("externUser2@starugh-stateline.com");
            theMessage.setRecipients(Arrays.asList(validAddress, injectedAttackAddress));
            theMailet.service(theMessage);
            // rejected and ghosted
            assertEquals(Mail.GHOST, theMessage.getState());
        }
    }.perform();
}
Also used : MailAddress(org.apache.mailet.MailAddress) BaseTestPlan(org.nhindirect.gateway.testutils.BaseTestPlan) NHINDSecurityAndTrustMailet(org.nhindirect.gateway.smtp.james.mailet.NHINDSecurityAndTrustMailet) MailetConfig(org.apache.mailet.MailetConfig) NHINDSecurityAndTrustMailet(org.nhindirect.gateway.smtp.james.mailet.NHINDSecurityAndTrustMailet) Mailet(org.apache.mailet.Mailet) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with MailAddress

use of org.apache.mailet.MailAddress in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_UseRcpt_AssertRecipientsUsed.

public void testService_UseRcpt_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];
            usedSender = (NHINDAddress) invocation.getArguments()[2];
            return new MessageProcessResult(new DefaultMessageEnvelope(new Message(mimeMsg), usedRecipients, usedSender), null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(Arrays.asList(new MailAddress("you@cerner.com")));
    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("you@cerner.com", usedRecipients.iterator().next().toString());
}
Also used : MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Mail(org.apache.mailet.Mail) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 5 with MailAddress

use of org.apache.mailet.MailAddress in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState.

public void testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(null);
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    doThrow(new RuntimeException("Just Passing Through")).when(mockAgent).processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any());
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    boolean exceptionOccured = false;
    try {
        mailet.service(mockMail);
    } catch (MessagingException e) {
        assertEquals("Failed to process message: Just Passing Through", e.getMessage());
        exceptionOccured = true;
    }
    assertFalse(exceptionOccured);
    assertEquals(Mail.GHOST, mockMail.getState());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent)

Aggregations

MailAddress (org.apache.mailet.MailAddress)28 MimeMessage (javax.mail.internet.MimeMessage)17 ArrayList (java.util.ArrayList)11 Mail (org.apache.mailet.Mail)11 InternetAddress (javax.mail.internet.InternetAddress)8 MockMail (org.nhindirect.gateway.smtp.james.mailet.MockMail)8 SmtpAgent (org.nhindirect.gateway.smtp.SmtpAgent)7 NHINDAddressCollection (org.nhindirect.stagent.NHINDAddressCollection)7 NHINDAddress (org.nhindirect.stagent.NHINDAddress)6 Collection (java.util.Collection)5 MessageProcessResult (org.nhindirect.gateway.smtp.MessageProcessResult)5 Address (javax.mail.Address)4 MessagingException (javax.mail.MessagingException)4 MatcherConfig (org.apache.mailet.MatcherConfig)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Message (org.nhindirect.stagent.mail.Message)4 Tx (org.nhindirect.common.tx.model.Tx)2 SmtpAgentException (org.nhindirect.gateway.smtp.SmtpAgentException)2 DefaultMessageEnvelope (org.nhindirect.stagent.DefaultMessageEnvelope)2 NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)2