You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/08/10 11:26:10 UTC
[1/3] james-project git commit: MAILET-159 Correct javadoc of
RemoveMimeHeader
Repository: james-project
Updated Branches:
refs/heads/master 6ed40404e -> 080904254
MAILET-159 Correct javadoc of RemoveMimeHeader
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7a5a2eeb
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7a5a2eeb
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7a5a2eeb
Branch: refs/heads/master
Commit: 7a5a2eeb64c0e1cf4605c45d0d06d58821ee41bb
Parents: 6ed4040
Author: benwa <bt...@linagora.com>
Authored: Thu Aug 10 11:18:16 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Aug 10 11:18:16 2017 +0700
----------------------------------------------------------------------
.../java/org/apache/james/transport/mailets/RemoveMimeHeader.java | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/7a5a2eeb/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
index 43857a6..99cc574 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
@@ -40,8 +40,7 @@ import com.google.common.collect.ImmutableList;
*
* <pre><code>
* <mailet match="All" class="RemoveMimeHeader">
- * <name>header1</name>
- * <name>header2</name>
+ * <name>header1,header2</name>
* </mailet>
* </code></pre>
*
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[2/3] james-project git commit: MAILET-159 Implement
RemoveMimeHeaderByPrefix
Posted by bt...@apache.org.
MAILET-159 Implement RemoveMimeHeaderByPrefix
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/f3488890
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/f3488890
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/f3488890
Branch: refs/heads/master
Commit: f348889002f432b0ed8e97c975621eab83c6e5e1
Parents: 7a5a2ee
Author: benwa <bt...@linagora.com>
Authored: Thu Aug 10 11:39:30 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Aug 10 18:25:32 2017 +0700
----------------------------------------------------------------------
.../mailets/RemoveMimeHeaderByPrefix.java | 88 ++++++++++
.../mailets/utils/MimeMessageUtils.java | 12 ++
.../mailets/RemoveMimeHeaderByPrefixTest.java | 166 +++++++++++++++++++
.../mailets/utils/MimeMessageUtilsTest.java | 23 +++
4 files changed, 289 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/f3488890/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefix.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefix.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefix.java
new file mode 100644
index 0000000..0d9acf1
--- /dev/null
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefix.java
@@ -0,0 +1,88 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+
+package org.apache.james.transport.mailets;
+
+import java.util.List;
+
+import javax.mail.Header;
+import javax.mail.MessagingException;
+
+import org.apache.james.transport.mailets.utils.MimeMessageUtils;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * This mailet removes all of the headers starting with a given prefix.
+ *
+ * Sample configuration:
+ *
+ * <pre><code>
+ * <mailet match="All" class="RemoveMimeHeaderByPrefix">
+ * <name>X-APPLICATIVE-HEADER-</name>
+ * </mailet>
+ * </code></pre>
+ */
+
+public class RemoveMimeHeaderByPrefix extends GenericMailet {
+
+ public static final String PREFIX = "prefix";
+
+ private String prefix;
+
+ @Override
+ public void init() throws MessagingException {
+ prefix = getInitParameter(PREFIX);
+ if (Strings.isNullOrEmpty(prefix)) {
+ throw new MessagingException("Expecting prefix not to be empty or null with RemoveMimeHeaderByPrefix");
+ }
+ }
+
+ @Override
+ public String getMailetInfo() {
+ return "RemoveMimeHeaderByPrefix Mailet";
+ }
+
+ @Override
+ public void service(Mail mail) throws MessagingException {
+ List<String> headerNamesToRemove = headerNamesStartingByPrefix(mail);
+ for(String headerName: headerNamesToRemove) {
+ mail.getMessage().removeHeader(headerName);
+ }
+ if (!headerNamesToRemove.isEmpty()) {
+ mail.getMessage().saveChanges();
+ }
+ }
+
+ private List<String> headerNamesStartingByPrefix(Mail mail) throws MessagingException {
+ ImmutableList.Builder<String> headerToRemove = ImmutableList.builder();
+ List<Header> headers = new MimeMessageUtils(mail.getMessage()).toHeaderList();
+ for (Header header: headers) {
+ if (header.getName().startsWith(prefix)) {
+ headerToRemove.add(header.getName());
+ }
+ }
+ return headerToRemove.build();
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/james-project/blob/f3488890/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageUtils.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageUtils.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageUtils.java
index 5186abc..caea2fa 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageUtils.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/utils/MimeMessageUtils.java
@@ -19,7 +19,9 @@
package org.apache.james.transport.mailets.utils;
import java.util.Enumeration;
+import java.util.List;
+import javax.mail.Header;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@@ -29,6 +31,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
public class MimeMessageUtils {
@@ -83,4 +86,13 @@ public class MimeMessageUtils {
}
return headBuffer.toString();
}
+
+ public List<Header> toHeaderList() throws MessagingException {
+ ImmutableList.Builder<Header> headers = ImmutableList.builder();
+ Enumeration allHeaders = message.getAllHeaders();
+ for (@SuppressWarnings("unchecked") Enumeration<Header> e = allHeaders; e.hasMoreElements();) {
+ headers.add(e.nextElement());
+ }
+ return headers.build();
+ }
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/f3488890/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefixTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefixTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefixTest.java
new file mode 100644
index 0000000..2a820da
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderByPrefixTest.java
@@ -0,0 +1,166 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.transport.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.transport.mailets.utils.MimeMessageUtils;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class RemoveMimeHeaderByPrefixTest {
+
+ private static final String PREFIX = "X-OPENPAAS-";
+ private static final String HEADER_NAME_PREFIX_1 = "X-OPENPAAS-FEATURE-A";
+ private static final String HEADER_NAME_PREFIX_2 = "X-OPENPAAS-FEATURE-B";
+ private static final String HEADER_NAME_NO_PREFIX = "X-OTHER-BUSINESS";
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private GenericMailet mailet;
+
+ @Before
+ public void setup() {
+ mailet = new RemoveMimeHeaderByPrefix();
+ }
+
+ @Test
+ public void getMailetInfoShouldReturnValue() {
+ assertThat(mailet.getMailetInfo()).isEqualTo("RemoveMimeHeaderByPrefix Mailet");
+ }
+
+ @Test
+ public void serviceShouldRemoveHeaderWhenPrefixed() throws MessagingException {
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .setProperty("prefix", PREFIX)
+ .build();
+ mailet.init(mailetConfig);
+
+ Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_PREFIX_1, "true")
+ .build());
+
+ mailet.service(mail);
+
+ assertThat(new MimeMessageUtils(mail.getMessage()).toHeaderList())
+ .extracting("name")
+ .doesNotContain(HEADER_NAME_PREFIX_1);
+ }
+
+ @Test
+ public void serviceShouldRemoveAllPrefixedHeaders() throws MessagingException {
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .setProperty("prefix", PREFIX)
+ .build();
+ mailet.init(mailetConfig);
+
+ Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_PREFIX_1, "true")
+ .addHeader(HEADER_NAME_PREFIX_2, "true")
+ .build());
+
+ mailet.service(mail);
+
+ assertThat(new MimeMessageUtils(mail.getMessage()).toHeaderList())
+ .extracting("name")
+ .doesNotContain(HEADER_NAME_PREFIX_1, HEADER_NAME_PREFIX_2);
+ }
+
+ @Test
+ public void serviceShouldNotRemoveNonPrefixedHeaders() throws MessagingException {
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .setProperty("prefix", PREFIX)
+ .build();
+ mailet.init(mailetConfig);
+
+ Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_PREFIX_1, "true")
+ .addHeader(HEADER_NAME_NO_PREFIX, "true")
+ .build());
+
+ mailet.service(mail);
+
+ assertThat(new MimeMessageUtils(mail.getMessage()).toHeaderList())
+ .extracting("name")
+ .contains(HEADER_NAME_NO_PREFIX)
+ .doesNotContain(HEADER_NAME_PREFIX_1);
+ }
+
+ @Test
+ public void exactMatchOfPrefixShouldBeAllowed() throws MessagingException {
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .setProperty("prefix", PREFIX)
+ .build();
+ mailet.init(mailetConfig);
+
+ Mail mail = createMail(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(PREFIX, "true")
+ .addHeader(HEADER_NAME_NO_PREFIX, "true")
+ .build());
+
+ mailet.service(mail);
+
+ assertThat(new MimeMessageUtils(mail.getMessage()).toHeaderList())
+ .extracting("name")
+ .doesNotContain(PREFIX);
+ assertThat(mail.getMessage().getHeader(PREFIX)).isNull();
+ }
+
+
+ @Test
+ public void initShouldThrowWhenInvalidConfig() throws MessagingException {
+ expectedException.expect(MessagingException.class);
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .build();
+ mailet.init(mailetConfig);
+ }
+
+ @Test
+ public void initShouldThrowWhenPrefixEmpty() throws MessagingException {
+ expectedException.expect(MessagingException.class);
+ FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
+ .mailetName("Test")
+ .setProperty(RemoveMimeHeaderByPrefix.PREFIX, "")
+ .build();
+ mailet.init(mailetConfig);
+ }
+
+ private Mail createMail(MimeMessage message) throws MessagingException {
+ return FakeMail.builder()
+ .mimeMessage(message)
+ .build();
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/f3488890/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageUtilsTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageUtilsTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageUtilsTest.java
index 6a66541..87b53e0 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageUtilsTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/utils/MimeMessageUtilsTest.java
@@ -28,6 +28,7 @@ import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.MimeMessageBuilder;
import org.junit.Test;
import com.google.common.base.Optional;
@@ -152,4 +153,26 @@ public class MimeMessageUtilsTest {
String expectedHeaders = "first: value\r\nsecond: value2\r\n";
assertThat(messageHeaders).isEqualTo(expectedHeaders);
}
+
+ @Test
+ public void toHeaderListShouldReturnMessageIdAndMimeVersionByDefault() throws Exception {
+ assertThat(
+ new MimeMessageUtils(MimeMessageBuilder.mimeMessageBuilder()
+ .build())
+ .toHeaderList())
+ .extracting("name")
+ .containsOnly("Message-Id", "MIME-Version");
+ }
+
+ @Test
+ public void toHeaderListShouldReturnAllMessageHeaders() throws Exception {
+ String headerName = "X-OPENPAAS-FEATURE-1";
+ assertThat(
+ new MimeMessageUtils(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(headerName, "value")
+ .build())
+ .toHeaderList())
+ .extracting("name")
+ .containsOnly("Message-Id", "MIME-Version", headerName);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[3/3] james-project git commit: MAILET-159 Implement
HasHeaderWithPrefix
Posted by bt...@apache.org.
MAILET-159 Implement HasHeaderWithPrefix
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/08090425
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/08090425
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/08090425
Branch: refs/heads/master
Commit: 080904254dff29cad0dcfd263ce253122a38015f
Parents: f348889
Author: benwa <bt...@linagora.com>
Authored: Thu Aug 10 11:58:11 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Aug 10 18:25:37 2017 +0700
----------------------------------------------------------------------
.../transport/matchers/HasHeaderWithPrefix.java | 69 +++++++++
.../matchers/HasHeaderWithPrefixTest.java | 149 +++++++++++++++++++
2 files changed, 218 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/08090425/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeaderWithPrefix.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeaderWithPrefix.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeaderWithPrefix.java
new file mode 100644
index 0000000..05ee715
--- /dev/null
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasHeaderWithPrefix.java
@@ -0,0 +1,69 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.transport.matchers;
+
+import java.util.Collection;
+import java.util.List;
+
+import javax.mail.Header;
+import javax.mail.MessagingException;
+
+import org.apache.james.transport.mailets.utils.MimeMessageUtils;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMatcher;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Matches emails with headers having a given prefix.
+ *
+ * If a header with the given prefix is found, all recipients will be matched. Otherwise, no recipient in returned.
+ *
+ * use: <pre><code><mailet match="HasHeaderWithPrefix=PREFIX" class="..." /></code></pre>
+ */
+public class HasHeaderWithPrefix extends GenericMatcher {
+
+ private String prefix;
+
+ @Override
+ public void init() throws MessagingException {
+ prefix = getCondition();
+ if (Strings.isNullOrEmpty(prefix)) {
+ throw new MessagingException("Expecting prefix not to be empty or null with RemoveMimeHeaderByPrefix");
+ }
+ }
+
+ public String getMatcherInfo() {
+ return "HasHeaderWithPrefix Matcher";
+ }
+
+ @Override
+ public Collection<MailAddress> match(Mail mail) throws MessagingException {
+ List<Header> headers = new MimeMessageUtils(mail.getMessage()).toHeaderList();
+ for (Header header: headers) {
+ if (header.getName().startsWith(prefix)) {
+ return mail.getRecipients();
+ }
+ }
+ return ImmutableList.of();
+ }
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/08090425/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasHeaderWithPrefixTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasHeaderWithPrefixTest.java b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasHeaderWithPrefixTest.java
new file mode 100644
index 0000000..90f2978
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasHeaderWithPrefixTest.java
@@ -0,0 +1,149 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+
+package org.apache.james.transport.matchers;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import javax.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.Matcher;
+import org.apache.mailet.base.MailAddressFixture;
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMatcherConfig;
+import org.apache.mailet.base.test.MimeMessageBuilder;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class HasHeaderWithPrefixTest {
+ private static final String PREFIX = "X-OPENPAAS-";
+ private static final String HEADER_NAME_PREFIX_1 = "X-OPENPAAS-FEATURE-A";
+ private static final String HEADER_NAME_NO_PREFIX = "X-OTHER-BUSINESS";
+
+ private Matcher matcher;
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Before
+ public void setUp() throws Exception {
+ matcher = new HasHeaderWithPrefix();
+ }
+
+ @Test
+ public void matchShouldReturnAddressesWhenPrefixedHeaderName() throws MessagingException {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .condition(PREFIX)
+ .build();
+
+ matcher.init(matcherConfig);
+
+ Mail mail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_PREFIX_1, "true")
+ .build())
+ .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES2)
+ .build();
+
+ assertThat(matcher.match(mail)).containsAll(mail.getRecipients());
+ }
+
+ @Test
+ public void matchShouldReturnAddressesWhenExactlyPrefix() throws MessagingException {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .condition(PREFIX)
+ .build();
+
+ matcher.init(matcherConfig);
+
+ Mail mail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(PREFIX, "true")
+ .build())
+ .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES2)
+ .build();
+
+ assertThat(matcher.match(mail)).containsAll(mail.getRecipients());
+ }
+
+ @Test
+ public void matchShouldReturnEmptyWhenNoPrefixedHeader() throws MessagingException {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .condition(PREFIX)
+ .build();
+
+ matcher.init(matcherConfig);
+
+ Mail mail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_NO_PREFIX, "true")
+ .build())
+ .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES2)
+ .build();
+
+ assertThat(matcher.match(mail)).isEmpty();
+ }
+
+ @Test
+ public void matchShouldReturnAddressesWhenAtLeastOneHeaderPrefixed() throws MessagingException {
+ FakeMatcherConfig matcherConfig = FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .condition(PREFIX)
+ .build();
+
+ matcher.init(matcherConfig);
+
+ Mail mail = FakeMail.builder()
+ .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+ .addHeader(HEADER_NAME_PREFIX_1, "true")
+ .addHeader(HEADER_NAME_NO_PREFIX, "true")
+ .build())
+ .recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES2)
+ .build();
+
+ assertThat(matcher.match(mail)).containsAll(mail.getRecipients());
+ }
+
+ @Test
+ public void initShouldRejectEmptyPrefix() throws MessagingException {
+ expectedException.expect(MessagingException.class);
+
+ matcher.init(FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .condition("")
+ .build());
+ }
+
+ @Test
+ public void initShouldRejectNoCondition() throws MessagingException {
+ expectedException.expect(MessagingException.class);
+
+ matcher.init(FakeMatcherConfig.builder()
+ .matcherName("HasHeader")
+ .build());
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org