james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [2/3] james-project git commit: MAILET-159 Implement RemoveMimeHeaderByPrefix
Date Thu, 10 Aug 2017 11:26:11 GMT
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 <btellier@linagora.com>
Authored: Thu Aug 10 11:39:30 2017 +0700
Committer: benwa <btellier@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>
+ * &lt;mailet match="All" class="RemoveMimeHeaderByPrefix"&gt;
+ *   &lt;name&gt;X-APPLICATIVE-HEADER-&lt;/name&gt;
+ * &lt;/mailet&gt;
+ * </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


Mime
View raw message