Author: matthieu Date: Mon Nov 2 13:47:30 2015 New Revision: 1711987 URL: http://svn.apache.org/viewvc?rev=1711987&view=rev Log: JAMES-1595 Mapping gain some domain handling
Added: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java Modified: james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTableMailet.java Modified: james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java?rev=1711987&r1=1711986&r2=1711987&view=diff ============================================================================== --- james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java (original) +++ james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mapping.java Mon Nov 2 13:47:30 2015 @@ -25,4 +25,8 @@ public interface Mapping { String asString(); + boolean hasDomain(); + + Mapping appendDomain(String domain); + } \ No newline at end of file Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java?rev=1711987&r1=1711986&r2=1711987&view=diff ============================================================================== --- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java (original) +++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingImpl.java Mon Nov 2 13:47:30 2015 @@ -21,6 +21,7 @@ package org.apache.james.rrt.lib; import com.google.common.base.Objects; +import com.google.common.base.Preconditions; public class MappingImpl implements Mapping { @@ -41,6 +42,17 @@ public class MappingImpl implements Mapp } @Override + public boolean hasDomain() { + return mapping.contains("@"); + } + + @Override + public Mapping appendDomain(String domain) { + Preconditions.checkNotNull(domain); + return new MappingImpl(mapping + "@" + domain); + } + + @Override public boolean equals(Object other) { if (other instanceof MappingImpl) { MappingImpl otherMapping = (MappingImpl) other; Added: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java?rev=1711987&view=auto ============================================================================== --- james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java (added) +++ james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingImplTest.java Mon Nov 2 13:47:30 2015 @@ -0,0 +1,69 @@ +/* + * 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.rrt.lib; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MappingImplTest { + + @Test + public void hasDomainshouldReturnTrueWhenMappingContainAtMark() { + assertThat(MappingImpl.of("a@b").hasDomain()).isTrue(); + } + + @Test + public void hasDomainshouldReturnFalseWhenMappingIsEmpty() { + assertThat(MappingImpl.of("").hasDomain()).isFalse(); + } + + @Test + public void hasDomainshouldReturnFalseWhenMappingIsBlank() { + assertThat(MappingImpl.of(" ").hasDomain()).isFalse(); + } + + @Test + public void hasDomainshouldReturnFalseWhenMappingDoesntContainAtMark() { + assertThat(MappingImpl.of("abc").hasDomain()).isFalse(); + } + + @Test + public void appendDomainShouldWorkOnValidDomain() { + assertThat(MappingImpl.of("abc").appendDomain("domain")).isEqualTo(MappingImpl.of("abc@domain")); + } + + @Test + public void appendDomainShouldWorkWhenMappingAlreadyContainsDomains() { + assertThat(MappingImpl.of("abc@d").appendDomain("domain")).isEqualTo(MappingImpl.of("abc@d@domain")); + } + + @Test(expected=NullPointerException.class) + public void appendDomainShouldThrowWhenNullDomain() { + MappingImpl.of("abc@d").appendDomain(null); + } + + @Test + public void appendDomainShouldWorkWhenEmptyDomain() { + assertThat(MappingImpl.of("abc").appendDomain("")).isEqualTo(MappingImpl.of("abc@")); + } + +} Modified: james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTableMailet.java URL: http://svn.apache.org/viewvc/james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTableMailet.java?rev=1711987&r1=1711986&r2=1711987&view=diff ============================================================================== --- james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTableMailet.java (original) +++ james/project/trunk/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/AbstractRecipientRewriteTableMailet.java Mon Nov 2 13:47:30 2015 @@ -31,6 +31,7 @@ import javax.mail.internet.MimeMessage; import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.api.DomainListException; +import org.apache.james.rrt.lib.Mapping; import org.apache.james.rrt.lib.Mappings; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; @@ -125,22 +126,22 @@ public abstract class AbstractRecipientR * @throws MessagingException */ protected Collection<MailAddress> handleMappings(Mappings mappings, MailAddress sender, MailAddress recipient, MimeMessage message) throws MessagingException { - Iterator<String> i = mappings.asStrings().iterator(); + Iterator<Mapping> i = mappings.iterator(); Collection<MailAddress> remoteRecipients = new ArrayList<MailAddress>(); Collection<MailAddress> localRecipients = new ArrayList<MailAddress>(); while (i.hasNext()) { - String rcpt = i.next(); + Mapping rcpt = i.next(); - if (!rcpt.contains("@")) { + if (!rcpt.hasDomain()) { // the mapping contains no domain name, use the default domain try { - rcpt = rcpt + "@" + domainList.getDefaultDomain(); + rcpt = rcpt.appendDomain(domainList.getDefaultDomain()); } catch (DomainListException e) { throw new MessagingException("Unable to access DomainList", e); } } - MailAddress nextMap = new MailAddress(rcpt); + MailAddress nextMap = new MailAddress(rcpt.asString()); if (getMailetContext().isLocalServer(nextMap.getDomain())) { localRecipients.add(nextMap); } else { --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org