Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mailet/standard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
Expand All @@ -38,73 +40,80 @@
*/
public class HasHeader extends GenericMatcher {

private LinkedList<String> conditionline_ = new LinkedList<String>();
private static final String CONDITION_SEPARATOR = "+";
private static final String HEADER_VALUE_SEPARATOR = "=";

// set headernames and values
public void init() throws MessagingException {
StringTokenizer st = new StringTokenizer(getCondition(), "+");
conditionline_ = new LinkedList<String>();
private interface HeaderCondition {
boolean isMatching(MimeMessage mimeMessage) throws MessagingException;
}

private static class HeaderNameCondition implements HeaderCondition {
private final String headerName;

public HeaderNameCondition(String headerName) {
this.headerName = headerName;
}

// separates the headernames from the matchline
while (st.hasMoreTokens()) {
String condition = st.nextToken().trim();
conditionline_.add(condition);
@Override
public boolean isMatching(MimeMessage mimeMessage) throws MessagingException {
String[] headerArray = mimeMessage.getHeader(headerName);
return headerArray != null && headerArray.length > 0;
}
}

public Collection<MailAddress> match(Mail mail) throws javax.mail.MessagingException {
boolean match = false;
MimeMessage message = mail.getMessage();

for (String element : conditionline_) {
StringTokenizer st = new StringTokenizer(element, "=", false);
String header;

// read the headername
if (st.hasMoreTokens()) {
header = st.nextToken().trim();
} else {
throw new MessagingException("Missing headerName");
}
private static class HeaderValueCondition implements HeaderCondition {
private final String headerName;
private final String headerValue;

// try to read headervalue
String headerValue;
if (st.hasMoreTokens()) {
headerValue = st.nextToken().trim();
} else {
headerValue = null;
}
public HeaderValueCondition(String headerName, String headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
}

// find headername in Mailheaders
String[] headerArray = message.getHeader(header);
@Override
public boolean isMatching(MimeMessage mimeMessage) throws MessagingException {
String[] headerArray = mimeMessage.getHeader(headerName);
if (headerArray != null && headerArray.length > 0) {
// if there is the headername specified without the headervalue
// only the existence of the headername ist performed
if (headerValue != null) {
//
if (headerArray[0].trim().equalsIgnoreCase(headerValue)) {
// headername and value found and match to the condition
match = true;
} else {
// headername and value found but the value does not match the condition
match = false;
// if one condition fails the matcher returns false
break;
for (String value : headerArray) {
if (headerValue.equals(value)) {
return true;
}
} else {
// just the headername is specified
match = true;
}
} else {
// no headername is found
match = false;
// if one condition fails the matcher returns false
break;
}
return false;
}
}

private List<HeaderCondition> headerConditions;

public void init() throws MessagingException {
headerConditions = new ArrayList<HeaderCondition>();
StringTokenizer conditionTokenizer = new StringTokenizer(getCondition(), CONDITION_SEPARATOR);
while (conditionTokenizer.hasMoreTokens()) {
headerConditions.add(parseHeaderCondition(conditionTokenizer.nextToken().trim()));
}
}

return (match) ? mail.getRecipients() : null;
private HeaderCondition parseHeaderCondition(String element) throws MessagingException {
StringTokenizer valueSeparatorTokenizer = new StringTokenizer(element, HEADER_VALUE_SEPARATOR, false);
if (!valueSeparatorTokenizer.hasMoreElements()) {
throw new MessagingException("Missing headerName");
}
String headerName = valueSeparatorTokenizer.nextToken().trim();
if (valueSeparatorTokenizer.hasMoreTokens()) {
return new HeaderValueCondition(headerName, valueSeparatorTokenizer.nextToken().trim());
} else {
return new HeaderNameCondition(headerName);
}
}

public Collection<MailAddress> match(Mail mail) throws javax.mail.MessagingException {
for (HeaderCondition headerCondition : headerConditions) {
if (!headerCondition.isMatching(mail.getMessage())) {
return null;
}
}
return mail.getRecipients();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,126 @@

package org.apache.james.transport.matchers;

import org.apache.james.transport.matchers.HasHeader;
import org.apache.mailet.MailAddress;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import org.apache.mailet.Mail;
import org.apache.mailet.Matcher;
import org.apache.mailet.base.test.FakeMail;
import org.apache.mailet.base.test.FakeMailContext;
import org.apache.mailet.base.test.FakeMatcherConfig;
import org.apache.mailet.base.test.MailUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Collection;

public class HasHeaderTest {

private MimeMessage mockedMimeMessage;
private static final String HEADER_NAME_1 = "JUNIT";
private static final String HEADER_NAME_2 = "defaultHeaderName";
private static final String HEADER_VALUE_1 = "defaultHeaderValue";
private static final String HEADER_VALUE_2 = "defaultHeaderValue2";

private FakeMail mockedMail;

private Matcher matcher;

private final String HEADER_NAME = "JUNIT";
@Before
public void setUp() throws Exception {
MimeMessage mimeMessage = MailUtil.createMimeMessage(HEADER_NAME_1, HEADER_VALUE_1);
mockedMail = MailUtil.createMockMail2Recipients(mimeMessage);
matcher = new HasHeader();
}

@Test
public void matchShouldReturnAddressesWhenRightHeaderNameWithoutValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1, new FakeMailContext()));

private String headerName = "defaultHeaderName";
assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

private String headerValue = "defaultHeaderValue";
@Test
public void matchShouldReturnNullWhenWrongHeaderNameWithoutValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_2, new FakeMailContext()));

private void setHeaderName(String headerName) {
this.headerName = headerName;
assertThat(matcher.match(mockedMail)).isNull();
}

private void setHeaderValue(String headerValue) {
this.headerValue = headerValue;
@Test
public void matchShouldReturnAddressesWhenGoodHeaderNameAndValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1, new FakeMailContext()));

assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

private void setupMockedMimeMessage() throws MessagingException {
mockedMimeMessage = MailUtil.createMimeMessage(headerName, headerValue);
@Test
public void matchShouldReturnNullWhenWrongValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_2, new FakeMailContext()));

assertThat(matcher.match(mockedMail)).isNull();
}

private void setupMatcher() throws MessagingException {
setupMockedMimeMessage();
matcher = new HasHeader();
FakeMatcherConfig mci = new FakeMatcherConfig("HasHeader="
+ HEADER_NAME, new FakeMailContext());
matcher.init(mci);
@Test
public void matchShouldReturnNullWhenWrongHeaderNameWithValueSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_2 + "=" + HEADER_VALUE_2, new FakeMailContext()));

assertThat(matcher.match(mockedMail)).isNull();
}

