This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch postgresql
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit eca9ed2d9c90083572623ed0c85d6f04874ec453
Author: Rene Cordier <[email protected]>
AuthorDate: Tue Nov 21 15:03:41 2023 +0700

    JAMES-2586 Implement PostgresDomainList
---
 .../james/domainlist/jpa/PostgresDomainList.java   | 61 ++++++++++++++++++++++
 .../james/domainlist/jpa/PostgresDomainModule.java | 27 ++++++++++
 .../domainlist/jpa/PostgresDomainListTest.java     | 29 ++++++++++
 3 files changed, 117 insertions(+)

diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
new file mode 100644
index 0000000000..2135c84ed5
--- /dev/null
+++ 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainList.java
@@ -0,0 +1,61 @@
+package org.apache.james.domainlist.jpa;
+
+import static 
org.apache.james.domainlist.jpa.PostgresDomainModule.PostgresDomainTable.DOMAIN;
+import static 
org.apache.james.domainlist.jpa.PostgresDomainModule.PostgresDomainTable.TABLE_NAME;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.core.Domain;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.domainlist.lib.AbstractDomainList;
+import org.jooq.exception.DataAccessException;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class PostgresDomainList extends AbstractDomainList {
+    private final PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresDomainList(DNSService dnsService, PostgresExecutor 
postgresExecutor) {
+        super(dnsService);
+        this.postgresExecutor = postgresExecutor;
+    }
+
+    @Override
+    public void addDomain(Domain domain) throws DomainListException {
+        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.insertInto(TABLE_NAME, DOMAIN)
+                .values(domain.asString())))
+            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " already exists."))
+            .block();
+
+    }
+
+    @Override
+    protected List<Domain> getDomainListInternal() throws DomainListException {
+        return postgresExecutor.executeRows(dsl -> 
Flux.from(dsl.selectFrom(TABLE_NAME)))
+            .map(record -> Domain.of(record.get(DOMAIN)))
+            .collectList()
+            .block();
+    }
+
+    @Override
+    protected boolean containsDomainInternal(Domain domain) throws 
DomainListException {
+        return postgresExecutor.executeRow(dsl -> 
Mono.from(dsl.selectFrom(TABLE_NAME)
+            .where(DOMAIN.eq(domain.asString()))))
+            .blockOptional()
+            .isPresent();
+    }
+
+    @Override
+    protected void doRemoveDomain(Domain domain) throws DomainListException {
+        postgresExecutor.executeVoid(dslContext -> 
Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(DOMAIN.eq(domain.asString()))))
+            .onErrorMap(DataAccessException.class, e -> new 
DomainListException(domain.name() + " was not found"))
+            .block();
+    }
+}
diff --git 
a/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
new file mode 100644
index 0000000000..9a99e5e785
--- /dev/null
+++ 
b/server/data/data-postgres/src/main/java/org/apache/james/domainlist/jpa/PostgresDomainModule.java
@@ -0,0 +1,27 @@
+package org.apache.james.domainlist.jpa;
+
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTable;
+import org.jooq.Field;
+import org.jooq.Record;
+import org.jooq.Table;
+import org.jooq.impl.DSL;
+import org.jooq.impl.SQLDataType;
+
+public interface PostgresDomainModule {
+    interface PostgresDomainTable {
+        Table<Record> TABLE_NAME = DSL.table("domains");
+
+        Field<String> DOMAIN = DSL.field("domain", 
SQLDataType.VARCHAR.notNull());
+
+        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
+            .createTableStep(((dsl, tableName) -> 
dsl.createTableIfNotExists(tableName)
+                .column(DOMAIN)
+                .constraint(DSL.primaryKey(DOMAIN))))
+            .disableRowLevelSecurity();
+    }
+
+    PostgresModule MODULE = PostgresModule.builder()
+        .addTable(PostgresDomainTable.TABLE)
+        .build();
+}
diff --git 
a/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
 
b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
new file mode 100644
index 0000000000..909a1e8286
--- /dev/null
+++ 
b/server/data/data-postgres/src/test/java/org/apache/james/domainlist/jpa/PostgresDomainListTest.java
@@ -0,0 +1,29 @@
+package org.apache.james.domainlist.jpa;
+
+import org.apache.james.backends.postgres.PostgresExtension;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.lib.DomainListConfiguration;
+import org.apache.james.domainlist.lib.DomainListContract;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class PostgresDomainListTest implements DomainListContract {
+    @RegisterExtension
+    static PostgresExtension postgresExtension = 
PostgresExtension.withoutRowLevelSecurity(PostgresDomainModule.MODULE);
+
+    PostgresDomainList domainList;
+
+    @BeforeEach
+    public void setup() throws Exception {
+        domainList = new PostgresDomainList(getDNSServer("localhost"), 
postgresExtension.getPostgresExecutor());
+        domainList.configure(DomainListConfiguration.builder()
+            .autoDetect(false)
+            .autoDetectIp(false)
+            .build());
+    }
+
+    @Override
+    public DomainList domainList() {
+        return domainList;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to