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 ro...@apache.org on 2018/02/27 13:27:42 UTC

[2/5] james-project git commit: JAMES-2344 extract GlobalQuota logic into a service class

JAMES-2344 extract GlobalQuota logic into a service class


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

Branch: refs/heads/master
Commit: 5ebcf3ab8486900ec6c92b6b6625ff044b731f6b
Parents: e87c055
Author: Matthieu Baechler <ma...@apache.org>
Authored: Wed Feb 21 15:34:19 2018 +0100
Committer: Matthieu Baechler <ma...@apache.org>
Committed: Tue Feb 27 09:59:23 2018 +0100

----------------------------------------------------------------------
 .../webadmin/routes/GlobalQuotaRoutes.java      | 99 +++++++-------------
 .../webadmin/service/GlobalQuotaService.java    | 74 +++++++++++++++
 .../webadmin/routes/GlobalQuotaRoutesTest.java  |  3 +-
 3 files changed, 112 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/5ebcf3ab/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java
index 397381a..0121d60 100644
--- a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java
+++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/routes/GlobalQuotaRoutes.java
@@ -26,20 +26,16 @@ import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 
-import org.apache.james.mailbox.model.Quota;
-import org.apache.james.mailbox.quota.MaxQuotaManager;
-import org.apache.james.webadmin.Constants;
 import org.apache.james.webadmin.Routes;
 import org.apache.james.webadmin.dto.QuotaDTO;
 import org.apache.james.webadmin.dto.QuotaRequest;
+import org.apache.james.webadmin.service.GlobalQuotaService;
 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 io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -47,6 +43,7 @@ import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
+import spark.Request;
 import spark.Service;
 
 @Api(tags = "GlobalQuota")
@@ -57,16 +54,15 @@ public class GlobalQuotaRoutes implements Routes {
     public static final String QUOTA_ENDPOINT = "/quota";
     public static final String COUNT_ENDPOINT = QUOTA_ENDPOINT + "/count";
     public static final String SIZE_ENDPOINT = QUOTA_ENDPOINT + "/size";
-    private static final Logger LOGGER = LoggerFactory.getLogger(Routes.class);
 
-    private final MaxQuotaManager maxQuotaManager;
     private final JsonTransformer jsonTransformer;
     private final JsonExtractor<QuotaDTO> jsonExtractor;
+    private final GlobalQuotaService globalQuotaService;
     private Service service;
 
     @Inject
-    public GlobalQuotaRoutes(MaxQuotaManager maxQuotaManager, JsonTransformer jsonTransformer) {
-        this.maxQuotaManager = maxQuotaManager;
+    public GlobalQuotaRoutes(GlobalQuotaService globalQuotaService, JsonTransformer jsonTransformer) {
+        this.globalQuotaService = globalQuotaService;
         this.jsonTransformer = jsonTransformer;
         this.jsonExtractor = new JsonExtractor<>(QuotaDTO.class);
     }
@@ -106,11 +102,10 @@ public class GlobalQuotaRoutes implements Routes {
         service.put(QUOTA_ENDPOINT, ((request, response) -> {
             try {
                 QuotaDTO quotaDTO = jsonExtractor.parse(request.body());
-                maxQuotaManager.setDefaultMaxMessage(quotaDTO.getCount());
-                maxQuotaManager.setDefaultMaxStorage(quotaDTO.getSize());
+                globalQuotaService.defineQuota(quotaDTO);
                 response.status(HttpStatus.NO_CONTENT_204);
+                return response;
             } catch (JsonExtractException e) {
-                LOGGER.info("Malformed JSON", e);
                 throw ErrorResponder.builder()
                     .statusCode(HttpStatus.BAD_REQUEST_400)
                     .type(ErrorType.INVALID_ARGUMENT)
@@ -118,7 +113,6 @@ public class GlobalQuotaRoutes implements Routes {
                     .cause(e)
                     .haltError();
             } catch (IllegalArgumentException e) {
-                LOGGER.info("Quota should be positive or unlimited (-1)", e);
                 throw ErrorResponder.builder()
                     .statusCode(HttpStatus.BAD_REQUEST_400)
                     .type(ErrorType.INVALID_ARGUMENT)
@@ -126,7 +120,6 @@ public class GlobalQuotaRoutes implements Routes {
                     .cause(e)
                     .haltError();
             }
-            return Constants.EMPTY_BODY;
         }));
     }
 
@@ -140,13 +133,7 @@ public class GlobalQuotaRoutes implements Routes {
             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.")
     })
     public void defineGetQuota() {
-        service.get(QUOTA_ENDPOINT, (request, response) -> {
-            QuotaDTO quotaDTO = QuotaDTO.builder()
-                .count(maxQuotaManager.getDefaultMaxMessage())
-                .size(maxQuotaManager.getDefaultMaxStorage()).build();
-            response.status(HttpStatus.OK_200);
-            return quotaDTO;
-        }, jsonTransformer);
+        service.get(QUOTA_ENDPOINT, (request, response) -> globalQuotaService.getQuota(), jsonTransformer);
     }
 
     @DELETE
@@ -158,9 +145,9 @@ public class GlobalQuotaRoutes implements Routes {
     })
     public void defineDeleteQuotaSize() {
         service.delete(SIZE_ENDPOINT, (request, response) -> {
-            maxQuotaManager.setDefaultMaxStorage(Quota.UNLIMITED);
+            globalQuotaService.deleteMaxSizeQuota();
             response.status(HttpStatus.NO_CONTENT_204);
-            return Constants.EMPTY_BODY;
+            return response;
         });
     }
 
@@ -177,20 +164,10 @@ public class GlobalQuotaRoutes implements Routes {
     })
     public void defineUpdateQuotaSize() {
         service.put(SIZE_ENDPOINT, (request, response) -> {
-            try {
-                QuotaRequest quotaRequest = QuotaRequest.parse(request.body());
-                maxQuotaManager.setDefaultMaxStorage(quotaRequest.getValue());
-                response.status(HttpStatus.NO_CONTENT_204);
-            } catch (IllegalArgumentException e) {
-                LOGGER.info("Invalid quota. Need to be an integer value greater than 0");
-                throw ErrorResponder.builder()
-                    .statusCode(HttpStatus.BAD_REQUEST_400)
-                    .type(ErrorType.INVALID_ARGUMENT)
-                    .message("Invalid quota. Need to be an integer value greater than 0")
-                    .cause(e)
-                    .haltError();
-            }
-            return Constants.EMPTY_BODY;
+            QuotaRequest quotaRequest = parseQuotaRequest(request);
+            globalQuotaService.defineMaxSizeQuota(quotaRequest);
+            response.status(HttpStatus.NO_CONTENT_204);
+            return response;
         });
     }
 
@@ -202,11 +179,7 @@ public class GlobalQuotaRoutes implements Routes {
             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.")
     })
     public void defineGetQuotaSize() {
-        service.get(SIZE_ENDPOINT, (request, response) -> {
-            long value = maxQuotaManager.getDefaultMaxStorage();
-            response.status(HttpStatus.OK_200);
-            return value;
-        }, jsonTransformer);
+        service.get(SIZE_ENDPOINT, (request, response) -> globalQuotaService.getMaxSizeQuota(), jsonTransformer);
     }
 
     @DELETE
