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 2018/01/05 09:17:31 UTC

[05/15] james-project git commit: JAMES-2266 Webadmin frontend for merging mailboxes

JAMES-2266 Webadmin frontend for merging mailboxes


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fde33365
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fde33365
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fde33365

Branch: refs/heads/master
Commit: fde3336569b6ffcd494f42325469826e016a7657
Parents: c6ec283
Author: benwa <bt...@linagora.com>
Authored: Wed Dec 27 16:42:35 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Fri Jan 5 16:06:36 2018 +0700

----------------------------------------------------------------------
 .../modules/server/CassandraRoutesModule.java   |  3 +
 .../webadmin/dto/MailboxMergingRequest.java     | 65 +++++++++++++
 .../routes/CassandraMailboxMergingRoutes.java   | 97 ++++++++++++++++++++
 3 files changed, 165 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/fde33365/server/container/guice/protocols/webadmin-cassandra/src/main/java/org/apache/james/modules/server/CassandraRoutesModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/protocols/webadmin-cassandra/src/main/java/org/apache/james/modules/server/CassandraRoutesModule.java b/server/container/guice/protocols/webadmin-cassandra/src/main/java/org/apache/james/modules/server/CassandraRoutesModule.java
index 1cf1f93..040ca47 100644
--- a/server/container/guice/protocols/webadmin-cassandra/src/main/java/org/apache/james/modules/server/CassandraRoutesModule.java
+++ b/server/container/guice/protocols/webadmin-cassandra/src/main/java/org/apache/james/modules/server/CassandraRoutesModule.java
@@ -26,6 +26,7 @@ import org.apache.james.backends.cassandra.versions.SchemaVersion;
 import org.apache.james.mailbox.cassandra.mail.migration.AttachmentMessageIdCreation;
 import org.apache.james.mailbox.cassandra.mail.migration.AttachmentV2Migration;
 import org.apache.james.webadmin.Routes;
+import org.apache.james.webadmin.routes.CassandraMailboxMergingRoutes;
 import org.apache.james.webadmin.routes.CassandraMigrationRoutes;
 
 import com.google.inject.AbstractModule;
