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:12 UTC
[3/3] james-project git commit: MAILET-159 Implement
HasHeaderWithPrefix
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