JAMES-2341 Load SpamAssassin configuration from file
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/58c2debd Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/58c2debd Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/58c2debd Branch: refs/heads/master Commit: 58c2debda944e652ba272c86ad850df4525652a9 Parents: ae8652d Author: Antoine Duprat <adup...@linagora.com> Authored: Thu Feb 22 15:22:43 2018 +0100 Committer: Antoine Duprat <adup...@linagora.com> Committed: Wed Feb 28 13:29:31 2018 +0100 ---------------------------------------------------------------------- server/container/guice/cassandra-guice/pom.xml | 4 ++ .../SpamAssassinConfigurationLoader.java | 49 ++++++++++++++++ .../SpamAssassinConfigurationLoaderTest.java | 59 ++++++++++++++++++++ 3 files changed, 112 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/58c2debd/server/container/guice/cassandra-guice/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/pom.xml b/server/container/guice/cassandra-guice/pom.xml index a0e6b14..affbae2 100644 --- a/server/container/guice/cassandra-guice/pom.xml +++ b/server/container/guice/cassandra-guice/pom.xml @@ -78,6 +78,10 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>apache-james-mailbox-spamassassin</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>apache-james-mailbox-tika</artifactId> <type>test-jar</type> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/58c2debd/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoader.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoader.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoader.java new file mode 100644 index 0000000..35a2077 --- /dev/null +++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoader.java @@ -0,0 +1,49 @@ +/**************************************************************** + * 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.modules.mailbox; + +import java.util.Optional; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.mailbox.spamassassin.SpamAssassinConfiguration; +import org.apache.james.util.Host; + +public class SpamAssassinConfigurationLoader { + + private static final String SPAMASSASSIN_HOST = "spamassassin.host"; + private static final String SPAMASSASSIN_PORT = "spamassassin.port"; + public static final String DEFAULT_HOST = "127.0.0.1"; + public static final int DEFAULT_PORT = 783; + + public static SpamAssassinConfiguration disable() { + return new SpamAssassinConfiguration(Optional.empty()); + } + + public static SpamAssassinConfiguration fromProperties(PropertiesConfiguration configuration) throws ConfigurationException { + Host host = getHost(configuration); + return new SpamAssassinConfiguration(Optional.of(host)); + } + + private static Host getHost(PropertiesConfiguration propertiesReader) throws ConfigurationException { + return Host.from(propertiesReader.getString(SPAMASSASSIN_HOST, DEFAULT_HOST), + propertiesReader.getInteger(SPAMASSASSIN_PORT, DEFAULT_PORT)); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/58c2debd/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoaderTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoaderTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoaderTest.java new file mode 100644 index 0000000..a2c8f31 --- /dev/null +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/SpamAssassinConfigurationLoaderTest.java @@ -0,0 +1,59 @@ +/**************************************************************** + * 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.modules.mailbox; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.mailbox.spamassassin.SpamAssassinConfiguration; +import org.apache.james.util.Host; +import org.junit.Test; + +public class SpamAssassinConfigurationLoaderTest { + + @Test + public void isEnableShouldReturnFalseWhenDisable() { + SpamAssassinConfiguration configuration = SpamAssassinConfigurationLoader.disable(); + assertThat(configuration.isEnable()).isFalse(); + } + + @Test + public void isEnableShouldReturnTrueWhenEnable() throws Exception { + SpamAssassinConfiguration configuration = SpamAssassinConfigurationLoader.fromProperties(new PropertiesConfiguration()); + assertThat(configuration.isEnable()).isTrue(); + } + + @Test + public void hostShouldReturnDefaultWhenConfigurationIsEmpty() throws Exception { + SpamAssassinConfiguration configuration = SpamAssassinConfigurationLoader.fromProperties(new PropertiesConfiguration()); + assertThat(configuration.getHost().get()).isEqualTo(Host.from(SpamAssassinConfigurationLoader.DEFAULT_HOST, SpamAssassinConfigurationLoader.DEFAULT_PORT)); + } + + @Test + public void hostShouldReturnCustomWhenConfigurationIsProvided() throws Exception { + PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(); + String host = "10.69.1.123"; + propertiesConfiguration.addProperty("spamassassin.host", host); + int port = 1783; + propertiesConfiguration.addProperty("spamassassin.port", port); + + SpamAssassinConfiguration configuration = SpamAssassinConfigurationLoader.fromProperties(propertiesConfiguration); + assertThat(configuration.getHost().get()).isEqualTo(Host.from(host, port)); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org