james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [1/9] james-project git commit: JAMES-2157 Added HasMimeType matcher and tests.
Date Tue, 10 Oct 2017 15:17:53 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 4b1552dd8 -> c590901a9


JAMES-2157 Added HasMimeType matcher and tests.

Added HasMimeType matcher and tests.


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/43386fd8
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/43386fd8
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/43386fd8

Branch: refs/heads/master
Commit: 43386fd83b9289f729790faff5462c18be90808b
Parents: 651a03d
Author: Jasmin <jasmin.patel@bigbasket.com>
Authored: Mon Oct 2 11:20:26 2017 +0530
Committer: benwa <btellier@linagora.com>
Committed: Tue Oct 3 10:58:10 2017 +0700

----------------------------------------------------------------------
 .../james/transport/matchers/HasMimeType.java   | 74 ++++++++++++++++
 .../transport/matchers/HasMimeTypeTest.java     | 93 ++++++++++++++++++++
 2 files changed, 167 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/43386fd8/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
new file mode 100644
index 0000000..c97b6d8
--- /dev/null
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/HasMimeType.java
@@ -0,0 +1,74 @@
+/****************************************************************
+ * 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 com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.apache.james.core.MailAddress;
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMatcher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.util.Collection;
+import java.util.Optional;
+import java.util.Set;
+
+
+/**
+ * <p>This matcher checks if the content type matches.</p>
+ *
+ * use: <pre><code><mailet match="HasMimeType=text/plain,text/html" class="..."
/></code></pre>
+ */
+public class HasMimeType extends GenericMatcher {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(HasMimeType.class);
+
+    private Set<String> acceptedContentTypes;
+
+    public void init() throws javax.mail.MessagingException {
+        acceptedContentTypes = ImmutableSet.copyOf(Splitter.on(",").trimResults().split(getCondition()));
+    }
+
+    public Collection<MailAddress> match(Mail mail) throws javax.mail.MessagingException
{
+        Optional<String> mimeTypes = getMimeTypeFromMessage(mail.getMessage());
+
+        return mimeTypes.filter(acceptedContentTypes::contains)
+                .map(any -> mail.getRecipients())
+                .orElse(ImmutableList.of());
+    }
+
+    private static Optional<String> getMimeTypeFromMessage(MimeMessage message) throws
MessagingException {
+        try {
+            return Optional.of(new MimeType(message.getContentType()).getBaseType());
+        } catch (MimeTypeParseException e) {
+            LOGGER.warn(String.format("Error while parsing message's mimeType %s", message.getContentType()),
e);
+            return Optional.empty();
+        }
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/james-project/blob/43386fd8/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
new file mode 100644
index 0000000..23dd533
--- /dev/null
+++ b/mailet/standard/src/test/java/org/apache/james/transport/matchers/HasMimeTypeTest.java
@@ -0,0 +1,93 @@
+/****************************************************************
+ * 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 static org.junit.Assert.assertEquals;
+
+import org.apache.james.core.MailAddress;
+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.Test;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import java.util.Collections;
+
+public class HasMimeTypeTest {
+
+    private static final String RECIPIENT = "test@james.apache.org";
+    private static final String FROM = "test@james.apache.org";
+    private static final String MIME_TYPES = "multipart/mixed";
+    private static final String NON_MATCHING_MIME_TYPES = "text/plain, application/zip";
+
+    private HasMimeType matcher;
+    private FakeMail mail;
+
+    @Before
+    public void setUp() throws Exception {
+        matcher = new HasMimeType();
+        MimeMessage message = MimeMessageBuilder.mimeMessageBuilder()
+                .setMultipartWithBodyParts(
+                        MimeMessageBuilder.bodyPartBuilder()
+                                .data("simple text")
+                                .disposition("text")
+                                .build(),
+                        MimeMessageBuilder.bodyPartBuilder()
+                                .filename("text_file.txt")
+                                .disposition("attachment")
+                                .build(),
+                        MimeMessageBuilder.bodyPartBuilder()
+                                .type("application/zip")
+                                .filename("zip_file.zip")
+                                .disposition("attachment")
+                                .build())
+                .setSubject("test")
+                .build();
+
+        mail = FakeMail.builder()
+                .mimeMessage(message)
+                .sender(new MailAddress(FROM))
+                .recipient(new MailAddress(RECIPIENT))
+                .build();
+    }
+
+    @Test
+    public void hasMimeType() throws MessagingException {
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("HasMimeType")
+                .condition(MIME_TYPES)
+                .build());
+
+        assertThat(matcher.match(mail)).containsAll(mail.getRecipients());
+    }
+
+    @Test
+    public void doesNotHaveMimeType() throws MessagingException{
+        matcher.init(FakeMatcherConfig.builder()
+                .matcherName("HasMimeType")
+                .condition(NON_MATCHING_MIME_TYPES)
+                .build());
+
+        assertThat(matcher.match(mail)).isEmpty();
+    }
+}


---------------------------------------------------------------------
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