@@ -218,9 +191,9 @@ public class GlobalQuotaRoutes implements Routes {
     })
     public void defineDeleteQuotaCount() {
         service.delete(COUNT_ENDPOINT, (request, response) -> {
-            maxQuotaManager.setDefaultMaxMessage(Quota.UNLIMITED);
+            globalQuotaService.deleteMaxCountQuota();
             response.status(HttpStatus.NO_CONTENT_204);
-            return Constants.EMPTY_BODY;
+            return response;
         });
     }
 
@@ -237,20 +210,10 @@ public class GlobalQuotaRoutes implements Routes {
     })
     public void defineUpdateQuotaCount() {
         service.put(COUNT_ENDPOINT, (request, response) -> {
-            try {
-                QuotaRequest quotaRequest = QuotaRequest.parse(request.body());
-                maxQuotaManager.setDefaultMaxMessage(quotaRequest.getValue());
-                response.status(HttpStatus.NO_CONTENT_204);
-            } catch (IllegalArgumentException e) {
-                LOGGER.info("Invalid quota. Need to be an integer value greater than 0");
-                throw ErrorResponder.builder()
-                    .statusCode(HttpStatus.BAD_REQUEST_400)
-                    .type(ErrorType.INVALID_ARGUMENT)
-                    .message("Invalid quota. Need to be an integer value greater than 0")
-                    .cause(e)
-                    .haltError();
-            }
-            return Constants.EMPTY_BODY;
+            QuotaRequest quotaRequest = parseQuotaRequest(request);
+            globalQuotaService.defineMaxCountQuota(quotaRequest);
+            response.status(HttpStatus.NO_CONTENT_204);
+            return response;
         });
     }
 
