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/04/10 10:23:04 UTC

[1/2] james-project git commit: MAILET-155 Add a utility for integer parsing as part of mailets

Repository: james-project
Updated Branches:
  refs/heads/master e1c486e24 -> f8506f768


MAILET-155 Add a utility for integer parsing as part of mailets


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

Branch: refs/heads/master
Commit: 49b99519dbbca70f969b4b09629bd24e528473ed
Parents: e1c486e
Author: benwa <bt...@linagora.com>
Authored: Fri Apr 7 12:04:43 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Mon Apr 10 17:22:12 2017 +0700

----------------------------------------------------------------------
 .../java/org/apache/mailet/base/MailetUtil.java | 36 ++++++++
 .../org/apache/mailet/base/MailetUtilTest.java  | 90 +++++++++++++++++++-
 .../james/transport/matchers/RelayLimit.java    | 10 +--
 .../james/transport/matchers/TooManyLines.java  | 21 +----
 .../transport/matchers/TooManyRecipients.java   | 17 +---
 5 files changed, 131 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java b/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java
index 642f760..ae16f7c 100644
--- a/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java
+++ b/mailet/base/src/main/java/org/apache/mailet/base/MailetUtil.java
@@ -21,9 +21,12 @@
 
 package org.apache.mailet.base;
 