@Test
public void matchShouldIgnoreExtraEquals() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1 + "=any", new FakeMailContext()));

assertThat(matcher.match(mockedMail)).containsAll(mockedMail.getRecipients());
}

// test if the Header was matched
@Test
public void testHeaderIsMatched() throws MessagingException {
setHeaderName(HEADER_NAME);
String HEADER_VALUE = "test-value";
setHeaderValue(HEADER_VALUE);
public void matchShouldNotMatchMailsWithNoHeaderWhenValueSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_1, new FakeMailContext()));
Mail mail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());

setupMockedMimeMessage();
mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
setupMatcher();
assertThat(matcher.match(mail)).isNull();
}

Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
@Test
public void matchShouldNotMatchMailsWithNoHeaderWhenValueNotSpecified() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1, new FakeMailContext()));
Mail mail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage());

Assert.assertNotNull(matchedRecipients);
Assert.assertEquals(matchedRecipients.size(), mockedMail.getRecipients()
.size());
assertThat(matcher.match(mail)).isNull();
}

// test if the Header was not matched
@Test
public void testHeaderIsNotMatched() throws MessagingException {
setupMockedMimeMessage();
mockedMail = MailUtil.createMockMail2Recipients(mockedMimeMessage);
setupMatcher();
public void matchShouldReturnNullWhenOneConditionIsNotTrue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "+" + HEADER_NAME_2, new FakeMailContext()));

assertThat(matcher.match(mockedMail)).isNull();
}

Collection<MailAddress> matchedRecipients = matcher.match(mockedMail);
@Test
public void matchShouldReturnAddressesWhenAllConditionsMatch() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "+" + HEADER_NAME_2, new FakeMailContext()));
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_1);
mimeMessage.addHeader(HEADER_NAME_2, HEADER_VALUE_2);
mimeMessage.saveChanges();
Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);

assertThat(matcher.match(mail)).containsAll(mockedMail.getRecipients());
}

Assert.assertNull(matchedRecipients);
@Test
public void matchShouldFindTheRightHeaderLineWhenUsedWithValue() throws MessagingException {
matcher.init(new FakeMatcherConfig("HasHeader=" + HEADER_NAME_1 + "=" + HEADER_VALUE_2, new FakeMailContext()));
MimeMessage mimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties()));
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_1);
mimeMessage.addHeader(HEADER_NAME_1, HEADER_VALUE_2);
mimeMessage.saveChanges();
Mail mail = MailUtil.createMockMail2Recipients(mimeMessage);

assertThat(matcher.match(mail)).containsAll(mockedMail.getRecipients());
}
}
Loading