@@ -262,10 +225,20 @@ public class GlobalQuotaRoutes implements Routes {
             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Internal server error - Something went bad on the server side.")
     })
     public void defineGetQuotaCount() {
-        service.get(COUNT_ENDPOINT, (request, response) -> {
-            long value = maxQuotaManager.getDefaultMaxMessage();
-            response.status(HttpStatus.OK_200);
-            return value;
-        }, jsonTransformer);
+        service.get(COUNT_ENDPOINT, (request, response) -> globalQuotaService.getMaxCountQuota(), jsonTransformer);
     }
+
+    private QuotaRequest parseQuotaRequest(Request request) {
+        try {
+            return QuotaRequest.parse(request.body());
+        } catch (IllegalArgumentException e) {
+            throw ErrorResponder.builder()
+                .statusCode(HttpStatus.BAD_REQUEST_400)
+                .type(ErrorType.INVALID_ARGUMENT)
+                .message("Invalid quota. Need to be an integer value greater than 0")
+                .cause(e)
+                .haltError();
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/5ebcf3ab/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/GlobalQuotaService.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/GlobalQuotaService.java b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/GlobalQuotaService.java
new file mode 100644
index 0000000..33d76de
--- /dev/null
+++ b/server/protocols/webadmin/webadmin-mailbox/src/main/java/org/apache/james/webadmin/service/GlobalQuotaService.java
@@ -0,0 +1,74 @@
+/****************************************************************
+ * 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.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.Quota;
+import org.apache.james.mailbox.quota.MaxQuotaManager;
+import org.apache.james.webadmin.dto.QuotaDTO;
+import org.apache.james.webadmin.dto.QuotaRequest;
+
+public class GlobalQuotaService {
+
+    private final MaxQuotaManager maxQuotaManager;
+
+    @Inject
+    public GlobalQuotaService(MaxQuotaManager maxQuotaManager) {
+        this.maxQuotaManager = maxQuotaManager;
+    }
+
+    public void defineQuota(QuotaDTO quota) throws MailboxException {
+        maxQuotaManager.setDefaultMaxMessage(quota.getCount());
+        maxQuotaManager.setDefaultMaxStorage(quota.getSize());
+    }
+
+    public QuotaDTO getQuota() throws MailboxException {
+        return QuotaDTO
+            .builder()
+            .count(maxQuotaManager.getDefaultMaxMessage())
+            .size(maxQuotaManager.getDefaultMaxStorage())
+            .build();
+    }
+
+    public Long getMaxSizeQuota() throws MailboxException {
+        return maxQuotaManager.getDefaultMaxStorage();
+    }
+
+    public void defineMaxSizeQuota(QuotaRequest quotaRequest) throws MailboxException {
+        maxQuotaManager.setDefaultMaxStorage(quotaRequest.getValue());
+    }
+
+    public void deleteMaxSizeQuota() throws MailboxException {
+        maxQuotaManager.setDefaultMaxStorage(Quota.UNLIMITED);
+    }
+
+    public Long getMaxCountQuota() throws MailboxException {
+        return maxQuotaManager.getDefaultMaxMessage();
+    }
+
+    public void defineMaxCountQuota(QuotaRequest quotaRequest) throws MailboxException {
+        maxQuotaManager.setDefaultMaxMessage(quotaRequest.getValue());
+    }
+
+    public void deleteMaxCountQuota() throws MailboxException {
+        maxQuotaManager.setDefaultMaxMessage(Quota.UNLIMITED);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/5ebcf3ab/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
index 60029c1..e626fd6 100644
--- a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
@@ -30,6 +30,7 @@ import org.apache.james.mailbox.model.Quota;
 import org.apache.james.metrics.logger.DefaultMetricFactory;
 import org.apache.james.webadmin.WebAdminServer;
 import org.apache.james.webadmin.WebAdminUtils;
+import org.apache.james.webadmin.service.GlobalQuotaService;
 import org.apache.james.webadmin.utils.JsonTransformer;
 import org.eclipse.jetty.http.HttpStatus;
 import org.junit.After;
@@ -50,7 +51,7 @@ public class GlobalQuotaRoutesTest {
         maxQuotaManager = new InMemoryPerUserMaxQuotaManager();
         webAdminServer = WebAdminUtils.createWebAdminServer(
             new DefaultMetricFactory(),
-            new GlobalQuotaRoutes(maxQuotaManager, new JsonTransformer()));
+            new GlobalQuotaRoutes(new GlobalQuotaService(maxQuotaManager), new JsonTransformer()));
         webAdminServer.configure(NO_CONFIGURATION);
         webAdminServer.await();
 


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