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/12/17 01:55:31 UTC

[james-project] 10/24: JAMES-3006 Use Task factory in dead-letter routes

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 dc69a933f2a335d045edf57c4c7084300910e85f
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Thu Dec 12 08:15:14 2019 +0100

    JAMES-3006 Use Task factory in dead-letter routes
---
 .../apache/james/webadmin/dto/ActionEvents.java    | 37 --------------
 .../webadmin/routes/EventDeadLettersRoutes.java    | 59 ++++++++--------------
 .../james/webadmin/dto/ActionEventsTest.java       | 50 ------------------
 .../routes/EventDeadLettersRoutesTest.java         | 12 ++---
 4 files changed, 27 insertions(+), 131 deletions(-)

diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/ActionEvents.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/ActionEvents.java
deleted file mode 100644
index 6861f51..0000000
--- a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/dto/ActionEvents.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/****************************************************************
- * 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.dto;
-
-import java.util.Optional;
-
-import com.google.common.base.Preconditions;
-
-public enum ActionEvents {
-    REDELIVER;
-
-    public static Optional<ActionEvents> find(String action) {
-        Preconditions.checkArgument(action != null, "'action' url parameter is mandatory");
-
-        if (action.equalsIgnoreCase(ActionEvents.REDELIVER.toString())) {
-            return Optional.of(ActionEvents.REDELIVER);
-        }
-        return Optional.empty();
-    }
-}
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/EventDeadLettersRoutes.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/EventDeadLettersRoutes.java
index 6f21a88..c877a50 100644
--- a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/EventDeadLettersRoutes.java
+++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/EventDeadLettersRoutes.java
@@ -20,7 +20,6 @@
 package org.apache.james.webadmin.routes;
 
 import java.util.List;
-import java.util.Optional;
 
 import javax.inject.Inject;
 import javax.ws.rs.DELETE;
@@ -33,13 +32,12 @@ import org.apache.james.event.json.EventSerializer;
 import org.apache.james.mailbox.events.Event;
 import org.apache.james.mailbox.events.EventDeadLetters;
 import org.apache.james.mailbox.events.Group;
-import org.apache.james.task.Task;
-import org.apache.james.task.TaskId;
 import org.apache.james.task.TaskManager;
 import org.apache.james.webadmin.Routes;
-import org.apache.james.webadmin.dto.ActionEvents;
-import org.apache.james.webadmin.dto.TaskIdDto;
 import org.apache.james.webadmin.service.EventDeadLettersService;
+import org.apache.james.webadmin.tasks.TaskFactory;
+import org.apache.james.webadmin.tasks.TaskIdDto;
+import org.apache.james.webadmin.tasks.TaskRegistrationKey;
 import org.apache.james.webadmin.utils.ErrorResponder;
 import org.apache.james.webadmin.utils.JsonTransformer;
 import org.apache.james.webadmin.utils.Responses;
@@ -54,6 +52,7 @@ import io.swagger.annotations.ApiResponses;
 import io.swagger.annotations.ResponseHeader;
 import spark.Request;
 import spark.Response;
+import spark.Route;
 import spark.Service;
 
 @Api(tags = "EventDeadLetter")
@@ -65,6 +64,7 @@ public class EventDeadLettersRoutes implements Routes {
     private static final String INSERTION_ID_PARAMETER = ":insertionId";
 
     private static final String INTERNAL_SERVER_ERROR = "Internal server error - Something went bad on the server side.";
+    private static final TaskRegistrationKey RE_DELIVER = TaskRegistrationKey.of("reDeliver");
 
     private final EventDeadLettersService eventDeadLettersService;
     private final EventSerializer eventSerializer;
@@ -87,13 +87,13 @@ public class EventDeadLettersRoutes implements Routes {
 
     @Override
     public void define(Service service) {
-        service.post(BASE_PATH, this::performActionOnAllEvents, jsonTransformer);
+        service.post(BASE_PATH, performActionOnAllEvents(), jsonTransformer);
         service.get(BASE_PATH + "/groups", this::listGroups, jsonTransformer);
         service.get(BASE_PATH + "/groups/" + GROUP_PARAM, this::listFailedEvents, jsonTransformer);
-        service.post(BASE_PATH + "/groups/" + GROUP_PARAM, this::performActionOnGroupEvents, jsonTransformer);
+        service.post(BASE_PATH + "/groups/" + GROUP_PARAM, performActionOnGroupEvents(), jsonTransformer);
         service.get(BASE_PATH + "/groups/" + GROUP_PARAM + "/" + INSERTION_ID_PARAMETER, this::getEventDetails);
         service.delete(BASE_PATH + "/groups/" + GROUP_PARAM + "/" + INSERTION_ID_PARAMETER, this::deleteEvent);
-        service.post(BASE_PATH + "/groups/" + GROUP_PARAM + "/" + INSERTION_ID_PARAMETER, this::performActionOnSingleEvent, jsonTransformer);
+        service.post(BASE_PATH + "/groups/" + GROUP_PARAM + "/" + INSERTION_ID_PARAMETER, performActionOnSingleEvent(), jsonTransformer);
     }
 
     @POST
@@ -117,12 +117,10 @@ public class EventDeadLettersRoutes implements Routes {
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid action argument"),
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = INTERNAL_SERVER_ERROR)
     })
-    public TaskIdDto performActionOnAllEvents(Request request, Response response) {
-        assertValidActionParameter(request);
-
-        Task task = eventDeadLettersService.redeliverAllEvents();
-        TaskId taskId = taskManager.submit(task);
-        return TaskIdDto.respond(response, taskId);
+    public Route performActionOnAllEvents() {
+        return TaskFactory.builder()
+            .register(RE_DELIVER, request -> eventDeadLettersService.redeliverAllEvents())
+            .buildAsRoute(taskManager);
     }
 
     @GET
@@ -186,13 +184,10 @@ public class EventDeadLettersRoutes implements Routes {
         @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid group name or action argument"),
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = INTERNAL_SERVER_ERROR)
     })
-    public TaskIdDto performActionOnGroupEvents(Request request, Response response) {
-        Group group = parseGroup(request);
-        assertValidActionParameter(request);
-
-        Task task = eventDeadLettersService.redeliverGroupEvents(group);
-        TaskId taskId = taskManager.submit(task);
-        return TaskIdDto.respond(response, taskId);
+    public Route performActionOnGroupEvents() {
+        return TaskFactory.builder()
+            .register(RE_DELIVER, request -> eventDeadLettersService.redeliverGroupEvents(parseGroup(request)))
+            .buildAsRoute(taskManager);
     }
 
     @GET
@@ -297,23 +292,11 @@ public class EventDeadLettersRoutes implements Routes {
         @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "No event with this insertionId"),
         @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = INTERNAL_SERVER_ERROR)
     })
-    public TaskIdDto performActionOnSingleEvent(Request request, Response response) {
-        Group group = parseGroup(request);
-        EventDeadLetters.InsertionId insertionId = parseInsertionId(request);
-        assertValidActionParameter(request);
-
-        Task task = eventDeadLettersService.redeliverSingleEvent(group, insertionId);
-        TaskId taskId = taskManager.submit(task);
-        return TaskIdDto.respond(response, taskId);
-    }
-
-    private void assertValidActionParameter(Request request) {
-        String action = request.queryParams("action");
-        Optional<ActionEvents> actionEvent = ActionEvents.find(action);
-
-        if (!actionEvent.isPresent()) {
-            throw new IllegalArgumentException(action + " is not a supported action");
-        }
+    public Route performActionOnSingleEvent() {
+        return TaskFactory.builder()
+            .register(RE_DELIVER,
+                request -> eventDeadLettersService.redeliverSingleEvent(parseGroup(request), parseInsertionId(request)))
+            .buildAsRoute(taskManager);
     }
 
     private Group parseGroup(Request request) {
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/dto/ActionEventsTest.java b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/dto/ActionEventsTest.java
deleted file mode 100644
index f4a572a..0000000
--- a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/dto/ActionEventsTest.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/****************************************************************
- * 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.dto;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import java.util.Optional;
-
-import org.junit.jupiter.api.Test;
-
-class ActionEventsTest {
-    private static final String ACTION = "redeliver";
-
-    @Test
-    void parseShouldSucceedWithCorrectActionEventsArgument() {
-        assertThat(ActionEvents.find(ACTION))
-            .isEqualTo(Optional.of(ActionEvents.REDELIVER));
-    }
-
-    @Test
-    void parseShouldFailWithIncorrectActionEventsArgument() {
-        assertThat(ActionEvents.find("incorrect-action"))
-            .isEqualTo(Optional.empty());
-    }
-
-    @Test
-    void parseShouldFailWithMissingActionEventsArgument() {
-        assertThatThrownBy(() -> ActionEvents.find(null))
-            .isInstanceOf(IllegalArgumentException.class)
-            .hasMessage("'action' url parameter is mandatory");
-    }
-}
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/EventDeadLettersRoutesTest.java b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/EventDeadLettersRoutesTest.java
index 2b4d51a..9c56056 100644
--- a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/EventDeadLettersRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/EventDeadLettersRoutesTest.java
@@ -533,7 +533,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("invalid-action is not a supported action"));
+                .body("details", is("Invalid value supplied for 'action': invalid-action. Supported values are [reDeliver]"));
         }
 
         @Test
@@ -548,7 +548,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("'action' url parameter is mandatory"));
+                .body("details", is("'action' query parameter is compulsory. Supported values are [reDeliver]"));
         }
     }
 
@@ -721,7 +721,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("invalid-action is not a supported action"));
+                .body("details", is("Invalid value supplied for 'action': invalid-action. Supported values are [reDeliver]"));
         }
 
         @Test
@@ -736,7 +736,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("'action' url parameter is mandatory"));
+                .body("details", is("'action' query parameter is compulsory. Supported values are [reDeliver]"));
         }
 
         @Test
@@ -900,7 +900,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("invalid-action is not a supported action"));
+                .body("details", is("Invalid value supplied for 'action': invalid-action. Supported values are [reDeliver]"));
         }
 
         @Test
@@ -915,7 +915,7 @@ class EventDeadLettersRoutesTest {
                 .body("statusCode", is(400))
                 .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType()))
                 .body("message", is("Invalid arguments supplied in the user request"))
-                .body("details", is("'action' url parameter is mandatory"));
+                .body("details", is("'action' query parameter is compulsory. Supported values are [reDeliver]"));
         }
 
         @Test


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