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 2019/08/02 09:56:45 UTC

[james-project] 01/05: JAMES-2842 AddressMappingRoutes Implementation

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 79e41774299e88a50e0008e182b04beb48152b85
Author: Khanh Le <bk...@linagora.com>
AuthorDate: Tue Jul 23 17:30:37 2019 +0700

    JAMES-2842 AddressMappingRoutes Implementation
---
 .../webadmin/routes/AddressMappingRoutes.java      | 108 ++++++++++++++++
 .../webadmin/routes/AddressMappingRoutesTest.java  | 143 +++++++++++++++++++++
 2 files changed, 251 insertions(+)

diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
new file mode 100644
index 0000000..faeb2cc
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/AddressMappingRoutes.java
@@ -0,0 +1,108 @@
+/****************************************************************
+ * 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.routes;
+
+import static spark.Spark.halt;
+
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import org.apache.james.core.MailAddress;
+import org.apache.james.rrt.api.MappingAlreadyExistsException;
+import org.apache.james.rrt.api.RecipientRewriteTable;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
+import org.apache.james.rrt.api.SameSourceAndDestinationException;
+import org.apache.james.rrt.api.SourceDomainIsNotInDomainListException;
+import org.apache.james.rrt.lib.MappingSource;
+import org.apache.james.webadmin.Constants;
+import org.apache.james.webadmin.Routes;
+import org.apache.james.webadmin.utils.ErrorResponder;
+import org.eclipse.jetty.http.HttpStatus;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+import spark.HaltException;
+import spark.Request;
+import spark.Response;
+import spark.Service;
+
+
+@Api(tags = "AddressMappings")
+@Path(MappingRoutes.BASE_PATH)
+@Produces(Constants.JSON_CONTENT_TYPE)
+public class AddressMappingRoutes implements Routes {
+
+    static final String BASE_PATH = "/mappings/address/";
+    static final String ADD_ADDRESS_MAPPING_PATH = "/mappings/address/:mappingSource/targets/:destinationAddress";
+
+    private final RecipientRewriteTable recipientRewriteTable;
+
+    @Inject
+    AddressMappingRoutes(RecipientRewriteTable recipientRewriteTable) {
+        this.recipientRewriteTable = recipientRewriteTable;
+    }
+
+    @Override
+    public String getBasePath() {
+        return BASE_PATH;
+    }
+
+    @Override
+    public void define(Service service) {
+        service.post(ADD_ADDRESS_MAPPING_PATH, this::addAddressMapping);
+    }
+
+    @POST
+    @Path(ADD_ADDRESS_MAPPING_PATH)
+    @ApiOperation(value = "Getting all user mappings in RecipientRewriteTable")
+    @ApiResponses(value = {
+        @ApiResponse(code = HttpStatus.NO_CONTENT_204, message = "No body on created", response = List.class),
+        @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid parameter values.")
+    })
+    public HaltException addAddressMapping(Request request, Response response) throws RecipientRewriteTableException {
+        MailAddress source = MailAddressParser.parseMailAddress(
+            request.params("mappingSource"),"address");
+        MailAddress destinationAddress = MailAddressParser.parseMailAddress(
+            request.params("destinationAddress"), "address");
+        addAddressMapping(MappingSource.fromMailAddress(source), destinationAddress);
+        return halt(HttpStatus.NO_CONTENT_204);
+    }
+
+    private void addAddressMapping(MappingSource source, MailAddress destination) throws RecipientRewriteTableException {
+        try {
+            recipientRewriteTable.addAddressMapping(source, destination.asString());
+        } catch (MappingAlreadyExistsException e) {
+            // ignore
+        } catch (SameSourceAndDestinationException | SourceDomainIsNotInDomainListException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorResponder.ErrorType.INVALID_ARGUMENT)
+                .message(e.getMessage())
+                .haltError();
+        }
+    }
+
+}
diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
new file mode 100644
index 0000000..048f17e
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/AddressMappingRoutesTest.java
@@ -0,0 +1,143 @@
+/****************************************************************
+ * 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.routes;
+
+import static io.restassured.RestAssured.when;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+import org.apache.james.core.Domain;
+import org.apache.james.core.MailAddress;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.domainlist.memory.MemoryDomainList;
+import org.apache.james.rrt.lib.Mapping;
+import org.apache.james.rrt.lib.MappingSource;
+import org.apache.james.rrt.memory.MemoryRecipientRewriteTable;
+import org.apache.james.webadmin.WebAdminServer;
+import org.apache.james.webadmin.WebAdminUtils;
+import org.eclipse.jetty.http.HttpStatus;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import io.restassured.RestAssured;
+import io.restassured.filter.log.LogDetail;
+
+class AddressMappingRoutesTest {
+
+    private WebAdminServer webAdminServer;
+    private MemoryRecipientRewriteTable recipientRewriteTable;
+
+    @BeforeEach
+    void setUp() throws DomainListException {
+        recipientRewriteTable = new MemoryRecipientRewriteTable();
+        DNSService dnsService = mock(DNSService.class);
+        DomainList domainList = new MemoryDomainList(dnsService);
+        domainList.addDomain(Domain.of("domain.tld"));
+
+        recipientRewriteTable.setDomainList(domainList);
+
+        webAdminServer = WebAdminUtils.createWebAdminServer(
+            new AddressMappingRoutes(recipientRewriteTable))
+            .start();
+
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminServer)
+            .setBasePath(AddressMappingRoutes.BASE_PATH)
+            .log(LogDetail.METHOD)
+            .build();
+    }
+
+    @AfterEach
+    void stop() {
+        webAdminServer.destroy();
+    }
+
+    @Test
+    void addAddressMappingShouldAddMappingOnRecipientRewriteTable() {
+        when()
+            .post("source@domain.tld/targets/user@domain.tld");
+
+        assertThat(recipientRewriteTable.getStoredMappings(MappingSource.parse("source@domain.tld")))
+            .containsAnyOf(Mapping.of("user@domain.tld"));
+    }
+
+    @Test
+    void addAddressMappingShouldReturnNotFoundWhenOneParameterIsEmpty() {
+        when()
+            .post("source@domain.tld/targets/" )
+        .then()
+            .statusCode(HttpStatus.NOT_FOUND_404);
+    }
+
+    @Test
+    void addAddressMappingShouldReturnNoContentWhenValidParameter() {
+        when()
+            .post("source@domain.tld/targets/alice@domain.tld")
+        .then()
+            .statusCode(HttpStatus.NO_CONTENT_204);
+    }
+
+    @Test
+    void addAddressMappingShouldReturnBadRequestWhenInvalidMappingSource() {
+        when()
+            .post("source/targets/alice@domain.tld")
+        .then()
+            .statusCode(HttpStatus.BAD_REQUEST_400);
+    }
+
+    @Test
+    void addAddressMappingShouldReturnBadRequestWhenInvalidDestinationAddress() {
+        when()
+            .post("source@domain.tld/targets/alice")
+        .then()
+            .statusCode(HttpStatus.BAD_REQUEST_400);
+    }
+
+    @Test
+    void addAddressMappingShouldReturnNoContentWithDuplicatedAddress() throws Exception {
+        MappingSource mappingSource =  MappingSource.fromMailAddress(new MailAddress("source@domain.tld"));
+
+        recipientRewriteTable.addAddressMapping(mappingSource, "alice@domain.tld");
+        recipientRewriteTable.addAddressMapping(mappingSource, "bob@domain.tld");
+
+        when()
+            .post("source@domain.tld/targets/alice@domain.tld")
+        .then()
+            .statusCode(HttpStatus.NO_CONTENT_204);
+    }
+
+    @Test
+    void addAddressMappingShouldReturnBadRequestWhenSourceAndDestinationIsTheSame() {
+        when()
+            .post("source@domain.tld/targets/source@domain.tld")
+        .then()
+            .statusCode(HttpStatus.BAD_REQUEST_400);
+    }
+
+    @Test
+    void addressMappingSHouldReturnBadRequestWhenSourceDomainNotInDomainList() {
+        when()
+            .post("source@dexample/targets/alice@domain.tld")
+        .then()
+            .statusCode(HttpStatus.BAD_REQUEST_400);
+    }
+}
\ No newline at end of file


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