james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [4/5] james-project git commit: JAMES-2536 Fetch a DLP configuration item by sender domain and rule id
Date Wed, 05 Sep 2018 09:49:47 GMT
JAMES-2536 Fetch a DLP configuration item by sender domain and rule id


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

Branch: refs/heads/master
Commit: 96e215560e4767efa1f49d20dd92963f0da942d0
Parents: 3f2f8a7
Author: Gautier DI FOLCO <gdifolco@linagora.com>
Authored: Mon Sep 3 15:08:51 2018 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Wed Sep 5 11:49:07 2018 +0200

----------------------------------------------------------------------
 .../james/dlp/api/DLPConfigurationStore.java    |   5 +-
 .../EventSourcingDLPConfigurationStore.java     |   9 ++
 .../webadmin/dto/DLPConfigurationItemDTO.java   |   4 +-
 .../webadmin/routes/DLPConfigurationRoutes.java |  41 +++++++-
 .../routes/DLPConfigurationRoutesTest.java      | 101 +++++++++++++++++++
 src/site/markdown/server/manage-webadmin.md     |  29 ++++++
 6 files changed, 184 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationStore.java
----------------------------------------------------------------------
diff --git a/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationStore.java
b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationStore.java
index 4272f49..291b932 100644
--- a/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationStore.java
+++ b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationStore.java
@@ -20,6 +20,7 @@
 package org.apache.james.dlp.api;
 
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.james.core.Domain;
 
@@ -34,5 +35,7 @@ public interface DLPConfigurationStore extends DLPConfigurationLoader {
     }
 
     void clear(Domain domain);
-    
+
+    Optional<DLPConfigurationItem> fetch(Domain domain, DLPConfigurationItem.Id ruleId);
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/EventSourcingDLPConfigurationStore.java
----------------------------------------------------------------------
diff --git a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/EventSourcingDLPConfigurationStore.java
b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/EventSourcingDLPConfigurationStore.java
index f367135..0053ba0 100644
--- a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/EventSourcingDLPConfigurationStore.java
+++ b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/EventSourcingDLPConfigurationStore.java
@@ -20,12 +20,14 @@
 package org.apache.james.dlp.eventsourcing;
 
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Stream;
 
 import javax.inject.Inject;
 
 import org.apache.james.core.Domain;
 import org.apache.james.dlp.api.DLPConfigurationItem;
+import org.apache.james.dlp.api.DLPConfigurationItem.Id;
 import org.apache.james.dlp.api.DLPConfigurationStore;
 import org.apache.james.dlp.eventsourcing.aggregates.DLPAggregateId;
 import org.apache.james.dlp.eventsourcing.aggregates.DLPDomainConfiguration;
@@ -78,4 +80,11 @@ public class EventSourcingDLPConfigurationStore implements DLPConfigurationStore
         eventSourcingSystem.dispatch(new ClearCommand(domain));
     }
 
