JAMES-2420 Add a DLPConfiguration object

Represent a collection of rules. This allow to share Id duplication checks
between WebAdmin and rule storage.


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

Branch: refs/heads/master
Commit: e6ef1744d7a3d639d0c77df574c7064f2ba1e75d
Parents: b1062f3
Author: Benoit Tellier <btell...@linagora.com>
Authored: Tue Sep 4 10:20:25 2018 +0700
Committer: Antoine Duprat <adup...@linagora.com>
Committed: Thu Sep 6 09:54:20 2018 +0200

----------------------------------------------------------------------
 .../apache/james/util/streams/Iterables.java    | 30 +++++++
 .../james/dlp/api/DLPConfigurationLoader.java   |  4 +-
 .../james/dlp/api/DLPConfigurationStore.java    | 15 ++--
 .../java/org/apache/james/dlp/api/DLPRules.java | 84 ++++++++++++++++++++
 .../dlp/api/DLPConfigurationStoreContract.java  |  4 +-
 server/data/data-library/pom.xml                |  4 +
 .../EventSourcingDLPConfigurationStore.java     | 10 +--
 .../aggregates/DLPDomainConfiguration.java      | 23 ++----
 .../eventsourcing/commands/StoreCommand.java    |  9 +--
 .../commands/StoreCommandTest.java              |  3 +-
 .../transport/matchers/dlp/DlpRulesLoader.java  |  6 +-
 .../james/webadmin/dto/DLPConfigurationDTO.java | 18 ++---
 .../webadmin/routes/DLPConfigurationRoutes.java | 28 +++----
 13 files changed, 165 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/container/util/src/main/java/org/apache/james/util/streams/Iterables.java
----------------------------------------------------------------------
diff --git 
a/server/container/util/src/main/java/org/apache/james/util/streams/Iterables.java
 
