This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit a0545c7102f6c618074f5c14323850dd4d9691ba Author: Benoit Tellier <[email protected]> AuthorDate: Wed May 15 11:17:09 2019 +0700 JAMES-2149 Introduce DomainAliasService --- .../james/webadmin/routes/DomainsRoutes.java | 75 ++++++----------- .../james/webadmin/service/DomainAliasService.java | 98 ++++++++++++++++++++++ .../james/webadmin/routes/DomainsRoutesTest.java | 9 +- 3 files changed, 129 insertions(+), 53 deletions(-) diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java index cd047f9..de7300d 100644 --- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DomainsRoutes.java @@ -34,24 +34,19 @@ import javax.ws.rs.Produces; import org.apache.james.core.Domain; import org.apache.james.domainlist.api.DomainList; import org.apache.james.domainlist.api.DomainListException; -import org.apache.james.rrt.api.RecipientRewriteTable; import org.apache.james.rrt.api.RecipientRewriteTableException; -import org.apache.james.rrt.lib.Mapping; -import org.apache.james.rrt.lib.MappingSource; -import org.apache.james.webadmin.Constants; import org.apache.james.webadmin.Routes; import org.apache.james.webadmin.dto.DomainAliasResponse; +import org.apache.james.webadmin.service.DomainAliasService; import org.apache.james.webadmin.utils.ErrorResponder; import org.apache.james.webadmin.utils.ErrorResponder.ErrorType; import org.apache.james.webadmin.utils.JsonTransformer; +import org.apache.james.webadmin.utils.Responses; import org.eclipse.jetty.http.HttpStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.github.fge.lambdas.Throwing; -import com.github.steveash.guavate.Guavate; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import io.swagger.annotations.Api; @@ -69,10 +64,6 @@ import spark.Service; @Path(DomainsRoutes.DOMAINS) @Produces("application/json") public class DomainsRoutes implements Routes { - @FunctionalInterface - interface MappingOperation { - void perform(MappingSource mappingSource, Mapping mapping) throws RecipientRewriteTableException; - } public static final String DOMAINS = "/domains"; private static final Logger LOGGER = LoggerFactory.getLogger(DomainsRoutes.class); @@ -86,14 +77,14 @@ public class DomainsRoutes implements Routes { private static final int MAXIMUM_DOMAIN_SIZE = 256; private final DomainList domainList; - private final RecipientRewriteTable recipientRewriteTable; + private final DomainAliasService domainAliasService; private final JsonTransformer jsonTransformer; private Service service; @Inject - public DomainsRoutes(DomainList domainList, RecipientRewriteTable recipientRewriteTable, JsonTransformer jsonTransformer) { + DomainsRoutes(DomainList domainList, DomainAliasService domainAliasService, JsonTransformer jsonTransformer) { this.domainList = domainList; - this.recipientRewriteTable = recipientRewriteTable; + this.domainAliasService = domainAliasService; this.jsonTransformer = jsonTransformer; } @@ -234,26 +225,18 @@ public class DomainsRoutes implements Routes { Domain domain = checkValidDomain(request.params(DOMAIN_NAME)); domainList.removeDomain(domain); - removeCorrespondingDomainAliases(domain); + domainAliasService.removeCorrespondingDomainAliases(domain); } catch (DomainListException e) { LOGGER.info("{} did not exists", request.params(DOMAIN_NAME)); } - response.status(HttpStatus.NO_CONTENT_204); - return Constants.EMPTY_BODY; - } - - private void removeCorrespondingDomainAliases(Domain domain) throws RecipientRewriteTableException { - MappingSource mappingSource = MappingSource.fromDomain(domain); - recipientRewriteTable.getStoredMappings(mappingSource) - .asStream() - .forEach(Throwing.<Mapping>consumer(mapping -> recipientRewriteTable.removeMapping(mappingSource, mapping)).sneakyThrow()); + return Responses.returnNoContent(response); } private String addDomain(Request request, Response response) { Domain domain = checkValidDomain(request.params(DOMAIN_NAME)); try { addDomain(domain); - response.status(204); + return Responses.returnNoContent(response); } catch (DomainListException e) { LOGGER.info("{} already exists", domain); throw ErrorResponder.builder() @@ -271,7 +254,6 @@ public class DomainsRoutes implements Routes { .cause(e) .haltError(); } - return Constants.EMPTY_BODY; } private Domain checkValidDomain(String domainName) { @@ -292,53 +274,48 @@ public class DomainsRoutes implements Routes { domainList.addDomain(domain); } - private Response exists(Request request, Response response) throws DomainListException { + private String exists(Request request, Response response) throws DomainListException { Domain domain = checkValidDomain(request.params(DOMAIN_NAME)); if (!domainList.containsDomain(domain)) { throw domainNotFound(domain); } else { - response.status(HttpStatus.NO_CONTENT_204); - return response; + return Responses.returnNoContent(response); } } private ImmutableSet<DomainAliasResponse> listDomainAliases(Request request, Response response) throws DomainListException, RecipientRewriteTableException { Domain domain = checkValidDomain(request.params(DOMAIN_NAME)); - if (!hasAliases(domain)) { + if (!domainAliasService.hasAliases(domain)) { throw domainHasNoAliases(domain); } else { - return recipientRewriteTable.listSources(Mapping.domain(domain)) - .map(DomainAliasResponse::new) - .collect(Guavate.toImmutableSet()); + return domainAliasService.listDomainAliases(domain); } } private String addDomainAlias(Request request, Response response) throws DomainListException, RecipientRewriteTableException { - return performOperationOnAlias(request, response, recipientRewriteTable::addMapping); - } + Domain sourceDomain = checkValidDomain(request.params(SOURCE_DOMAIN)); + Domain destinationDomain = checkValidDomain(request.params(DESTINATION_DOMAIN)); - private String removeDomainAlias(Request request, Response response) throws DomainListException, RecipientRewriteTableException { - return performOperationOnAlias(request, response, recipientRewriteTable::removeMapping); + try { + domainAliasService.addDomainAlias(sourceDomain, destinationDomain); + return Responses.returnNoContent(response); + } catch (DomainAliasService.DomainNotFound e) { + throw domainNotFound(e.getDomain()); + } } - private String performOperationOnAlias(Request request, Response response, MappingOperation operation) throws DomainListException, RecipientRewriteTableException { + private String removeDomainAlias(Request request, Response response) throws DomainListException, RecipientRewriteTableException { Domain sourceDomain = checkValidDomain(request.params(SOURCE_DOMAIN)); Domain destinationDomain = checkValidDomain(request.params(DESTINATION_DOMAIN)); - if (!domainList.containsDomain(sourceDomain)) { - throw domainNotFound(sourceDomain); + try { + domainAliasService.removeDomainAlias(sourceDomain, destinationDomain); + return Responses.returnNoContent(response); + } catch (DomainAliasService.DomainNotFound e) { + throw domainNotFound(e.getDomain()); } - - operation.perform(MappingSource.fromDomain(sourceDomain), Mapping.domain(destinationDomain)); - response.status(HttpStatus.NO_CONTENT_204); - return Constants.EMPTY_BODY; - } - - private boolean hasAliases(Domain domain) throws DomainListException, RecipientRewriteTableException { - return domainList.containsDomain(domain) - || recipientRewriteTable.listSources(Mapping.domain(domain)).findFirst().isPresent(); } private HaltException domainNotFound(Domain domain) { diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/service/DomainAliasService.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/service/DomainAliasService.java new file mode 100644 index 0000000..afbc093 --- /dev/null +++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/service/DomainAliasService.java @@ -0,0 +1,98 @@ +/**************************************************************** + * 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.webadmin.service; + +import javax.inject.Inject; + +import org.apache.james.core.Domain; +import org.apache.james.domainlist.api.DomainList; +import org.apache.james.domainlist.api.DomainListException; +import org.apache.james.rrt.api.RecipientRewriteTable; +import org.apache.james.rrt.api.RecipientRewriteTableException; +import org.apache.james.rrt.lib.Mapping; +import org.apache.james.rrt.lib.MappingSource; +import org.apache.james.webadmin.dto.DomainAliasResponse; + +import com.github.fge.lambdas.Throwing; +import com.github.steveash.guavate.Guavate; +import com.google.common.collect.ImmutableSet; + +public class DomainAliasService { + public static class DomainNotFound extends RuntimeException { + private final Domain domain; + + DomainNotFound(Domain domain) { + this.domain = domain; + } + + public Domain getDomain() { + return domain; + } + } + + @FunctionalInterface + interface MappingOperation { + void perform(MappingSource mappingSource, Mapping mapping) throws RecipientRewriteTableException; + } + + private final RecipientRewriteTable recipientRewriteTable; + private final DomainList domainList; + + @Inject + public DomainAliasService(RecipientRewriteTable recipientRewriteTable, DomainList domainList) { + this.recipientRewriteTable = recipientRewriteTable; + this.domainList = domainList; + } + + public void removeCorrespondingDomainAliases(Domain domain) throws RecipientRewriteTableException { + MappingSource mappingSource = MappingSource.fromDomain(domain); + recipientRewriteTable.getStoredMappings(mappingSource) + .asStream() + .forEach(Throwing.<Mapping>consumer(mapping -> recipientRewriteTable.removeMapping(mappingSource, mapping)).sneakyThrow()); + } + + public ImmutableSet<DomainAliasResponse> listDomainAliases(Domain domain) throws RecipientRewriteTableException { + return recipientRewriteTable.listSources(Mapping.domain(domain)) + .map(DomainAliasResponse::new) + .collect(Guavate.toImmutableSet()); + } + + public boolean hasAliases(Domain domain) throws DomainListException, RecipientRewriteTableException { + return domainList.containsDomain(domain) + || recipientRewriteTable.listSources(Mapping.domain(domain)).findFirst().isPresent(); + } + + public void addDomainAlias(Domain sourceDomain, Domain destinationDomain) throws DomainListException, RecipientRewriteTableException { + performOperationOnAlias(recipientRewriteTable::addMapping, sourceDomain, destinationDomain); + } + + public void removeDomainAlias(Domain sourceDomain, Domain destinationDomain) throws DomainListException, RecipientRewriteTableException { + performOperationOnAlias(recipientRewriteTable::removeMapping, sourceDomain, destinationDomain); + } + + private void performOperationOnAlias(MappingOperation operation, Domain sourceDomain, Domain destinationDomain) throws DomainListException, RecipientRewriteTableException { + if (!domainList.containsDomain(sourceDomain)) { + throw new DomainNotFound(sourceDomain); + } + + operation.perform(MappingSource.fromDomain(sourceDomain), Mapping.domain(destinationDomain)); + } + +} diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java index dea3fc9..01dbe7e 100644 --- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java +++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java @@ -46,6 +46,7 @@ import org.apache.james.metrics.logger.DefaultMetricFactory; import org.apache.james.rrt.memory.MemoryRecipientRewriteTable; import org.apache.james.webadmin.WebAdminServer; import org.apache.james.webadmin.WebAdminUtils; +import org.apache.james.webadmin.service.DomainAliasService; import org.apache.james.webadmin.utils.JsonTransformer; import org.eclipse.jetty.http.HttpStatus; import org.junit.jupiter.api.AfterEach; @@ -57,19 +58,19 @@ import io.restassured.RestAssured; import io.restassured.http.ContentType; -public class DomainsRoutesTest { +class DomainsRoutesTest { private static final String DOMAIN = "domain"; private static final String ALIAS_DOMAIN = "alias.domain"; private static final String ALIAS_DOMAIN_2 = "alias.domain.bis"; - public static final String EXTERNAL_DOMAIN = "external.domain.tld"; - public static final String DOMAIN_2 = "domain2"; + private static final String EXTERNAL_DOMAIN = "external.domain.tld"; private WebAdminServer webAdminServer; private void createServer(DomainList domainList) throws Exception { + DomainAliasService domainAliasService = new DomainAliasService(new MemoryRecipientRewriteTable(), domainList); webAdminServer = WebAdminUtils.createWebAdminServer( new DefaultMetricFactory(), - new DomainsRoutes(domainList, new MemoryRecipientRewriteTable(), new JsonTransformer())); + new DomainsRoutes(domainList, domainAliasService, new JsonTransformer())); webAdminServer.configure(NO_CONFIGURATION); webAdminServer.await(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
