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;
}
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;
}
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();
}
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());
}
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());
}
Aggregations