b/server/container/util/src/main/java/org/apache/james/util/streams/Iterables.java
new file mode 100644
index 0000000..b3730ba
--- /dev/null
+++ 
b/server/container/util/src/main/java/org/apache/james/util/streams/Iterables.java
@@ -0,0 +1,30 @@
+/****************************************************************
+ * 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.util.streams;
+
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+public class Iterables {
+
+    public static <T> Stream<T> toStream(Iterable<T> iterable) {
+        return StreamSupport.stream(iterable.spliterator(), false);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationLoader.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationLoader.java
 
b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationLoader.java
index e2d27f5..ccd7223 100644
--- 
a/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationLoader.java
+++ 
b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPConfigurationLoader.java
@@ -19,10 +19,8 @@
 
 package org.apache.james.dlp.api;
 
-import java.util.stream.Stream;
-
 import org.apache.james.core.Domain;
 
 public interface DLPConfigurationLoader {
-    Stream<DLPConfigurationItem> list(Domain domain);
+    DLPRules list(Domain domain);
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/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 2404b1e..b766d7f 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,9 +20,7 @@
 package org.apache.james.dlp.api;
 
 import java.util.Arrays;
-import java.util.List;
 import java.util.Optional;
-import java.util.stream.Stream;
 
 import org.apache.james.core.Domain;
 
@@ -30,13 +28,14 @@ import com.google.common.collect.ImmutableList;
 
 public interface DLPConfigurationStore extends DLPConfigurationLoader {
 
-    void store(Domain domain, List<DLPConfigurationItem> rule);
+    void store(Domain domain, DLPRules rule);
 
-    default void store(Domain domain, DLPConfigurationItem firstRules, 
DLPConfigurationItem... rules) {
-        store(domain, Stream.concat(
-                Stream.of(firstRules),
-                Arrays.stream(rules))
-            .collect(ImmutableList.toImmutableList()));
+    default void store(Domain domain, DLPConfigurationItem firstRule, 
DLPConfigurationItem... rules) {
+        store(domain, new DLPRules(
+            ImmutableList.<DLPConfigurationItem>builder()
+                .add(firstRule)
+                .addAll(Arrays.asList(rules))
+                .build()));
     }
 
     void clear(Domain domain);

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPRules.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPRules.java 
b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPRules.java
new file mode 100644
index 0000000..a048116
--- /dev/null
+++ b/server/data/data-api/src/main/java/org/apache/james/dlp/api/DLPRules.java
@@ -0,0 +1,84 @@
+/****************************************************************
+ * 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.dlp.api;
+
+import java.util.Iterator;
+import java.util.Objects;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+public class DLPRules implements Iterable<DLPConfigurationItem> {
+
+    public static final class DuplicateRulesIdsException extends 
RuntimeException {
+    }
+
+    private final ImmutableList<DLPConfigurationItem> items;
+
+    public DLPRules(ImmutableList<DLPConfigurationItem> items) throws 
DuplicateRulesIdsException {
+        Preconditions.checkNotNull(items);
+        checkNotContainDuplicateIds(items);
+
+        this.items = items;
+    }
+
+    private void 
checkNotContainDuplicateIds(ImmutableList<DLPConfigurationItem> items) throws 
DuplicateRulesIdsException {
+        long uniqueIdCount = items.stream()
+            .map(DLPConfigurationItem::getId)
+            .distinct()
+            .count();
+
+        if (uniqueIdCount != items.size()) {
+            throw new DuplicateRulesIdsException();
+        }
+    }
+
+    public ImmutableList<DLPConfigurationItem> getItems() {
+        return items;
+    }
+
+    @Override
+    public Iterator<DLPConfigurationItem> iterator() {
+        return items.iterator();
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof DLPRules) {
+            DLPRules dlpConfiguration = (DLPRules) o;
+
+            return Objects.equals(this.items, dlpConfiguration.items);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(items);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+            .add("items", items)
+            .toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-api/src/test/java/org/apache/james/dlp/api/DLPConfigurationStoreContract.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/test/java/org/apache/james/dlp/api/DLPConfigurationStoreContract.java
 
b/server/data/data-api/src/test/java/org/apache/james/dlp/api/DLPConfigurationStoreContract.java
index e2e7251..6ed3b0d 100644
--- 
a/server/data/data-api/src/test/java/org/apache/james/dlp/api/DLPConfigurationStoreContract.java
+++ 
b/server/data/data-api/src/test/java/org/apache/james/dlp/api/DLPConfigurationStoreContract.java
@@ -111,7 +111,7 @@ public interface DLPConfigurationStoreContract {
     @Test
     default void storeShouldRejectDuplicateIds(DLPConfigurationStore 
dlpConfigurationStore) {
         assertThatThrownBy(() -> dlpConfigurationStore.store(Domain.LOCALHOST, 
RULE, RULE))
-            .isInstanceOf(IllegalArgumentException.class);
+            .isInstanceOf(DLPRules.DuplicateRulesIdsException.class);
     }
 
     @Test
@@ -133,7 +133,7 @@ public interface DLPConfigurationStoreContract {
     @Test
     default void storeShouldClearRulesWhenEmpty(DLPConfigurationStore 
dlpConfigurationStore) {
         dlpConfigurationStore.store(Domain.LOCALHOST, RULE);
-        dlpConfigurationStore.store(Domain.LOCALHOST, ImmutableList.of());
+        dlpConfigurationStore.store(Domain.LOCALHOST, new 
DLPRules(ImmutableList.of()));
 
         assertThat(dlpConfigurationStore.list(Domain.LOCALHOST)).isEmpty();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-library/pom.xml
----------------------------------------------------------------------
diff --git a/server/data/data-library/pom.xml b/server/data/data-library/pom.xml
index 8b18787..dffd557 100644
--- a/server/data/data-library/pom.xml
+++ b/server/data/data-library/pom.xml
@@ -74,6 +74,10 @@
             <artifactId>james-server-lifecycle-api</artifactId>
         </dependency>
         <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-util</artifactId>
+        </dependency>
+        <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/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 0053ba0..1aeba39 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
@@ -19,9 +19,7 @@
 
 package org.apache.james.dlp.eventsourcing;
 
-import java.util.List;
 import java.util.Optional;
-import java.util.stream.Stream;
 
 import javax.inject.Inject;
 
@@ -29,6 +27,7 @@ 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.api.DLPRules;
 import org.apache.james.dlp.eventsourcing.aggregates.DLPAggregateId;
 import org.apache.james.dlp.eventsourcing.aggregates.DLPDomainConfiguration;
 import org.apache.james.dlp.eventsourcing.commands.ClearCommand;
@@ -38,6 +37,7 @@ import 
org.apache.james.dlp.eventsourcing.commands.StoreCommandHandler;
 import org.apache.james.eventsourcing.EventSourcingSystem;
 import org.apache.james.eventsourcing.Subscriber;
 import org.apache.james.eventsourcing.eventstore.EventStore;
+import org.apache.james.util.streams.Iterables;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -60,7 +60,7 @@ public class EventSourcingDLPConfigurationStore implements 
DLPConfigurationStore
     }
 
     @Override
-    public Stream<DLPConfigurationItem> list(Domain domain) {
+    public DLPRules list(Domain domain) {
 
         DLPAggregateId aggregateId = new DLPAggregateId(domain);
 
@@ -71,7 +71,7 @@ public class EventSourcingDLPConfigurationStore implements 
DLPConfigurationStore
     }
 
     @Override
-    public void store(Domain domain, List<DLPConfigurationItem> rules) {
+    public void store(Domain domain, DLPRules rules) {
         eventSourcingSystem.dispatch(new StoreCommand(domain, rules));
     }
 
@@ -82,7 +82,7 @@ public class EventSourcingDLPConfigurationStore implements 
DLPConfigurationStore
 
     @Override
     public Optional<DLPConfigurationItem> fetch(Domain domain, Id ruleId) {
-        return list(domain)
+        return Iterables.toStream(list(domain))
                 .filter((DLPConfigurationItem item) -> 
item.getId().equals(ruleId))
                 .findFirst();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/aggregates/DLPDomainConfiguration.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/aggregates/DLPDomainConfiguration.java
 
b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/aggregates/DLPDomainConfiguration.java
index 0666fbd..385b8fd 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/aggregates/DLPDomainConfiguration.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/aggregates/DLPDomainConfiguration.java
@@ -19,13 +19,13 @@
 
 package org.apache.james.dlp.eventsourcing.aggregates;
 
-import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Stream;
 
 import org.apache.james.dlp.api.DLPConfigurationItem;
+import org.apache.james.dlp.api.DLPRules;
 import org.apache.james.dlp.eventsourcing.events.ConfigurationItemsAdded;
 import org.apache.james.dlp.eventsourcing.events.ConfigurationItemsRemoved;
 import org.apache.james.eventsourcing.Event;
@@ -34,7 +34,6 @@ import org.apache.james.eventsourcing.eventstore.History;
 import org.apache.james.util.OptionalUtils;
 
 import com.github.steveash.guavate.Guavate;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
@@ -79,12 +78,12 @@ public class DLPDomainConfiguration {
         this.history = history;
     }
 
-    public Stream<DLPConfigurationItem> retrieveRules() {
-        return state.rules.stream();
+    public DLPRules retrieveRules() {
+        return new DLPRules(ImmutableList.copyOf(state.rules));
     }
 
     public List<Event> clear() {
-        ImmutableList<DLPConfigurationItem> rules = 
retrieveRules().collect(Guavate.toImmutableList());
+        ImmutableList<DLPConfigurationItem> rules = retrieveRules().getItems();
         if (!rules.isEmpty()) {
             ImmutableList<Event> events = ImmutableList.of(new 
ConfigurationItemsRemoved(aggregateId, history.getNextEventId(), rules));
             events.forEach(this::apply);
@@ -94,10 +93,8 @@ public class DLPDomainConfiguration {
         }
     }
 
-    public List<Event> store(List<DLPConfigurationItem> updatedRules) {
-        Preconditions.checkArgument(shouldNotContainDuplicates(updatedRules));
-
-        ImmutableSet<DLPConfigurationItem> existingRules = 
retrieveRules().collect(Guavate.toImmutableSet());
+    public List<Event> store(DLPRules updatedRules) {
+        ImmutableSet<DLPConfigurationItem> existingRules = 
retrieveRules().getItems().stream().collect(Guavate.toImmutableSet());
         ImmutableSet<DLPConfigurationItem> updatedRulesSet = 
ImmutableSet.copyOf(updatedRules);
 
         Optional<Event> removedRulesEvent = 
generateRemovedRulesEvent(existingRules, updatedRulesSet);
@@ -112,14 +109,6 @@ public class DLPDomainConfiguration {
         return events;
     }
 
-    private boolean 
shouldNotContainDuplicates(Collection<DLPConfigurationItem> items) {
-        long uniqueIdCount = items.stream()
-            .map(DLPConfigurationItem::getId)
-            .distinct()
-            .count();
-        return uniqueIdCount == items.size();
-    }
-
     private EventId computeNextEventId(Optional<Event> removedRulesEvent) {
         return removedRulesEvent
             .map(Event::eventId)

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/commands/StoreCommand.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/commands/StoreCommand.java
 
b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/commands/StoreCommand.java
index d48bfa4..5188b35 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/commands/StoreCommand.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/dlp/eventsourcing/commands/StoreCommand.java
@@ -19,11 +19,10 @@
 
 package org.apache.james.dlp.eventsourcing.commands;
 
-import java.util.List;
 import java.util.Objects;
 
 import org.apache.james.core.Domain;
-import org.apache.james.dlp.api.DLPConfigurationItem;
+import org.apache.james.dlp.api.DLPRules;
 import org.apache.james.eventsourcing.Command;
 
 import com.google.common.base.MoreObjects;
@@ -31,9 +30,9 @@ import com.google.common.base.Preconditions;
 
 public class StoreCommand implements Command {
     private final Domain domain;
-    private final List<DLPConfigurationItem> rules;
+    private final DLPRules rules;
 
-    public StoreCommand(Domain domain, List<DLPConfigurationItem> rules) {
+    public StoreCommand(Domain domain, DLPRules rules) {
         Preconditions.checkNotNull(domain);
         Preconditions.checkNotNull(rules);
 
@@ -45,7 +44,7 @@ public class StoreCommand implements Command {
         return domain;
     }
 
-    public List<DLPConfigurationItem> getRules() {
+    public DLPRules getRules() {
         return rules;
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/data/data-library/src/test/java/org/apache/james/dlp/eventsourcing/commands/StoreCommandTest.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/test/java/org/apache/james/dlp/eventsourcing/commands/StoreCommandTest.java
 
b/server/data/data-library/src/test/java/org/apache/james/dlp/eventsourcing/commands/StoreCommandTest.java
index b8be60f..f327caf 100644
--- 
a/server/data/data-library/src/test/java/org/apache/james/dlp/eventsourcing/commands/StoreCommandTest.java
+++ 
b/server/data/data-library/src/test/java/org/apache/james/dlp/eventsourcing/commands/StoreCommandTest.java
@@ -23,6 +23,7 @@ import static org.apache.james.dlp.api.DLPFixture.RULE;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import org.apache.james.core.Domain;
+import org.apache.james.dlp.api.DLPRules;
 import org.junit.Test;
 
 import com.google.common.collect.ImmutableList;
@@ -39,7 +40,7 @@ public class StoreCommandTest {
 
     @Test
     public void constructorShouldThrowWhenNullDomain() {
-        assertThatThrownBy(() -> new StoreCommand(null, 
ImmutableList.of(RULE)))
+        assertThatThrownBy(() -> new StoreCommand(null, new 
DLPRules(ImmutableList.of(RULE))))
             .isInstanceOf(NullPointerException.class);
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/dlp/DlpRulesLoader.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/dlp/DlpRulesLoader.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/dlp/DlpRulesLoader.java
index 13430d9..08e3c4d 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/dlp/DlpRulesLoader.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/dlp/DlpRulesLoader.java
@@ -19,13 +19,11 @@
 
 package org.apache.james.transport.matchers.dlp;
 
-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.DLPConfigurationStore;
+import org.apache.james.dlp.api.DLPRules;
 
 public interface DlpRulesLoader {
 
@@ -45,7 +43,7 @@ public interface DlpRulesLoader {
           return toRules(configurationStore.list(domain));
         }
 
-        private DlpDomainRules toRules(Stream<DLPConfigurationItem> items) {
+        private DlpDomainRules toRules(DLPRules items) {
             DlpDomainRules.DlpDomainRulesBuilder builder = 
DlpDomainRules.builder();
             items.forEach(item ->
                 item.getTargets().list().forEach(type ->

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationDTO.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationDTO.java
 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationDTO.java
index 913ccea..b696abd 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationDTO.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/main/java/org/apache/james/webadmin/dto/DLPConfigurationDTO.java
@@ -19,10 +19,9 @@
 
 package org.apache.james.webadmin.dto;
 
-import java.util.List;
-import java.util.stream.Stream;
-
-import org.apache.james.dlp.api.DLPConfigurationItem;
+import org.apache.james.dlp.api.DLPRules;
+import org.apache.james.dlp.api.DLPRules.DuplicateRulesIdsException;
+import org.apache.james.util.streams.Iterables;
 
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -33,11 +32,12 @@ import com.google.common.collect.ImmutableList;
 
 public class DLPConfigurationDTO {
 
-    public static DLPConfigurationDTO toDTO(Stream<DLPConfigurationItem> 
dlpConfigurations) {
+    public static DLPConfigurationDTO toDTO(DLPRules dlpConfigurations) {
         Preconditions.checkNotNull(dlpConfigurations);
 
         return new DLPConfigurationDTO(
-            dlpConfigurations.map(DLPConfigurationItemDTO::toDTO)
+            Iterables.toStream(dlpConfigurations)
+                .map(DLPConfigurationItemDTO::toDTO)
                 .collect(Guavate.toImmutableList()));
     }
 
@@ -54,9 +54,9 @@ public class DLPConfigurationDTO {
     }
 
     @JsonIgnore
-    public List<DLPConfigurationItem> toDLPConfigurations() {
-        return rules.stream()
+    public DLPRules toDLPConfiguration() throws DuplicateRulesIdsException {
+        return new DLPRules(rules.stream()
             .map(DLPConfigurationItemDTO::toDLPConfiguration)
-            .collect(Guavate.toImmutableList());
+            .collect(Guavate.toImmutableList()));
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e6ef1744/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 bb257db..1b550b9 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
@@ -24,10 +24,6 @@ import static org.apache.james.webadmin.Constants.EMPTY_BODY;
 import static org.apache.james.webadmin.Constants.JSON_CONTENT_TYPE;
 import static org.apache.james.webadmin.Constants.SEPARATOR;
 
-import java.util.Collection;
-import java.util.List;
-import java.util.stream.Stream;
-
 import javax.inject.Inject;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -39,6 +35,8 @@ 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.api.DLPRules;
+import org.apache.james.dlp.api.DLPRules.DuplicateRulesIdsException;
 import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.webadmin.Routes;
@@ -129,8 +127,7 @@ public class DLPConfigurationRoutes implements Routes {
             Domain senderDomain = parseDomain(request);
             DLPConfigurationDTO dto = jsonExtractor.parse(request.body());
 
-            List<DLPConfigurationItem> rules = dto.toDLPConfigurations();
-            shouldNotContainDuplicates(rules);
+            DLPRules rules = constructRules(dto);
 
             dlpConfigurationStore.store(senderDomain, rules);
 
@@ -139,9 +136,11 @@ public class DLPConfigurationRoutes implements Routes {
         });
     }
 
-    private void shouldNotContainDuplicates(Collection<DLPConfigurationItem> 
items) {
-        if (containsDuplicate(items)) {
-            ErrorResponder.builder()
+    private DLPRules constructRules(DLPConfigurationDTO dto) {
+        try {
+            return dto.toDLPConfiguration();
+        } catch (DuplicateRulesIdsException e) {
+            throw ErrorResponder.builder()
                 .statusCode(HttpStatus.BAD_REQUEST_400)
                 .type(ErrorType.INVALID_ARGUMENT)
                 .message("'id' duplicates are not allowed in DLP rules")
@@ -149,15 +148,6 @@ public class DLPConfigurationRoutes implements Routes {
         }
     }
 
-    private boolean containsDuplicate(Collection<DLPConfigurationItem> items) {
-        long uniqueIdCount = items.stream()
-            .map(DLPConfigurationItem::getId)
-            .distinct()
-            .count();
-
-        return uniqueIdCount != items.size();
-    }
-
     @GET
     @Path("/{senderDomain}")
     @ApiOperation(value = "Return a DLP configuration for a given 
senderDomain")
@@ -177,7 +167,7 @@ public class DLPConfigurationRoutes implements Routes {
     public void defineList(Service service) {
         service.get(SPECIFIC_DLP_RULE_DOMAIN, (request, response) -> {
             Domain senderDomain = parseDomain(request);
-            Stream<DLPConfigurationItem> dlpConfigurations = 
dlpConfigurationStore.list(senderDomain);
+            DLPRules dlpConfigurations = 
dlpConfigurationStore.list(senderDomain);
 
             DLPConfigurationDTO dto = 
DLPConfigurationDTO.toDTO(dlpConfigurations);
             response.status(HttpStatus.OK_200);


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

Reply via email to