+    @Override
+    public Optional<DLPConfigurationItem> fetch(Domain domain, Id ruleId) {
+        return list(domain)
+                .filter((DLPConfigurationItem item) -> item.getId().equals(ruleId))
+                .findFirst();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationItemDTO.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationItemDTO.java
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationItemDTO.java
index 4ba6416..0b54f42 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationItemDTO.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationItemDTO.java
@@ -27,13 +27,11 @@ import org.apache.james.dlp.api.DLPConfigurationItem;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
 public class DLPConfigurationItemDTO {
 
-    @VisibleForTesting
-    static DLPConfigurationItemDTO toDTO(DLPConfigurationItem dlpConfiguration) {
+    public static DLPConfigurationItemDTO toDTO(DLPConfigurationItem dlpConfiguration) {
         DLPConfigurationItem.Targets targets = dlpConfiguration.getTargets();
         return new DLPConfigurationItemDTO(
             dlpConfiguration.getId().asString(),

http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DLPConfigurationRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DLPConfigurationRoutes.java
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DLPConfigurationRoutes.java
index 6bd321b..9da61f0 100644
--- a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DLPConfigurationRoutes.java
+++ b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/routes/DLPConfigurationRoutes.java
@@ -35,11 +35,13 @@ import javax.ws.rs.Produces;
 
 import org.apache.james.core.Domain;
 import org.apache.james.dlp.api.DLPConfigurationItem;
+import org.apache.james.dlp.api.DLPConfigurationItem.Id;
 import org.apache.james.dlp.api.DLPConfigurationStore;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.webadmin.Routes;
 import org.apache.james.webadmin.dto.DLPConfigurationDTO;
+import org.apache.james.webadmin.dto.DLPConfigurationItemDTO;
 import org.apache.james.webadmin.utils.ErrorResponder;
 import org.apache.james.webadmin.utils.ErrorResponder.ErrorType;
 import org.apache.james.webadmin.utils.JsonExtractor;
@@ -72,6 +74,9 @@ public class DLPConfigurationRoutes implements Routes {
     private static final String DOMAIN_NAME = ":senderDomain";
     private static final String SPECIFIC_DLP_RULE_DOMAIN = BASE_PATH + SEPARATOR + DOMAIN_NAME;
 
+    private static final String RULE_ID_NAME = ":ruleId";
+    private static final String RULE_SPECIFIC_PATH = SPECIFIC_DLP_RULE_DOMAIN + SEPARATOR
+ "rules" + SEPARATOR + RULE_ID_NAME;
+
     private final JsonTransformer jsonTransformer;
     private final DLPConfigurationStore dlpConfigurationStore;
     private final JsonExtractor<DLPConfigurationDTO> jsonExtractor;
@@ -98,6 +103,8 @@ public class DLPConfigurationRoutes implements Routes {
         defineList(service);
 
         defineClear(service);
+
+        defineFetch(service);
     }
 
     @PUT
@@ -131,7 +138,7 @@ public class DLPConfigurationRoutes implements Routes {
 
     @GET
     @Path("/{senderDomain}")
-    @ApiOperation(value = "Return a DLP configuration for given senderDomain")
+    @ApiOperation(value = "Return a DLP configuration for a given senderDomain")
     @ApiImplicitParams({
         @ApiImplicitParam(required = true, dataType = "string", name = "senderDomain", paramType
= "path")
     })
@@ -185,6 +192,38 @@ public class DLPConfigurationRoutes implements Routes {
         }, jsonTransformer);
     }
 
+    @GET
+    @Path("/{senderDomain}/rules/{ruleId}")
+    @ApiOperation(value = "Return a DLP rule for a given senderDomain and a ruleId")
+    @ApiImplicitParams({
+        @ApiImplicitParam(required = true, dataType = "string", name = "senderDomain", paramType
= "path"),
+        @ApiImplicitParam(required = true, dataType = "string", name = "ruleId", paramType
= "path")
+    })
+    @ApiResponses(value = {
+        @ApiResponse(code = HttpStatus.OK_200, message = "OK. DLP rule is returned", response
= DLPConfigurationItemDTO.class),
+        @ApiResponse(code = HttpStatus.BAD_REQUEST_400, message = "Invalid senderDomain in
request",
+            response = ErrorResponder.ErrorDetail.class),
+        @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "The domain and/or the rule
does not exist.",
+            response = ErrorResponder.ErrorDetail.class),
+        @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
+            message = "Internal server error - Something went bad on the server side.",
+            response = ErrorResponder.ErrorDetail.class)
+    })
+    public void defineFetch(Service service) {
+        service.get(RULE_SPECIFIC_PATH, (request, response) -> {
+            Domain senderDomain = parseDomain(request);
+            Id ruleId = DLPConfigurationItem.Id.of(request.params(RULE_ID_NAME));
+            DLPConfigurationItem dlpConfigurationItem = dlpConfigurationStore
+                .fetch(senderDomain, ruleId)
+                .orElseThrow(() -> notFound("There is no rule '" + ruleId.asString() +
"' for '" + senderDomain.asString() + "' managed by this James server"));
+
+            DLPConfigurationItemDTO dto = DLPConfigurationItemDTO.toDTO(dlpConfigurationItem);
+            response.status(HttpStatus.OK_200);
+            response.header(CONTENT_TYPE, JSON_CONTENT_TYPE);
+            return dto;
+        }, jsonTransformer);
+    }
+
     private Domain parseDomain(Request request) {
         String domainName = request.params(DOMAIN_NAME);
         try {

http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DLPConfigurationRoutesTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DLPConfigurationRoutesTest.java
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DLPConfigurationRoutesTest.java
index 9bc2aa0..72d5a8a 100644
--- a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DLPConfigurationRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DLPConfigurationRoutesTest.java
@@ -24,6 +24,8 @@ import static io.restassured.RestAssured.requestSpecification;
 import static io.restassured.RestAssured.when;
 import static io.restassured.RestAssured.with;
 import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
+import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_FIELDS;
 import static org.apache.james.webadmin.Constants.JSON_CONTENT_TYPE;
 import static org.apache.james.webadmin.WebAdminServer.NO_CONFIGURATION;
 import static org.hamcrest.Matchers.containsString;
@@ -698,4 +700,103 @@ class DLPConfigurationRoutesTest {
                 .body("details", is("Domain can not be empty nor contain `@`"));
         }
     }
+    
+    @Nested
+    class DefineFetch {
+        @Test
+        public void fetchShouldBeOK() throws Exception {
+            storeRules();
+
+            String jsonAsString =
+                when()
+                    .get(DEFAULT_DOMAIN + "/rules/1")
+                .then()
+                    .statusCode(HttpStatus.OK_200)
+                    .contentType(ContentType.JSON)
+                    .extract()
+                        .body()
+                        .asString();
+
+            assertThatJson(jsonAsString)
+                .when(IGNORING_ARRAY_ORDER)
+                .when(IGNORING_EXTRA_FIELDS)
+                    .isEqualTo("{" +
+                            "    \"id\": \"1\"," +
+                            "    \"expression\": \"expression 1\"," +
+                            "    \"explanation\": \"explanation 1\"," +
+                            "    \"targetsSender\": true," +
+                            "    \"targetsRecipients\": true," +
+                            "    \"targetsContent\": true" +
+                            "}");
+        }
+
+        @Test
+        public void fetchOnUnknownDomainShouldBe404() throws Exception {
+            storeRules();
+
+            when()
+                .get("strange.com/rules/1")
+            .then()
+                .statusCode(HttpStatus.NOT_FOUND_404)
+                .contentType(JSON_CONTENT_TYPE)
+                .body("statusCode", is(HttpStatus.NOT_FOUND_404))
+                .body("type", is("InvalidArgument"))
+                .body("message", is("'strange.com' is not managed by this James server"));
+        }
+
+        @Test
+        public void fetchOnUnknownDomainAndRuleShouldBe404() throws Exception {
+            when()
+                .get("strange.com/rules/666")
+            .then()
+                .statusCode(HttpStatus.NOT_FOUND_404)
+                .contentType(JSON_CONTENT_TYPE)
+                .body("statusCode", is(HttpStatus.NOT_FOUND_404))
+                .body("type", is("InvalidArgument"))
+                .body("message", is("'strange.com' is not managed by this James server"));
+        }
+
+        @Test
+        public void fetchOnUnknownRuleIdShouldBe404() throws Exception {
+            storeRules();
+
+            when()
+                .get(DEFAULT_DOMAIN + "/rules/666")
+            .then()
+                .statusCode(HttpStatus.NOT_FOUND_404)
+                .contentType(JSON_CONTENT_TYPE)
+                .body("statusCode", is(HttpStatus.NOT_FOUND_404))
+                .body("type", is("InvalidArgument"))
+                .body("message", is("There is no rule '666' for '" + DEFAULT_DOMAIN + "'
managed by this James server"));
+        }
+
+        private void storeRules() {
+            String storeBody =
+                "{\"rules\": [" +
+                "  {" +
+                "    \"id\": \"1\"," +
+                "    \"expression\": \"expression 1\"," +
+                "    \"explanation\": \"explanation 1\"," +
+                "    \"targetsSender\": true," +
+                "    \"targetsRecipients\": true," +
+                "    \"targetsContent\": true" +
+                "  }," +
+                "  {" +
+                "    \"id\": \"2\"," +
+                "    \"expression\": \"expression 2\"," +
+                "    \"explanation\": \"explanation 2\"," +
+                "    \"targetsSender\": true," +
+                "    \"targetsRecipients\": false," +
+                "    \"targetsContent\": false" +
+                "  }]}";
+
+            given()
+                .body(storeBody)
+            .when()
+                .put(DEFAULT_DOMAIN)
+            .then()
+                .statusCode(HttpStatus.NO_CONTENT_204);
+
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/96e21556/src/site/markdown/server/manage-webadmin.md
----------------------------------------------------------------------
diff --git a/src/site/markdown/server/manage-webadmin.md b/src/site/markdown/server/manage-webadmin.md
index 2e869c2..f935551 100644
--- a/src/site/markdown/server/manage-webadmin.md
+++ b/src/site/markdown/server/manage-webadmin.md
@@ -1809,6 +1809,7 @@ Each `senderDomain` correspond to a distinct DLP configuration.
 - [List DLP configuration by sender domain](List_dlp_configuration_by_sender_domain)
 - [Store DLP configuration by sender domain](Store_dlp_configuration_by_sender_domain)
 - [Remove DLP configuration by sender domain](Remove_dlp_configuration_by_sender_domain)
+- [Fetch a DLP configuration item by sender domain and rule id](Fetch_a_dlp_configuration_item_by_sender_domain_and_rule_id)
 
 ### List DLP configuration by sender domain
 
@@ -1907,6 +1908,34 @@ Response codes:
  - 400: Invalid senderDomain or payload in request
  - 404: The domain does not exist.
 
+
+### Fetch a DLP configuration item by sender domain and rule id
+
+Retrieve a DLP configuration rule for corresponding `senderDomain` and a `ruleId`
+
+```
+curl -XGET http://ip:port/dlp/rules/senderDomain/rules/ruleId
+```
+
+Response codes:
+
+ - 200: A dlp configuration item is returned
+ - 400: Invalid senderDomain or payload in request
+ - 404: The domain and/or the rule does not exist.
+
+This is an example of returned body.
+
+```
+{
+  "id": "1",
+  "expression": "james.org",
+  "explanation": "Find senders or recipients containing james[any char]org",
+  "targetsSender": true,
+  "targetsRecipients": true,
+  "targetsContent": false
+}
+```
+
 ## Administrating Sieve quotas
 
 Some limitations on space Users Sieve script can occupy can be configured by default, and
overridden by user.


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


Mime
View raw message