@@ -42,10 +43,12 @@ public class CassandraRoutesModule extends AbstractModule {
     @Override
     protected void configure() {
         bind(CassandraRoutesModule.class).in(Scopes.SINGLETON);
+        bind(CassandraMailboxMergingRoutes.class).in(Scopes.SINGLETON);
         bind(CassandraMigrationService.class).in(Scopes.SINGLETON);
 
         Multibinder<Routes> routesMultibinder = Multibinder.newSetBinder(binder(), Routes.class);
         routesMultibinder.addBinding().to(CassandraMigrationRoutes.class);
+        routesMultibinder.addBinding().to(CassandraMailboxMergingRoutes.class);
 
         MapBinder<SchemaVersion, Migration> allMigrationClazzBinder = MapBinder.newMapBinder(binder(), SchemaVersion.class, Migration.class);
         allMigrationClazzBinder.addBinding(FROM_V2_TO_V3).toInstance(() -> Migration.Result.COMPLETED);

http://git-wip-us.apache.org/repos/asf/james-project/blob/fde33365/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/dto/MailboxMergingRequest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/dto/MailboxMergingRequest.java b/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/dto/MailboxMergingRequest.java
new file mode 100644
index 0000000..f48ae0f
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/dto/MailboxMergingRequest.java
@@ -0,0 +1,65 @@
+/****************************************************************
+ * 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.Objects;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+
+public class MailboxMergingRequest {
+    private final String mergeOrigin;
+    private final String mergeDestination;
+
+    @JsonCreator
+    public MailboxMergingRequest(@JsonProperty("mergeOrigin") String mergeOrigin,
+                                 @JsonProperty("mergeDestination") String mergeDestination) {
+        Preconditions.checkArgument(!Strings.isNullOrEmpty(mergeOrigin));
+        Preconditions.checkArgument(!Strings.isNullOrEmpty(mergeDestination));
+        this.mergeOrigin = mergeOrigin;
+        this.mergeDestination = mergeDestination;
+    }
+
+    public String getMergeOrigin() {
+        return mergeOrigin;
+    }
+
+    public String getMergeDestination() {
+        return mergeDestination;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof MailboxMergingRequest) {
+            MailboxMergingRequest that = (MailboxMergingRequest) o;
+
+            return Objects.equals(this.mergeOrigin, that.mergeOrigin)
+                && Objects.equals(this.mergeDestination, that.mergeDestination);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(mergeOrigin, mergeDestination);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/fde33365/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/routes/CassandraMailboxMergingRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/routes/CassandraMailboxMergingRoutes.java b/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/routes/CassandraMailboxMergingRoutes.java
new file mode 100644
index 0000000..b778ef6
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-cassandra/src/main/java/org/apache/james/webadmin/routes/CassandraMailboxMergingRoutes.java
@@ -0,0 +1,97 @@
+/****************************************************************
+ * 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 javax.inject.Inject;
+
+import org.apache.james.mailbox.cassandra.ids.CassandraId;
+import org.apache.james.mailbox.cassandra.mail.task.MailboxMergingTask;
+import org.apache.james.mailbox.cassandra.mail.task.MailboxMergingTaskRunner;
+import org.apache.james.task.TaskId;
+import org.apache.james.task.TaskManager;
+import org.apache.james.webadmin.Routes;
+import org.apache.james.webadmin.dto.MailboxMergingRequest;
+import org.apache.james.webadmin.dto.TaskIdDto;
+import org.apache.james.webadmin.utils.ErrorResponder;
+import org.apache.james.webadmin.utils.ErrorResponder.ErrorType;
+import org.apache.james.webadmin.utils.JsonExtractException;
+import org.apache.james.webadmin.utils.JsonExtractor;
+import org.apache.james.webadmin.utils.JsonTransformer;
+import org.eclipse.jetty.http.HttpStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import spark.Request;
+import spark.Response;
+import spark.Service;
+
+public class CassandraMailboxMergingRoutes implements Routes {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CassandraMailboxMergingRoutes.class);
+
+    public static final String BASE = "/cassandra/mailbox/merging";
+
+    private final MailboxMergingTaskRunner mailboxMergingTaskRunner;
+    private final CassandraId.Factory mailboxIdFactory;
+    private final JsonExtractor<MailboxMergingRequest> jsonExtractor;
+    private final TaskManager taskManager;
+    private final JsonTransformer jsonTransformer;
+
+    @Inject
+    public CassandraMailboxMergingRoutes(MailboxMergingTaskRunner mailboxMergingTaskRunner, CassandraId.Factory mailboxIdFactory, TaskManager taskManager, JsonTransformer jsonTransformer) {
+        this.mailboxMergingTaskRunner = mailboxMergingTaskRunner;
+        this.mailboxIdFactory = mailboxIdFactory;
+        this.taskManager = taskManager;
+        this.jsonTransformer = jsonTransformer;
+        this.jsonExtractor = new JsonExtractor<>(MailboxMergingRequest.class);
+    }
+
+    @Override
+    public void define(Service service) {
+        service.post(BASE, this::mergeMailboxes, jsonTransformer);
+    }
+
+    private Object mergeMailboxes(Request request, Response response) {
+        try {
+            LOGGER.debug("Cassandra upgrade launched");
+            MailboxMergingRequest mailboxMergingRequest = jsonExtractor.parse(request.body());
+            CassandraId originId = mailboxIdFactory.fromString(mailboxMergingRequest.getMergeOrigin());
+            CassandraId destinationId = mailboxIdFactory.fromString(mailboxMergingRequest.getMergeDestination());
+
+            MailboxMergingTask task = new MailboxMergingTask(mailboxMergingTaskRunner, originId, destinationId);
+            TaskId taskId = taskManager.submit(task);
+            return TaskIdDto.respond(response, taskId);
+        } catch (JsonExtractException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorType.INVALID_ARGUMENT)
+                .cause(e)
+                .message("Failed to parse JSON request")
+                .haltError();
+        } catch (IllegalArgumentException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorType.INVALID_ARGUMENT)
+                .cause(e)
+                .message("Invalid mailbox id")
+                .haltError();
+        }
+    }
+}


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