+import javax.mail.MessagingException;
+
 import org.apache.mailet.MailetConfig;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Strings;
 
 
 /**
@@ -107,4 +110,37 @@ public class MailetUtil {
         }
         return Optional.absent();
     }
+
+    public static int getInitParameterAsStrictlyPositiveInteger(String condition, int defaultValue) throws MessagingException {
+        String defaultStringValue = String.valueOf(defaultValue);
+        return getInitParameterAsStrictlyPositiveInteger(condition, Optional.of(defaultStringValue));
+    }
+
+    public static int getInitParameterAsStrictlyPositiveInteger(String condition) throws MessagingException {
+        return getInitParameterAsStrictlyPositiveInteger(condition, Optional.<String>absent());
+    }
+
+    public static int getInitParameterAsStrictlyPositiveInteger(String condition, Optional<String> defaultValue) throws MessagingException {
+        Optional<String> value = Optional.fromNullable(condition)
+            .or(defaultValue);
+
+        if (Strings.isNullOrEmpty(value.orNull())) {
+            throw new MessagingException("Condition is required. It should be a strictly positive integer");
+        }
+
+        int valueAsInt = tryParseInteger(value.orNull());
+
+        if (valueAsInt < 1) {
+            throw new MessagingException("Expecting condition to be a strictly positive integer. Got " + value.get());
+        }
+        return valueAsInt;
+    }
+
+    private static int tryParseInteger(String value) throws MessagingException {
+        try {
+            return Integer.valueOf(value);
+        } catch (NumberFormatException e) {
+            throw new MessagingException("Expecting condition to be a strictly positive integer. Got " + value);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java b/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java
index cb56b99..be11e15 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/MailetUtilTest.java
@@ -22,13 +22,20 @@ package org.apache.mailet.base;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
 
+import javax.mail.MessagingException;
+
 import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class MailetUtilTest {
 
     private static final String A_PARAMETER = "aParameter";
-    public static final String DEFAULT_VALUE = "default";
+    public static final int DEFAULT_VALUE = 36;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
 
     @Test
     public void getInitParameterShouldReturnTrueWhenIsValueTrueLowerCase() {
@@ -85,6 +92,87 @@ public class MailetUtilTest {
         assertThat(MailetUtil.getInitParameter(mailetConfig, A_PARAMETER)).isAbsent();
     }
 
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnEmptyString() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("");
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnNull() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger(null);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnInvalid() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("invalid");
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnNegativeNumber() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("-1");
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldThrowOnZero() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("0");
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerShouldParseCorrectValue() throws Exception {
+        assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger("1"))
+            .isEqualTo(1);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnEmptyString() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("", DEFAULT_VALUE);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldReturnDefaultValueOnNull() throws Exception {
+        assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger(null, DEFAULT_VALUE))
+            .isEqualTo(DEFAULT_VALUE);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnInvalid() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("invalid", DEFAULT_VALUE);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnNegativeNumber() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("-1", DEFAULT_VALUE);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldThrowOnZero() throws Exception {
+        expectedException.expect(MessagingException.class);
+
+        MailetUtil.getInitParameterAsStrictlyPositiveInteger("0", DEFAULT_VALUE);
+    }
+
+    @Test
+    public void getInitParameterAsStrictlyPositiveIntegerWithDefaultValueShouldParseCorrectValue() throws Exception {
+        assertThat(MailetUtil.getInitParameterAsStrictlyPositiveInteger("1", DEFAULT_VALUE))
+            .isEqualTo(1);
+    }
+
     private boolean getParameterValued(String value, boolean defaultValue) {
         FakeMailetConfig mailetConfig = FakeMailetConfig.builder()
             .setProperty(A_PARAMETER, value)

http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java
index 2b5d52c..ddd91a0 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/RelayLimit.java
@@ -21,6 +21,7 @@
 
 package org.apache.james.transport.matchers;
 
+import org.apache.mailet.base.MailetUtil;
 import org.apache.mailet.base.RFC2822Headers;
 import org.apache.mailet.base.GenericMatcher;
 import org.apache.mailet.Mail;
@@ -42,14 +43,7 @@ public class RelayLimit extends GenericMatcher {
     int limit = 30;
 
     public void init() throws MessagingException {
-        try {
-            limit = Integer.parseInt(getCondition());
-        } catch (NumberFormatException e) {
-            throw new MessagingException("No valid integer: " + getCondition());
-        }
-        if (limit <= 0) {
-            throw new MessagingException("Relay limit should be superior to 0");
-        }
+        limit = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition());
     }
 
     public Collection<MailAddress> match(Mail mail) throws javax.mail.MessagingException {

http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
index ef2743f..aa37378 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyLines.java
@@ -26,6 +26,7 @@ import javax.mail.MessagingException;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.GenericMatcher;
+import org.apache.mailet.base.MailetUtil;
 
 import com.google.common.collect.ImmutableList;
 
@@ -35,25 +36,7 @@ public class TooManyLines extends GenericMatcher {
 
     @Override
     public void init() throws MessagingException {
-        String condition = getCondition();
-
-        maximumLineCount = parseCondition(condition);
-
-        if (maximumLineCount < 1) {
-            throw new MessagingException("Condition should be strictly positive");
-        }
-    }
-
-    private int parseCondition(String condition) throws MessagingException {
-        if (condition == null) {
-            throw new MessagingException("Missing condition");
-        }
-
-        try {
-            return Integer.valueOf(condition);
-        } catch (NumberFormatException e) {
-            throw new MessagingException("Invalid formating. Condition is expected to be an integer");
-        }
+        maximumLineCount = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/james-project/blob/49b99519/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
index 51895a3..abaeb96 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/matchers/TooManyRecipients.java
@@ -26,6 +26,7 @@ import javax.mail.MessagingException;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
 import org.apache.mailet.base.GenericMatcher;
+import org.apache.mailet.base.MailetUtil;
 
 import com.google.common.collect.ImmutableList;
 
@@ -35,21 +36,7 @@ public class TooManyRecipients extends GenericMatcher {
 
     @Override
     public void init() throws MessagingException {
-        String condition = getCondition();
-
-        if (condition == null) {
-            throw new MessagingException("it should have a condition");
-        }
-
-        try {
-            maximumRecipientCount = Integer.parseInt(condition);
-        } catch (Exception e) {
-            throw new MessagingException("Condition should be a number");
-        }
-
-        if (maximumRecipientCount < 1) {
-            throw new MessagingException("it should be positive condition");
-        }
+        maximumRecipientCount = MailetUtil.getInitParameterAsStrictlyPositiveInteger(getCondition());
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


[2/2] james-project git commit: MAILET-154 Adding more capabilities to MimeMessageBuilder

Posted by bt...@apache.org.
MAILET-154 Adding more capabilities to MimeMessageBuilder

Also correct setText method


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

Branch: refs/heads/master
Commit: f8506f768406489927d23318d0b136d3dbfd0d03
Parents: 49b9951
Author: benwa <bt...@linagora.com>
Authored: Fri Apr 7 09:50:42 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Mon Apr 10 17:22:16 2017 +0700

----------------------------------------------------------------------
 .../org/apache/mailet/base/test/MailUtil.java   |  2 +-
 .../mailet/base/test/MimeMessageBuilder.java    | 37 +++++++++++++++-----
 .../mailets/ICALToJsonAttributeTest.java        |  4 +--
 .../transport/mailets/StripAttachmentTest.java  |  4 +--
 4 files changed, 32 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/f8506f76/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java b/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
index 4758d77..17f26f6 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/MailUtil.java
@@ -72,7 +72,7 @@ public class MailUtil {
     private static MimeMessage createMimeMessage(String headerName, String headerValue, String subject) throws MessagingException {
         MimeMessageBuilder mimeMessageBuilder = MimeMessageBuilder.mimeMessageBuilder()
             .addToRecipient(RECIPIENT)
-            .setFrom(SENDER)
+            .addFrom(SENDER)
             .setSubject(subject);
         if (headerName != null) {
             mimeMessageBuilder.addHeader(headerName, headerValue);

http://git-wip-us.apache.org/repos/asf/james-project/blob/f8506f76/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
----------------------------------------------------------------------
diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
index c75136d..666803a 100644
--- a/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
+++ b/mailet/base/src/test/java/org/apache/mailet/base/test/MimeMessageBuilder.java
@@ -209,8 +209,8 @@ public class MimeMessageBuilder {
     private Optional<String> text = Optional.absent();
     private Optional<String> subject = Optional.absent();
     private Optional<InternetAddress> sender = Optional.absent();
-    private Optional<InternetAddress> from = Optional.absent();
     private Optional<MimeMultipart> content = Optional.absent();
+    private ImmutableList.Builder<InternetAddress> from = ImmutableList.builder();
     private ImmutableList.Builder<InternetAddress> cc = ImmutableList.builder();
     private ImmutableList.Builder<InternetAddress> to = ImmutableList.builder();
     private ImmutableList.Builder<InternetAddress> bcc = ImmutableList.builder();
@@ -236,8 +236,13 @@ public class MimeMessageBuilder {
         return this;
     }
 
-    public MimeMessageBuilder setFrom(String from) throws AddressException {
-        this.from = Optional.of(new InternetAddress(from));
+    public MimeMessageBuilder addFrom(String from) throws AddressException {
+        this.from.add(new InternetAddress(from));
+        return this;
+    }
+
+    public MimeMessageBuilder addFrom(InternetAddress... from) throws AddressException {
+        this.from.addAll(Arrays.asList(from));
         return this;
     }
 
@@ -272,6 +277,21 @@ public class MimeMessageBuilder {
         return this;
     }
 
+    public MimeMessageBuilder addToRecipient(InternetAddress... tos) throws AddressException {
+        this.to.addAll(Arrays.asList(tos));
+        return this;
+    }
+
+    public MimeMessageBuilder addCcRecipient(InternetAddress... ccs) throws AddressException {
+        this.cc.addAll(Arrays.asList(ccs));
+        return this;
+    }
+
+    public MimeMessageBuilder addBccRecipient(InternetAddress... bccs) throws AddressException {
+        this.bcc.addAll(Arrays.asList(bccs));
+        return this;
+    }
+
     public MimeMessageBuilder setContent(MimeMultipart mimeMultipart) {
         this.content = Optional.of(mimeMultipart);
         return this;
@@ -305,9 +325,7 @@ public class MimeMessageBuilder {
         Preconditions.checkState(!(text.isPresent() && content.isPresent()), "Can not get at the same time a text and a content");
         MimeMessage mimeMessage = new MimeMessage(Session.getInstance(new Properties()));
         if (text.isPresent()) {
-            BodyPart bodyPart = new MimeBodyPart();
-            bodyPart.setText(text.get());
-            mimeMessage.setContent(bodyPart, "text/plain");
+            mimeMessage.setContent(text.get(), "text/plain");
         }
         if (content.isPresent()) {
             mimeMessage.setContent(content.get());
@@ -315,12 +333,13 @@ public class MimeMessageBuilder {
         if (sender.isPresent()) {
             mimeMessage.setSender(sender.get());
         }
-        if (from.isPresent()) {
-            mimeMessage.setFrom(from.get());
-        }
         if (subject.isPresent()) {
             mimeMessage.setSubject(subject.get());
         }
+        ImmutableList<InternetAddress> fromAddresses = from.build();
+        if (!fromAddresses.isEmpty()) {
+            mimeMessage.addFrom(fromAddresses.toArray(new InternetAddress[fromAddresses.size()]));
+        }
         List<InternetAddress> toAddresses = to.build();
         if (!toAddresses.isEmpty()) {
             mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses.toArray(new InternetAddress[toAddresses.size()]));

http://git-wip-us.apache.org/repos/asf/james-project/blob/f8506f76/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
----------------------------------------------------------------------
diff --git a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
index 76147ee..5538b15 100644
--- a/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
+++ b/mailet/icalendar/src/test/java/org/apache/james/transport/mailets/ICALToJsonAttributeTest.java
@@ -444,7 +444,7 @@ public class ICALToJsonAttributeTest {
             .sender(SENDER)
             .recipient(recipient)
             .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
-                .setFrom(from)
+                .addFrom(from)
                 .build())
             .attribute(ICALToJsonAttribute.DEFAULT_SOURCE_ATTRIBUTE_NAME, icals)
             .attribute(ICALToJsonAttribute.DEFAULT_RAW_SOURCE_ATTRIBUTE_NAME, rawIcals)
@@ -515,7 +515,7 @@ public class ICALToJsonAttributeTest {
         Mail mail = FakeMail.builder()
             .recipient(recipient)
             .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
-                .setFrom(from)
+                .addFrom(from)
                 .build())
             .attribute(ICALToJsonAttribute.DEFAULT_SOURCE_ATTRIBUTE_NAME, icals)
             .attribute(ICALToJsonAttribute.DEFAULT_RAW_SOURCE_ATTRIBUTE_NAME, rawIcals)

http://git-wip-us.apache.org/repos/asf/james-project/blob/f8506f76/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
index 5995f98..271e45e 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/StripAttachmentTest.java
@@ -53,7 +53,6 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 
-import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
@@ -125,8 +124,7 @@ public class StripAttachmentTest {
         mailet.service(mail);
 
         assertThat(mail).isEqualToComparingFieldByField(expectedMail);
-        BodyPart content = (BodyPart) mail.getMessage().getContent();
-        assertThat(IOUtils.toString(content.getInputStream(), Charsets.UTF_8)).isEqualTo("simple text");
+        assertThat(mail.getMessage().getContent()).isEqualTo("simple text");
     }
     
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org