JAMES-1877 Extract mailAddress conversion
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7f8cf9e9 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7f8cf9e9 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7f8cf9e9 Branch: refs/heads/master Commit: 7f8cf9e9f5c4f227759da77b82520df3e510fb9f Parents: fc1b1d3 Author: Benoit Tellier <[email protected]> Authored: Fri Dec 2 10:47:24 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Tue Jan 10 15:12:52 2017 +0700 ---------------------------------------------------------------------- .../InternetAddressConverter.java | 44 ++++++++++++++ .../mailets/remoteDelivery/MailDelivrer.java | 12 +--- .../InternetAddressConverterTest.java | 62 ++++++++++++++++++++ 3 files changed, 107 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/7f8cf9e9/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverter.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverter.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverter.java new file mode 100644 index 0000000..52bd2a6 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverter.java @@ -0,0 +1,44 @@ +/**************************************************************** + * 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.Collection; + +import javax.mail.internet.InternetAddress; + +import org.apache.mailet.MailAddress; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.FluentIterable; + +public class InternetAddressConverter { + + public static InternetAddress[] convert(Collection<MailAddress> recipients) { + Preconditions.checkNotNull(recipients); + return FluentIterable.from(recipients).transform(new Function<MailAddress, InternetAddress>() { + @Override + public InternetAddress apply(MailAddress input) { + return input.toInternetAddress(); + } + }).toArray(InternetAddress.class); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/7f8cf9e9/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java index bcfe330..941ef21 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java @@ -127,7 +127,7 @@ public class MailDelivrer { targetServers = getGatewaySMTPHostAddresses(configuration.getGatewayServer()); } - return doDeliver(mail, mail.getMessage(), convertToInetAddr(mail.getRecipients()), targetServers); + return doDeliver(mail, mail.getMessage(), InternetAddressConverter.convert(mail.getRecipients()), targetServers); } @SuppressWarnings("deprecation") @@ -312,16 +312,6 @@ public class MailDelivrer { } } - private InternetAddress[] convertToInetAddr(Collection<MailAddress> recipients) { - InternetAddress addr[] = new InternetAddress[recipients.size()]; - int j = 0; - for (Iterator<MailAddress> i = recipients.iterator(); i.hasNext(); j++) { - MailAddress rcpt = i.next(); - addr[j] = rcpt.toInternetAddress(); - } - return addr; - } - private ExecutionResult handleTemporaryResolutionException(Mail mail, String host) { ExecutionResult executionResult = ExecutionResult.temporaryFailure(new MessagingException("Temporary problem looking " + "up mail server for host: " + host + ". I cannot determine where to send this message.")); http://git-wip-us.apache.org/repos/asf/james-project/blob/7f8cf9e9/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverterTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverterTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverterTest.java new file mode 100644 index 0000000..9020533 --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/InternetAddressConverterTest.java @@ -0,0 +1,62 @@ +/**************************************************************** + * 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.internet.InternetAddress; + +import org.apache.mailet.MailAddress; +import org.apache.mailet.base.MailAddressFixture; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import com.google.common.collect.ImmutableList; + +public class InternetAddressConverterTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void convertShouldWorkWithEmptyAddressList() { + assertThat(InternetAddressConverter.convert(ImmutableList.<MailAddress>of())).isEmpty(); + } + + @Test + public void convertShouldThrowOnNullAddress() { + expectedException.expect(NullPointerException.class); + + InternetAddressConverter.convert(null); + } + + @Test + public void convertShouldWorkWithOneAddress() throws Exception { + assertThat(InternetAddressConverter.convert(ImmutableList.of(MailAddressFixture.ANY_AT_JAMES))) + .containsOnly(new InternetAddress(MailAddressFixture.ANY_AT_JAMES.asString())); + } + + @Test + public void convertShouldWorkWithTwoAddress() throws Exception { + assertThat(InternetAddressConverter.convert(ImmutableList.of(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES))) + .containsOnly(new InternetAddress(MailAddressFixture.ANY_AT_JAMES.asString()), new InternetAddress(MailAddressFixture.OTHER_AT_JAMES.asString())); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
