JAMES-1877 Extract Delay
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/1193b6b4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/1193b6b4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/1193b6b4 Branch: refs/heads/master Commit: 1193b6b4a542a0cd61bd2e7b74567b3a92207554 Parents: 03fc53a Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Nov 29 10:56:27 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 10 14:35:31 2017 +0700 ---------------------------------------------------------------------- .../james/transport/mailets/RemoteDelivery.java | 118 +------------ .../transport/mailets/remoteDelivery/Delay.java | 166 +++++++++++++++++++ .../mailets/remoteDelivery/DelayTest.java | 96 +++++++++++ 3 files changed, 264 insertions(+), 116 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/1193b6b4/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java index 89a4987..b23f79d 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/RemoteDelivery.java @@ -39,8 +39,6 @@ import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; import java.util.concurrent.TimeUnit; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.inject.Inject; import javax.mail.Address; @@ -65,10 +63,9 @@ import org.apache.james.queue.api.MailQueue; import org.apache.james.queue.api.MailQueue.MailQueueException; import org.apache.james.queue.api.MailQueue.MailQueueItem; import org.apache.james.queue.api.MailQueueFactory; +import org.apache.james.transport.mailets.remoteDelivery.Delay; import org.apache.james.transport.mailets.remoteDelivery.HeloNameProvider; import org.apache.james.transport.mailets.remoteDelivery.RemoteDeliverySocketFactory; -import org.apache.james.transport.util.Patterns; -import org.apache.james.util.TimeConverter; import org.apache.mailet.HostAddress; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -151,17 +148,6 @@ import com.sun.mail.smtp.SMTPTransport; @SuppressWarnings("deprecation") public class RemoteDelivery extends GenericMailet implements Runnable { - /** - * Default Delay Time (Default is 6*60*60*1000 Milliseconds (6 hours)). - */ - private static final long DEFAULT_DELAY_TIME = 21600000; - - /** - * Pattern to match [attempts*]delay[units]. - */ - private static final String PATTERN_STRING = "\\s*([0-9]*\\s*[\\*])?\\s*([0-9]+)\\s*([a-z,A-Z]*)\\s*"; - - private static final Pattern PATTERN = Patterns.compilePatternUncheckedException(PATTERN_STRING); private static final String OUTGOING_MAILS = "outgoingMails"; private final DNSService dnsServer; @@ -502,111 +488,11 @@ public class RemoteDelivery extends GenericMailet implements Runnable { */ private long getNextDelay(int retry_count) { if (retry_count > delayTimes.length) { - return DEFAULT_DELAY_TIME; + return Delay.DEFAULT_DELAY_TIME; } return delayTimes[retry_count - 1]; } - /** - * This class is used to hold a delay time and its corresponding number of - * retries. - */ - private final static class Delay { - private int attempts = 1; - - private long delayTime = DEFAULT_DELAY_TIME; - - /** - * <p> - * This constructor expects Strings of the form - * "[attempt\*]delaytime[unit]". - * </p> - * <p> - * The optional attempt is the number of tries this delay should be used - * (default = 1). The unit, if present, must be one of - * (msec,sec,minute,hour,day). The default value of unit is 'msec'. - * </p> - * <p> - * The constructor multiplies the delaytime by the relevant multiplier - * for the unit, so the delayTime instance variable is always in msec. - * </p> - * - * @param initString the string to initialize this Delay object from - */ - public Delay(String initString) throws MessagingException { - // Default unit value to 'msec'. - String unit = "msec"; - - Matcher res = PATTERN.matcher(initString); - if (res.matches()) { - // The capturing groups will now hold: - // at 1: attempts * (if present) - // at 2: delaytime - // at 3: unit (if present) - if (res.group(1) != null && !res.group(1).equals("")) { - // We have an attempt * - String attemptMatch = res.group(1); - - // Strip the * and whitespace. - attemptMatch = attemptMatch.substring(0, attemptMatch.length() - 1).trim(); - attempts = Integer.parseInt(attemptMatch); - } - - delayTime = Long.parseLong(res.group(2)); - - if (!res.group(3).equals("")) { - // We have a value for 'unit'. - unit = res.group(3).toLowerCase(Locale.US); - } - } else { - throw new MessagingException(initString + " does not match " + PATTERN_STRING); - } - - // calculate delayTime. - try { - delayTime = TimeConverter.getMilliSeconds(delayTime, unit); - } catch (NumberFormatException e) { - throw new MessagingException(e.getMessage()); - } - } - - /** - * This constructor makes a default Delay object with attempts = 1 and - * delayTime = DEFAULT_DELAY_TIME. - */ - public Delay() { - } - - /** - * @return the delayTime for this Delay - */ - public long getDelayTime() { - return delayTime; - } - - /** - * @return the number attempts this Delay should be used. - */ - public int getAttempts() { - return attempts; - } - - /** - * Set the number attempts this Delay should be used. - */ - public void setAttempts(int value) { - attempts = value; - } - - /** - * Pretty prints this Delay - */ - @Override - public String toString() { - return getAttempts() + "*" + getDelayTime() + "msecs"; - } - } - @Override public String getMailetInfo() { return "RemoteDelivery Mailet"; http://git-wip-us.apache.org/repos/asf/james-project/blob/1193b6b4/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Delay.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Delay.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Delay.java new file mode 100644 index 0000000..c066a63 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/Delay.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.remoteDelivery; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.mail.MessagingException; + +import org.apache.james.transport.util.Patterns; +import org.apache.james.util.TimeConverter; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Objects; + +/** + * This class is used to hold a delay time and its corresponding number of + * retries. + */ +public class Delay { + + /** + * Default Delay Time (Default is 6*60*60*1000 Milliseconds (6 hours)). + */ + public static final long DEFAULT_DELAY_TIME = 21600000; + public static final int DEFAULT_ATTEMPTS = 1; + /** + * Pattern to match [attempts*]delay[units]. + */ + private static final String PATTERN_STRING = "\\s*([0-9]*\\s*[\\*])?\\s*([0-9]+)\\s*([a-z,A-Z]*)\\s*"; + private static final Pattern PATTERN = Patterns.compilePatternUncheckedException(PATTERN_STRING); + + private int attempts = DEFAULT_ATTEMPTS; + + private long delayTime = DEFAULT_DELAY_TIME; + + /** + * <p> + * This constructor expects Strings of the form + * "[attempt\*]delaytime[unit]". + * </p> + * <p> + * The optional attempt is the number of tries this delay should be used + * (default = 1). The unit, if present, must be one of + * (msec,sec,minute,hour,day). The default value of unit is 'msec'. + * </p> + * <p> + * The constructor multiplies the delaytime by the relevant multiplier + * for the unit, so the delayTime instance variable is always in msec. + * </p> + * + * @param initString the string to initialize this Delay object from + */ + public Delay(String initString) throws MessagingException { + // Default unit value to 'msec'. + String unit = "msec"; + + Matcher res = PATTERN.matcher(initString); + if (res.matches()) { + // The capturing groups will now hold: + // at 1: attempts * (if present) + // at 2: delaytime + // at 3: unit (if present) + if (res.group(1) != null && !res.group(1).equals("")) { + // We have an attempt * + String attemptMatch = res.group(1); + + // Strip the * and whitespace. + attemptMatch = attemptMatch.substring(0, attemptMatch.length() - 1).trim(); + attempts = Integer.parseInt(attemptMatch); + } + + delayTime = Long.parseLong(res.group(2)); + + if (!res.group(3).equals("")) { + // We have a value for 'unit'. + unit = res.group(3).toLowerCase(Locale.US); + } + } else { + throw new MessagingException(initString + " does not match " + PATTERN_STRING); + } + + // calculate delayTime. + try { + delayTime = TimeConverter.getMilliSeconds(delayTime, unit); + } catch (NumberFormatException e) { + throw new MessagingException(e.getMessage()); + } + } + + /** + * This constructor makes a default Delay object with attempts = 1 and + * delayTime = DEFAULT_DELAY_TIME. + */ + public Delay() { + } + + @VisibleForTesting + Delay(int attempts, long delayTime) { + this.attempts = attempts; + this.delayTime = delayTime; + } + + /** + * @return the delayTime for this Delay + */ + public long getDelayTime() { + return delayTime; + } + + /** + * @return the number attempts this Delay should be used. + */ + public int getAttempts() { + return attempts; + } + + /** + * Set the number attempts this Delay should be used. + */ + public void setAttempts(int value) { + attempts = value; + } + + /** + * Pretty prints this Delay + */ + @Override + public String toString() { + return getAttempts() + "*" + getDelayTime() + "msecs"; + } + + @Override + public boolean equals(Object o) { + if (o instanceof Delay) { + Delay that = (Delay) o; + + return Objects.equal(this.attempts, that.attempts) + && Objects.equal(this.delayTime, that.delayTime); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(attempts, delayTime); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/1193b6b4/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DelayTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DelayTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DelayTest.java new file mode 100644 index 0000000..92ce97b --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DelayTest.java @@ -0,0 +1,96 @@ +/**************************************************************** + * 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.remoteDelivery; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.mail.MessagingException; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class DelayTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void defaultConstructorShouldConstructDefaultDelay() { + assertThat(new Delay()) + .isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, Delay.DEFAULT_DELAY_TIME)); + } + + @Test + public void stringConstructorShouldWorkForNumbers() throws Exception { + assertThat(new Delay("36")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 36)); + } + + @Test + public void stringConstructorShouldWorkForZero() throws Exception { + assertThat(new Delay("0")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 0)); + } + + @Test + public void stringConstructorShouldDefaultToZeroForNegatives() throws Exception { + assertThat(new Delay("0")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 0)); + } + + @Test + public void stringConstructorShouldWorkForNumberAndSecond() throws Exception { + assertThat(new Delay("1s")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 1000)); + } + + @Test + public void stringConstructorShouldWorkForNumberAndAttempts() throws Exception { + assertThat(new Delay("2*36")).isEqualTo(new Delay(2, 36)); + } + + @Test + public void stringConstructorShouldWorkForNumberAndZeroAttempts() throws Exception { + assertThat(new Delay("0*36")).isEqualTo(new Delay(0, 36)); + } + + @Test + public void stringConstructorShouldThrowOnNegativeAttempts() throws Exception { + expectedException.expect(MessagingException.class); + + new Delay("-1*36"); + } + + @Test + public void stringConstructorShouldWorkForNumberAttemptsAndUnit() throws Exception { + assertThat(new Delay("2*36s")).isEqualTo(new Delay(2, 36000)); + } + + @Test + public void stringConstructorShouldThrowOnInvalidInput() throws Exception { + expectedException.expect(MessagingException.class); + + new Delay("invalid"); + } + + @Test + public void stringConstructorShouldThrowOnInvalidUnit() throws Exception { + expectedException.expect(MessagingException.class); + + new Delay("36invalid"); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org