JAMES-2065 add MailetAttributeListToMimeHeaders
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e9f8a931 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e9f8a931 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e9f8a931 Branch: refs/heads/master Commit: e9f8a93171ff89fd469e39bc2e13be670daa4fea Parents: d5bddef Author: Luc DUZAN <[email protected]> Authored: Thu Jun 22 18:27:15 2017 +0200 Committer: benwa <[email protected]> Committed: Tue Aug 29 08:12:04 2017 +0700 ---------------------------------------------------------------------- mailet/standard/pom.xml | 4 + .../MailAttributesListToMimeHeaders.java | 110 ++++++++ .../mailets/MailAttributesToMimeHeaders.java | 11 +- .../MailAttributesListToMimeHeadersTest.java | 282 +++++++++++++++++++ .../MailAttributesToMimeHeadersTest.java | 62 +++- 5 files changed, 462 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/e9f8a931/mailet/standard/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/standard/pom.xml b/mailet/standard/pom.xml index 5f1a588..9ebfd19 100644 --- a/mailet/standard/pom.xml +++ b/mailet/standard/pom.xml @@ -53,6 +53,10 @@ <version>${apache-mime4j.version}</version> </dependency> <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>james-server-util-java8</artifactId> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/e9f8a931/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeaders.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeaders.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeaders.java new file mode 100644 index 0000000..e42deb6 --- /dev/null +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeaders.java @@ -0,0 +1,110 @@ +/**************************************************************** + * 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.io.Serializable; +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.apache.james.util.OptionalConverter; +import org.apache.mailet.Mail; +import org.apache.mailet.base.GenericMailet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Strings; + +/** + * <p>Convert attributes of type Collection<String> to headers</p> + * + * <p>Sample configuration:</p> + * <pre><code> + * <mailet match="All" class="MailAttributesToMimeHeaders"> + * <simplemapping>org.apache.james.attribute1; + * headerName1</simplemapping> + * <simplemapping>org.apache.james.attribute2; + * headerName2</simplemapping> </mailet> + * </code></pre> + */ +public class MailAttributesListToMimeHeaders extends GenericMailet { + private static final Logger LOGGER = LoggerFactory.getLogger(MailAttributesListToMimeHeaders.class); + + private Map<String, String> attributeNameToHeader; + + @Override + public void init() throws MessagingException { + String simpleMappings = getInitParameter("simplemapping"); + if (Strings.isNullOrEmpty(simpleMappings)) { + throw new MessagingException("simplemapping is required"); + } + + attributeNameToHeader = MappingArgument.parse(simpleMappings); + } + + @Override + public void service(Mail mail) { + try { + MimeMessage message = mail.getMessage(); + attributeNameToHeader.entrySet() + .forEach(entry -> addAttributeToHeader(mail, message, entry)); + message.saveChanges(); + } catch (MessagingException e) { + LOGGER.warn("Exception while adding headers", e); + } + } + + private void addAttributeToHeader(Mail mail, MimeMessage message, Entry<String, String> entry) { + Serializable attribute = mail.getAttribute(entry.getKey()); + if (attribute instanceof Collection) { + Optional<Collection> values = Optional.of((Collection) attribute); + addCollectionToHeader(message, entry.getValue(), values); + } else { + if (attribute != null) { + LOGGER.warn("Can not add {} to headers. Expecting class Collection but got {}.", attribute, attribute.getClass()); + } + } + } + + private void addCollectionToHeader(MimeMessage message, String headerName, Optional<Collection> values) { + OptionalConverter.toStream(values) + .flatMap(Collection::stream) + .forEach(value -> addValueToHeader(message, headerName, value)); + } + + private void addValueToHeader(MimeMessage message, String headerName, Object value) { + try { + if (value instanceof String) { + message.addHeader(headerName, (String) value); + } else { + if (value != null) { + LOGGER.warn("Invalid type for value intended to be added as {} header. Expecting String but got {}", headerName, value.getClass()); + } + } + } catch (MessagingException e) { + LOGGER.warn("Could not add header {} with value {}", headerName, value); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/e9f8a931/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java index 666ba1d..03c882d 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/MailAttributesToMimeHeaders.java @@ -17,8 +17,6 @@ * under the License. * ****************************************************************/ - - package org.apache.james.transport.mailets; import java.util.Map; @@ -35,8 +33,8 @@ import org.slf4j.LoggerFactory; import com.google.common.base.Strings; /** - * <p>Convert attributes to headers</p> - * + * <p>Convert attributes of type String to headers</p> + * * <p>Sample configuration:</p> * <pre><code> * <mailet match="All" class="MailAttributesToMimeHeaders"> @@ -54,7 +52,10 @@ public class MailAttributesToMimeHeaders extends GenericMailet { @Override public void init() throws MessagingException { String simpleMappings = getInitParameter("simplemapping"); - Preconditions.checkArgument(!Strings.isNullOrEmpty(simpleMappings), "simplemapping is required"); + + if (Strings.isNullOrEmpty(simpleMappings)) { + throw new MessagingException("simplemapping is required"); + } mappings = MappingArgument.parse(simpleMappings); } http://git-wip-us.apache.org/repos/asf/james-project/blob/e9f8a931/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeadersTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeadersTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeadersTest.java new file mode 100644 index 0000000..2b21058 --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesListToMimeHeadersTest.java @@ -0,0 +1,282 @@ +/**************************************************************** + * 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 java.util.ArrayList; + +import javax.mail.MessagingException; + +import org.apache.mailet.Mailet; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailetConfig; +import org.apache.mailet.base.test.MailUtil; +import org.apache.mailet.base.test.MimeMessageBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.google.common.collect.ImmutableList; + +public class MailAttributesListToMimeHeadersTest { + + private static final String VALUE_1_1 = "test1.1"; + private static final String VALUE_1_2 = "test1.2"; + private static final String VALUE_2_1 = "test2.1"; + private static final String VALUE_2_2 = "test2.2"; + private static final ImmutableList<String> MAIL_ATTRIBUTE_VALUE1 = ImmutableList.of(VALUE_1_1, VALUE_1_2); + private static final ImmutableList<String> MAIL_ATTRIBUTE_VALUE2 = ImmutableList.of(VALUE_2_1, VALUE_2_2); + + private static final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test"; + private static final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test2"; + private static final String HEADER_NAME1 = "JUNIT"; + private static final String HEADER_NAME2 = "JUNIT2"; + + @Rule public ExpectedException expectedException = ExpectedException.none(); + + private Mailet mailet; + + @Before + public void setup() { + mailet = new MailAttributesListToMimeHeaders(); + } + + @Test + public void shouldThrowMessagingExceptionIfMappingIsNotGiven() throws MessagingException { + expectedException.expect(MessagingException.class); + + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldThrowMessagingExceptionIfMappingIsEmpty() throws MessagingException { + expectedException.expect(MessagingException.class); + + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemmapping", "") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldIgnoreAttributeOfMappingThatDoesNotExistOnTheMessage() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2 + + "," + "another.attribute" + "; " + "Another-Header") + .build(); + + mailet.init(mailetConfig); + + FakeMail mail = FakeMail.builder() + .mimeMessage(MailUtil.createMimeMessage()) + .attribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1) + .attribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2) + .build(); + + mailet.service(mail); + assertThat(mail.getMessage().getHeader("another.attribute")).isNull(); + } + + @Test + public void shouldWorkWithMappingWithASingleBinding() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1) + .build(); + + mailet.init(mailetConfig); + + FakeMail mail = FakeMail.builder() + .mimeMessage(MailUtil.createMimeMessage()) + .attribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1) + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)) + .containsExactly(VALUE_1_1, VALUE_1_2); + } + + @Test + public void shouldIgnoreNullValueInsideList() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1) + .build(); + + mailet.init(mailetConfig); + + ArrayList<String> listWithNull = new ArrayList<String>(); + listWithNull.add("1"); + listWithNull.add(null); + listWithNull.add("2"); + FakeMail mail = FakeMail.builder() + .mimeMessage(MailUtil.createMimeMessage()) + .attribute(MAIL_ATTRIBUTE_NAME1, listWithNull) + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)) + .containsExactly("1", "2"); + } + + @Test + public void shouldPutAttributesIntoHeadersWhenMappingDefined() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2 + + "," + "another.attribute" + "; " + "Another-Header") + .build(); + mailet.init(mailetConfig); + + FakeMail mail = FakeMail.builder() + .mimeMessage(MailUtil.createMimeMessage()) + .attribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1) + .attribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2) + .attribute("unmatched.attribute", "value") + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)) + .containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE1); + + assertThat(mail.getMessage().getHeader(HEADER_NAME2)) + .containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE2); + } + + @Test + public void shouldNotRemovePreviousAttributeValueWhenAttributeAlreadyPresent() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1) + .build(); + mailet.init(mailetConfig); + + String firstValue = "first value"; + FakeMail mail = FakeMail.builder() + .mimeMessage(MimeMessageBuilder.mimeMessageBuilder() + .addHeader(HEADER_NAME1, firstValue) + .build()) + .attribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1) + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)) + .containsOnly(VALUE_1_1, VALUE_1_2, firstValue); + } + + @Test + public void shouldFilterAttributeOfWrongClass() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2) + .build(); + mailet.init(mailetConfig); + + FakeMail mail = FakeMail.builder() + .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().build()) + .attribute(MAIL_ATTRIBUTE_NAME1, 3L) + .attribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2) + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)).isNull(); + assertThat(mail.getMessage().getHeader(HEADER_NAME2)) + .containsExactlyElementsOf(MAIL_ATTRIBUTE_VALUE2); + } + + + @Test + public void shouldFilterAttributeElementsOfWrongClass() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1) + .build(); + mailet.init(mailetConfig); + + String value = "value"; + FakeMail mail = FakeMail.builder() + .mimeMessage(MimeMessageBuilder.mimeMessageBuilder().build()) + .attribute(MAIL_ATTRIBUTE_NAME1, ImmutableList.of(3L, value)) + .build(); + + mailet.service(mail); + + assertThat(mail.getMessage().getHeader(HEADER_NAME1)).containsOnly(value); + } + + + @Test + public void shouldThrowAtInitWhenNoSemicolumnInConfigurationEntry() throws MessagingException { + expectedException.expect(IllegalArgumentException.class); + + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", "invalidConfigEntry") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldThrowAtInitWhenTwoSemicolumnsInConfigurationEntry() throws MessagingException { + expectedException.expect(IllegalArgumentException.class); + + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", "first;second;third") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldThrowAtInitWhenNoConfigurationEntry() throws MessagingException { + expectedException.expect(MessagingException.class); + + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .build(); + + mailet.init(mailetConfig); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/e9f8a931/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java index 657b975..fbec22e 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/MailAttributesToMimeHeadersTest.java @@ -17,10 +17,10 @@ * under the License. * ****************************************************************/ - package org.apache.james.transport.mailets; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; import javax.mail.MessagingException; @@ -55,6 +55,64 @@ public class MailAttributesToMimeHeadersTest { } @Test + public void shouldThrowMessagingExceptionIfMappingIsNotGiven() throws MessagingException { + expectedException.expect(MessagingException.class); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldThrowMessagingExceptionIfMappingIsEmpty() throws MessagingException { + expectedException.expect(MessagingException.class); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemmapping", "") + .build(); + + mailet.init(mailetConfig); + } + + @Test + public void shouldIgnoreAttributeOfMappingThatDoesNotExistOnTheMessage() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1 + + "," + MAIL_ATTRIBUTE_NAME2 + "; " + HEADER_NAME2 + + "," + "another.attribute" + "; " + "Another-Header") + .build(); + + mailet.init(mailetConfig); + + FakeMail mockedMail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage()); + mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1); + mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME2, MAIL_ATTRIBUTE_VALUE2); + + mailet.service(mockedMail); + assertThat(mockedMail.getMessage().getHeader("another.attribute")).isNull(); + } + + @Test + public void shouldWorkWithMappingWithASingleBinding() throws MessagingException { + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .mailetName("Test") + .setProperty("simplemapping", + MAIL_ATTRIBUTE_NAME1 + "; " + HEADER_NAME1) + .build(); + + mailet.init(mailetConfig); + + FakeMail mockedMail = MailUtil.createMockMail2Recipients(MailUtil.createMimeMessage()); + mockedMail.setAttribute(MAIL_ATTRIBUTE_NAME1, MAIL_ATTRIBUTE_VALUE1); + + mailet.service(mockedMail); + assertThat(mockedMail.getMessage().getHeader(HEADER_NAME1)).containsExactly(MAIL_ATTRIBUTE_VALUE1); + } + + @Test public void shouldPutAttributesIntoHeadersWhenMappingDefined() throws MessagingException { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName("Test") @@ -120,7 +178,7 @@ public class MailAttributesToMimeHeadersTest { FakeMailetConfig mailetConfig = FakeMailetConfig.builder() .mailetName("Test") .build(); - expectedException.expect(IllegalArgumentException.class); + expectedException.expect(MessagingException.class); mailet.init(mailetConfig); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
