JAMES-1759 Integration tests for WebAdmin server
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/12ef929d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/12ef929d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/12ef929d Branch: refs/heads/master Commit: 12ef929deda69dc100f4964ff6d3a3874ab78b13 Parents: 1ab4194 Author: Benoit Tellier <[email protected]> Authored: Wed Jun 22 15:33:32 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Wed Jun 22 15:35:23 2016 +0700 ---------------------------------------------------------------------- .../java/org/apache/james/GuiceJamesServer.java | 18 +- server/pom.xml | 1 + .../protocols/webadmin-integration-test/pom.xml | 269 +++++++++++++++++++ .../WebAdminConfigurationModule.java | 50 ++++ .../WebAdminServerIntegrationTest.java | 149 ++++++++++ .../src/test/resources/dnsservice.xml | 29 ++ .../test/resources/fakemailrepositorystore.xml | 31 +++ .../src/test/resources/imapserver.xml | 54 ++++ .../src/test/resources/keystore | Bin 0 -> 2245 bytes .../src/test/resources/lmtpserver.xml | 41 +++ .../src/test/resources/mailetcontainer.xml | 113 ++++++++ .../src/test/resources/mailrepositorystore.xml | 31 +++ .../src/test/resources/managesieveserver.xml | 65 +++++ .../src/test/resources/pop3server.xml | 42 +++ .../src/test/resources/smtpserver.xml | 105 ++++++++ .../src/test/resources/webadmin.properties | 2 + .../org/apache/james/webadmin/FixedPort.java | 38 +++ .../java/org/apache/james/webadmin/Port.java | 26 ++ .../org/apache/james/webadmin/RandomPort.java | 50 ++++ .../apache/james/webadmin/WebAdminServer.java | 17 +- .../james/webadmin/utils/JsonTransformer.java | 3 +- .../apache/james/webadmin/FixedPortTest.java | 50 ++++ .../apache/james/webadmin/RandomPortTest.java | 34 +++ 23 files changed, 1205 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java ---------------------------------------------------------------------- diff --git a/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java index 1283147..d3ed9f3 100644 --- a/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java +++ b/server/container/guice/guice-common/src/main/java/org/apache/james/GuiceJamesServer.java @@ -19,6 +19,7 @@ package org.apache.james; import java.util.Arrays; +import java.util.Optional; import javax.annotation.PreDestroy; @@ -29,6 +30,8 @@ import org.apache.james.modules.ProtocolsModule; import org.apache.james.utils.ConfigurationsPerformer; import org.apache.james.utils.ExtendedServerProbe; import org.apache.james.utils.GuiceServerProbe; +import org.apache.james.webadmin.Port; +import org.apache.james.webadmin.WebAdminServer; import org.apache.onami.lifecycle.core.Stager; import com.google.common.collect.Iterables; @@ -45,8 +48,8 @@ public class GuiceJamesServer { private Stager<PreDestroy> preDestroy; private GuiceServerProbe serverProbe; private int jmapPort; + private Optional<Port> webadminPort; - public GuiceJamesServer() { this(Modules.combine( new CommonServicesModule(), @@ -72,6 +75,15 @@ public class GuiceJamesServer { injector.getInstance(ConfigurationsPerformer.class).initModules(); serverProbe = injector.getInstance(GuiceServerProbe.class); jmapPort = injector.getInstance(JMAPServer.class).getPort(); + webadminPort =locateWebAdminPort(injector); + } + + private Optional<Port> locateWebAdminPort(Injector injector) { + try { + return Optional.of(injector.getInstance(WebAdminServer.class).getPort()); + } catch(Exception e) { + return Optional.empty(); + } } public void stop() { @@ -87,4 +99,8 @@ public class GuiceJamesServer { public int getJmapPort() { return jmapPort; } + + public Optional<Port> getWebadminPort() { + return webadminPort; + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/pom.xml ---------------------------------------------------------------------- diff --git a/server/pom.xml b/server/pom.xml index c33c7c1..1c7dd87 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -105,6 +105,7 @@ <module>queue/queue-jms</module> <module>queue/queue-activemq</module> <module>protocols/webadmin</module> + <module>protocols/webadmin-integration-test</module> </modules> <distributionManagement> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/pom.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/pom.xml b/server/protocols/webadmin-integration-test/pom.xml new file mode 100644 index 0000000..8058357 --- /dev/null +++ b/server/protocols/webadmin-integration-test/pom.xml @@ -0,0 +1,269 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <artifactId>james-server</artifactId> + <groupId>org.apache.james</groupId> + <version>3.0.0-beta5-SNAPSHOT</version> + <relativePath>../../pom.xml</relativePath> + </parent> + + <artifactId>james-server-webadmin-integration-test</artifactId> + <packaging>jar</packaging> + + <name>Apache James :: Server :: Web Admin server integration tests</name> + + <profiles> + <profile> + <id>noTest</id> + <activation> + <os> + <family>windows</family> + </os> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <skipTests>true</skipTests> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>disable-build-for-older-jdk</id> + <activation> + <jdk>(,1.8)</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-jar-plugin</artifactId> + <executions> + <execution> + <id>default-jar</id> + <phase>none</phase> + </execution> + <execution> + <id>jar</id> + <phase>none</phase> + </execution> + <execution> + <id>test-jar</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + <executions> + <execution> + <id>default-compile</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testCompile</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <executions> + <execution> + <id>default-test</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-install-plugin</artifactId> + <executions> + <execution> + <id>default-install</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>default-resources</id> + <phase>none</phase> + </execution> + <execution> + <id>default-testResources</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <executions> + <execution> + <id>attach-descriptor</id> + <phase>none</phase> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>build-for-jdk-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <dependencies> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-james-mailbox-elasticsearch</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-cassandra-guice</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-webadmin</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-cassandra-guice</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.james</groupId> + <artifactId>james-server-guice-common</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.jayway.restassured</groupId> + <artifactId>rest-assured</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-3.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.cassandraunit</groupId> + <artifactId>cassandra-unit</artifactId> + <version>${cassandra-unit.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>java-hamcrest</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <archive> + <manifest> + <mainClass>fully.qualified.MainClass</mainClass> + </manifest> + </archive> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + </plugins> + </build> + </profile> + <profile> + <id>animal-sniffer-java-8</id> + <activation> + <jdk>[1.8,)</jdk> + </activation> + <build> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>animal-sniffer-maven-plugin</artifactId> + <configuration> + <signature> + <groupId>org.codehaus.mojo.signature</groupId> + <artifactId>java18</artifactId> + <version>1.0</version> + </signature> + </configuration> + <executions> + <execution> + <id>check_java_8</id> + <phase>test</phase> + <goals> + <goal>check</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminConfigurationModule.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminConfigurationModule.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminConfigurationModule.java new file mode 100644 index 0000000..f8728dd --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminConfigurationModule.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.integration; + +import static org.apache.james.webadmin.WebAdminServer.WEBADMIN_ENABLED; +import static org.apache.james.webadmin.WebAdminServer.WEBADMIN_PORT; + +import org.apache.james.webadmin.Port; +import org.apache.james.webadmin.RandomPort; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.name.Named; + +public class WebAdminConfigurationModule extends AbstractModule { + + @Override + protected void configure() { + + } + + @Provides + @Named(WEBADMIN_PORT) + public Port provideWebAdminPort() throws Exception { + return new RandomPort(); + } + + @Provides + @Named(WEBADMIN_ENABLED) + public boolean provideWebAdminEnabled() throws Exception { + return true; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java new file mode 100644 index 0000000..d720623 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/WebAdminServerIntegrationTest.java @@ -0,0 +1,149 @@ +/**************************************************************** + * 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.integration; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.RestAssured.when; +import static com.jayway.restassured.config.EncoderConfig.encoderConfig; +import static com.jayway.restassured.config.RestAssuredConfig.newConfig; +import static org.apache.james.webadmin.Constants.SEPARATOR; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.is; + +import org.apache.james.CassandraJamesServerMain; +import org.apache.james.GuiceJamesServer; +import org.apache.james.backends.cassandra.EmbeddedCassandra; +import org.apache.james.mailbox.elasticsearch.EmbeddedElasticSearch; +import org.apache.james.modules.CassandraJmapServerModule; +import org.apache.james.webadmin.routes.DomainRoutes; +import org.apache.james.webadmin.routes.UserRoutes; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.junit.rules.TemporaryFolder; + +import com.google.common.base.Charsets; +import com.jayway.restassured.RestAssured; +import com.jayway.restassured.parsing.Parser; + +public class WebAdminServerIntegrationTest { + + public static final String DOMAIN = "domain"; + public static final String USERNAME = "username@" + DOMAIN; + public static final String SPECIFIC_DOMAIN = DomainRoutes.DOMAINS + SEPARATOR + DOMAIN; + public static final String SPECIFIC_USER = UserRoutes.USERS + SEPARATOR + USERNAME; + + private TemporaryFolder temporaryFolder = new TemporaryFolder(); + private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder); + private EmbeddedCassandra cassandra = EmbeddedCassandra.createStartServer(); + + @Rule + public RuleChain chain = RuleChain + .outerRule(temporaryFolder) + .around(embeddedElasticSearch); + + private GuiceJamesServer guiceJamesServer; + + @Before + public void setUp() throws Exception { + guiceJamesServer = new GuiceJamesServer() + .combineWith(CassandraJamesServerMain.cassandraServerModule) + .overrideWith(new CassandraJmapServerModule(temporaryFolder, embeddedElasticSearch, cassandra), + new WebAdminConfigurationModule()); + guiceJamesServer.start(); + + RestAssured.port = guiceJamesServer.getWebadminPort() + .orElseThrow(() -> new RuntimeException("Unable to locate Web Admin port")) + .toInt(); + RestAssured.config = newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)); + RestAssured.defaultParser = Parser.JSON; + } + + @After + public void tearDown() { + guiceJamesServer.stop(); + } + + @Test + public void postShouldAddTheGivenDomain() throws Exception { + when() + .put(SPECIFIC_DOMAIN) + .then() + .statusCode(204); + + assertThat(guiceJamesServer.serverProbe().listDomains()).contains(DOMAIN); + } + + @Test + public void deleteShouldRemoveTheGivenDomain() throws Exception { + guiceJamesServer.serverProbe().addDomain(DOMAIN); + + when() + .delete(SPECIFIC_DOMAIN) + .then() + .statusCode(204); + + assertThat(guiceJamesServer.serverProbe().listDomains()).doesNotContain(DOMAIN); + } + + @Test + public void postShouldAddTheUser() throws Exception { + guiceJamesServer.serverProbe().addDomain(DOMAIN); + + given() + .body("{\"password\":\"password\"}") + .when() + .put(SPECIFIC_USER) + .then() + .statusCode(204); + + assertThat(guiceJamesServer.serverProbe().listUsers()).contains(USERNAME); + } + + @Test + public void deleteShouldRemoveTheUser() throws Exception { + guiceJamesServer.serverProbe().addDomain(DOMAIN); + guiceJamesServer.serverProbe().addUser(USERNAME, "anyPassword"); + + given() + .body("{\"username\":\"" + USERNAME + "\",\"password\":\"password\"}") + .when() + .delete(SPECIFIC_USER) + .then() + .statusCode(204); + + assertThat(guiceJamesServer.serverProbe().listUsers()).doesNotContain(USERNAME); + } + + @Test + public void getUsersShouldDisplayUsers() throws Exception { + guiceJamesServer.serverProbe().addDomain(DOMAIN); + guiceJamesServer.serverProbe().addUser(USERNAME, "anyPassword"); + + when() + .get(UserRoutes.USERS) + .then() + .statusCode(200) + .body(is("[{\"username\":\"username@domain\"}]")); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/dnsservice.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/dnsservice.xml b/server/protocols/webadmin-integration-test/src/test/resources/dnsservice.xml new file mode 100644 index 0000000..0978a00 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/dnsservice.xml @@ -0,0 +1,29 @@ +<?xml version="1.0"?> +<!-- + 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. + --> + +<dnsservice> + <servers> + <server>8.8.8.8</server> + <server>62.210.16.6</server> + </servers> + <autodiscover>false</autodiscover> + <authoritative>false</authoritative> + <maxcachesize>50000</maxcachesize> +</dnsservice> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/fakemailrepositorystore.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/fakemailrepositorystore.xml b/server/protocols/webadmin-integration-test/src/test/resources/fakemailrepositorystore.xml new file mode 100644 index 0000000..2d19a80 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/fakemailrepositorystore.xml @@ -0,0 +1,31 @@ +<?xml version="1.0"?> + +<!-- + 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. + --> + +<mailrepositorystore> + <mailrepositories> + <mailrepository class="org.apache.james.mailrepository.None"> + <protocols> + <protocol>file</protocol> + </protocols> + <config FIFO="false" CACHEKEYS="true"/> + </mailrepository> + </mailrepositories> +</mailrepositorystore> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/imapserver.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/imapserver.xml b/server/protocols/webadmin-integration-test/src/test/resources/imapserver.xml new file mode 100644 index 0000000..ff478a9 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/imapserver.xml @@ -0,0 +1,54 @@ +<?xml version="1.0"?> + +<!-- +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. +--> + + +<imapservers> + <imapserver enabled="true"> + <jmxName>imapserver</jmxName> + <bind>0.0.0.0:1143</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <!-- To create a new keystore execute: + keytool -genkey -alias james -keyalg RSA -keystore /path/to/james/conf/keystore + --> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + </tls> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + </imapserver> + <imapserver enabled="true"> + <jmxName>imapserver-ssl</jmxName> + <bind>0.0.0.0:1993</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <!-- To create a new keystore execute: + keytool -genkey -alias james -keyalg RSA -keystore /path/to/james/conf/keystore + --> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + </tls> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + </imapserver> +</imapservers> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/keystore ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/keystore b/server/protocols/webadmin-integration-test/src/test/resources/keystore new file mode 100644 index 0000000..536a6c7 Binary files /dev/null and b/server/protocols/webadmin-integration-test/src/test/resources/keystore differ http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/lmtpserver.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/lmtpserver.xml b/server/protocols/webadmin-integration-test/src/test/resources/lmtpserver.xml new file mode 100644 index 0000000..5c4a9c7 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/lmtpserver.xml @@ -0,0 +1,41 @@ +<?xml version="1.0"?> +<!-- + 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. + --> + +<lmtpservers> + + <lmtpserver enabled="true"> + <jmxName>lmtpserver</jmxName> + <!-- LMTP should not be reachable from outside your network so bind it to loopback--> + <bind>127.0.0.1:1024</bind> + <connectionBacklog>200</connectionBacklog> + <connectiontimeout>1200</connectiontimeout> + <!-- Set the maximum simultaneous incoming connections for this service --> + <connectionLimit>0</connectionLimit> + <!-- Set the maximum simultaneous incoming connections per IP for this service --> + <connectionLimitPerIP>0</connectionLimitPerIP> + <!-- This sets the maximum allowed message size (in kilobytes) for this --> + <!-- LMTP service. If unspecified, the value defaults to 0, which means no limit. --> + <maxmessagesize>0</maxmessagesize> + <handlerchain> + <handler class="org.apache.james.lmtpserver.CoreCmdHandlerLoader"/> + </handlerchain> + </lmtpserver> + +</lmtpservers> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/mailetcontainer.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/mailetcontainer.xml b/server/protocols/webadmin-integration-test/src/test/resources/mailetcontainer.xml new file mode 100644 index 0000000..b265f93 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/mailetcontainer.xml @@ -0,0 +1,113 @@ +<?xml version="1.0"?> + +<!-- + 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. + --> + +<mailetcontainer enableJmx="false"> + + <context> + <postmaster>[email protected]</postmaster> + </context> + + <spooler> + <threads>20</threads> + </spooler> + + <processors> + <processor state="root" enableJmx="false"> + <mailet match="All" class="PostmasterAlias"/> + <mailet match="RelayLimit=30" class="Null"/> + <mailet match="All" class="ToProcessor"> + <processor>transport</processor> + </mailet> + </processor> + + <processor state="error" enableJmx="false"> + <mailet match="All" class="Bounce"/> + <mailet match="All" class="ToRepository"> + <repositoryPath>file://var/mail/error/</repositoryPath> + </mailet> + </processor> + + + <processor state="transport" enableJmx="false"> + <mailet match="SMTPAuthSuccessful" class="SetMimeHeader"> + <name>X-UserIsAuth</name> + <value>true</value> + </mailet> + <mailet match="All" class="RemoveMimeHeader"> + <name>bcc</name> + </mailet> + <mailet match="All" class="RecipientRewriteTable" /> + <mailet match="RecipientIsLocal" class="org.apache.james.jmap.mailet.VacationMailet"/> + <mailet match="RecipientIsLocal" class="LocalDelivery"/> + <mailet match="HostIsLocal" class="ToProcessor"> + <processor>local-address-error</processor> + <notice>550 - Requested action not taken: no such user here</notice> + </mailet> + <mailet match="SMTPAuthSuccessful" class="RemoteDelivery"> + <outgoingQueue>outgoing</outgoingQueue> + <delayTime>5000, 100000, 500000</delayTime> + <maxRetries>25</maxRetries> + <maxDnsProblemRetries>0</maxDnsProblemRetries> + <deliveryThreads>10</deliveryThreads> + <sendpartial>true</sendpartial> + <bounceProcessor>bounces</bounceProcessor> + </mailet> + <mailet match="All" class="ToProcessor"> + <processor>relay-denied</processor> + </mailet> + </processor> + + <processor state="spam" enableJmx="false"> + <mailet match="All" class="ToRepository"> + <repositoryPath>file://var/mail/spam/</repositoryPath> + </mailet> + </processor> + + <processor state="local-address-error" enableJmx="false"> + <mailet match="All" class="Bounce"> + <attachment>none</attachment> + </mailet> + <mailet match="All" class="ToRepository"> + <repositoryPath>file://var/mail/address-error/</repositoryPath> + </mailet> + </processor> + + <processor state="relay-denied" enableJmx="false"> + <mailet match="All" class="Bounce"> + <attachment>none</attachment> + </mailet> + <mailet match="All" class="ToRepository"> + <repositoryPath>file://var/mail/relay-denied/</repositoryPath> + <notice>Warning: You are sending an e-mail to a remote server. You must be authentified to perform such an operation</notice> + </mailet> + </processor> + + <processor state="bounces" enableJmx="false"> + <mailet match="All" class="DSNBounce"> + <passThrough>false</passThrough> + </mailet> + </processor> + + </processors> + +</mailetcontainer> + + http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/mailrepositorystore.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/mailrepositorystore.xml b/server/protocols/webadmin-integration-test/src/test/resources/mailrepositorystore.xml new file mode 100644 index 0000000..3ca4a1d --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/mailrepositorystore.xml @@ -0,0 +1,31 @@ +<?xml version="1.0"?> + +<!-- + 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. + --> + +<mailrepositorystore> + <mailrepositories> + <mailrepository class="org.apache.james.mailrepository.file.FileMailRepository"> + <protocols> + <protocol>file</protocol> + </protocols> + <config FIFO="false" CACHEKEYS="true"/> + </mailrepository> + </mailrepositories> +</mailrepositorystore> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/managesieveserver.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/managesieveserver.xml b/server/protocols/webadmin-integration-test/src/test/resources/managesieveserver.xml new file mode 100644 index 0000000..ec57e09 --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/managesieveserver.xml @@ -0,0 +1,65 @@ +<?xml version="1.0"?> +<!-- + 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. + --> + +<!-- + This template file can be used as example for James Server configuration + DO NOT USE IT AS SUCH AND ADAPT IT TO YOUR NEEDS +--> + +<!-- See http://james.apache.org/server/3/config.html for usage --> + +<managesieveservers> + + <managesieveserver enabled="true"> + + <jmxName>managesieveserver</jmxName> + + <bind>0.0.0.0:4190</bind> + + <connectionBacklog>200</connectionBacklog> + + <tls socketTLS="false" startTLS="false"> + <!-- To create a new keystore execute: + keytool -genkey -alias james -keyalg RSA -keystore /path/to/james/conf/keystore + --> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + <!-- The algorithm is optional and only needs to be specified when using something other + than the Sun JCE provider - You could use IbmX509 with IBM Java runtime. --> + <algorithm>SunX509</algorithm> + </tls> + + <!-- connection timeout in secconds --> + <connectiontimeout>360</connectiontimeout> + + <!-- Set the maximum simultaneous incoming connections for this service --> + <connectionLimit>0</connectionLimit> + + <!-- Set the maximum simultaneous incoming connections per IP for this service --> + <connectionLimitPerIP>0</connectionLimitPerIP> + <maxmessagesize>0</maxmessagesize> + <addressBracketsEnforcement>true</addressBracketsEnforcement> + + </managesieveserver> + +</managesieveservers> + + http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/pop3server.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/pop3server.xml b/server/protocols/webadmin-integration-test/src/test/resources/pop3server.xml new file mode 100644 index 0000000..e4187da --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/pop3server.xml @@ -0,0 +1,42 @@ +<?xml version="1.0"?> +<!-- + 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. + --> + + +<pop3servers> + <pop3server enabled="true"> + <jmxName>pop3server</jmxName> + <bind>0.0.0.0:1110</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <!-- To create a new keystore execute: + keytool -genkey -alias james -keyalg RSA -keystore /path/to/james/conf/keystore + --> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + </tls> + <connectiontimeout>1200</connectiontimeout> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + <handlerchain> + <handler class="org.apache.james.pop3server.core.CoreCmdHandlerLoader"/> + </handlerchain> + </pop3server> +</pop3servers> http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/smtpserver.xml ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/smtpserver.xml b/server/protocols/webadmin-integration-test/src/test/resources/smtpserver.xml new file mode 100644 index 0000000..a3d4b8f --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/smtpserver.xml @@ -0,0 +1,105 @@ +<?xml version="1.0"?> + +<!-- + 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. + --> + +<smtpservers> + <smtpserver enabled="true"> + <jmxName>smtpserver-global</jmxName> + <bind>0.0.0.0:1025</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + <algorithm>SunX509</algorithm> + </tls> + <connectiontimeout>360</connectiontimeout> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + <authRequired>false</authRequired> + <authorizedAddresses>0.0.0.0/0</authorizedAddresses> + <verifyIdentity>true</verifyIdentity> + <maxmessagesize>0</maxmessagesize> + <addressBracketsEnforcement>true</addressBracketsEnforcement> + <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting> + <handlerchain> + <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/> + <handler class="org.apache.james.smtpserver.CoreCmdHandlerLoader"/> + </handlerchain> + </smtpserver> + <smtpserver enabled="true"> + <jmxName>smtpserver-TLS</jmxName> + <bind>0.0.0.0:10465</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + <algorithm>SunX509</algorithm> + </tls> + <connectiontimeout>360</connectiontimeout> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + <!-- + Authorize only local users + --> + <authRequired>true</authRequired> + <authorizedAddresses>0.0.0.0/0</authorizedAddresses> + <!-- Trust authenticated users --> + <verifyIdentity>false</verifyIdentity> + <maxmessagesize>0</maxmessagesize> + <addressBracketsEnforcement>true</addressBracketsEnforcement> + <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting> + <handlerchain> + <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/> + <handler class="org.apache.james.smtpserver.CoreCmdHandlerLoader"/> + </handlerchain> + </smtpserver> + <smtpserver enabled="true"> + <jmxName>smtpserver-authenticated</jmxName> + <bind>0.0.0.0:1587</bind> + <connectionBacklog>200</connectionBacklog> + <tls socketTLS="false" startTLS="false"> + <keystore>file://conf/keystore</keystore> + <secret>james72laBalle</secret> + <provider>org.bouncycastle.jce.provider.BouncyCastleProvider</provider> + <algorithm>SunX509</algorithm> + </tls> + <connectiontimeout>360</connectiontimeout> + <connectionLimit>0</connectionLimit> + <connectionLimitPerIP>0</connectionLimitPerIP> + <!-- + Authorize only local users + --> + <authRequired>true</authRequired> + <authorizedAddresses>0.0.0.0/0</authorizedAddresses> + <!-- Trust authenticated users --> + <verifyIdentity>false</verifyIdentity> + <maxmessagesize>0</maxmessagesize> + <addressBracketsEnforcement>true</addressBracketsEnforcement> + <smtpGreeting>JAMES Linagora's SMTP awesome Server</smtpGreeting> + <handlerchain> + <handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/> + <handler class="org.apache.james.smtpserver.CoreCmdHandlerLoader"/> + </handlerchain> + </smtpserver> +</smtpservers> + + http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin-integration-test/src/test/resources/webadmin.properties ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin-integration-test/src/test/resources/webadmin.properties b/server/protocols/webadmin-integration-test/src/test/resources/webadmin.properties new file mode 100644 index 0000000..09f46dd --- /dev/null +++ b/server/protocols/webadmin-integration-test/src/test/resources/webadmin.properties @@ -0,0 +1,2 @@ +enabled=true +port=8000 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java new file mode 100644 index 0000000..e977f09 --- /dev/null +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/FixedPort.java @@ -0,0 +1,38 @@ +/**************************************************************** + * 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; + +import com.google.common.base.Preconditions; + +public class FixedPort implements Port { + + private final int port; + + public FixedPort(int port) { + Preconditions.checkArgument(port > 0 && port < 65536, "Port should be strictly contained between 0 and 65536"); + this.port = port; + } + + @Override + public int toInt() { + return port; + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Port.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Port.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Port.java new file mode 100644 index 0000000..ff7a50b --- /dev/null +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/Port.java @@ -0,0 +1,26 @@ +/**************************************************************** + * 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; + +public interface Port { + + int toInt(); + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/RandomPort.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/RandomPort.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/RandomPort.java new file mode 100644 index 0000000..2b3ab5d --- /dev/null +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/RandomPort.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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; + +import java.io.IOException; +import java.net.ServerSocket; + +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.base.Throwables; + +public class RandomPort implements Port { + + public static int findFreePort() { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + + private final Supplier<Integer> portSupplier; + + public RandomPort() { + portSupplier = Suppliers.memoize(RandomPort::findFreePort); + } + + @Override + public int toInt() { + return portSupplier.get(); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java index 7ca5499..b777917 100644 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/WebAdminServer.java @@ -20,7 +20,6 @@ package org.apache.james.webadmin; import java.io.IOException; -import java.net.ServerSocket; import java.util.Set; import javax.annotation.PreDestroy; @@ -46,20 +45,16 @@ public class WebAdminServer implements Configurable { public static final String WEBADMIN_ENABLED = "webadmin_enabled"; public static final int DEFAULT_PORT = 8080; - private final int port; + private final Port port; private final Set<Routes> routesList; private final boolean enabled; private final Service service; // Spark do not allow to retrieve allocated port when using a random port. Thus we generate the port. - public static int findFreePort() throws IOException { - try (ServerSocket socket = new ServerSocket(0)) { - return socket.getLocalPort(); - } - } + @Inject - private WebAdminServer(@Named(WEBADMIN_ENABLED) boolean enabled, @Named(WEBADMIN_PORT)int port, Set<Routes> routesList) { + private WebAdminServer(@Named(WEBADMIN_ENABLED) boolean enabled, @Named(WEBADMIN_PORT)Port port, Set<Routes> routesList) { this.port = port; this.routesList = routesList; this.enabled = enabled; @@ -68,13 +63,13 @@ public class WebAdminServer implements Configurable { @VisibleForTesting public WebAdminServer(Routes... routes) throws IOException { - this(true, findFreePort(), ImmutableSet.copyOf(routes)); + this(true, new RandomPort(), ImmutableSet.copyOf(routes)); } @Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { if (enabled) { - service.port(port); + service.port(port.toInt()); routesList.forEach(routes -> routes.define(service)); LOGGER.info("Web admin server started"); } @@ -92,7 +87,7 @@ public class WebAdminServer implements Configurable { service.awaitInitialization(); } - public int getPort() { + public Port getPort() { return port; } } http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java index c1a17f9..3c32f0c 100644 --- a/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java +++ b/server/protocols/webadmin/src/main/java/org/apache/james/webadmin/utils/JsonTransformer.java @@ -19,6 +19,7 @@ package org.apache.james.webadmin.utils; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -34,7 +35,7 @@ public class JsonTransformer implements ResponseTransformer { } @Override - public String render(Object o) throws Exception { + public String render(Object o) throws JsonProcessingException { return objectMapper.writeValueAsString(o); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java new file mode 100644 index 0000000..eb6abdb --- /dev/null +++ b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/FixedPortTest.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; + +public class FixedPortTest { + + @Test + public void toIntShouldThrowOnNegativePort() { + assertThatThrownBy(() -> new FixedPort(-1)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void toIntShouldThrowOnNullPort() { + assertThatThrownBy(() -> new FixedPort(0)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void toIntShouldThrowOnTooBigNumbers() { + assertThatThrownBy(() -> new FixedPort(65536)).isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void toIntShouldReturnedDesiredPort() { + int expectedPort = 452; + assertThat(new FixedPort(expectedPort).toInt()).isEqualTo(expectedPort); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/12ef929d/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/RandomPortTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/RandomPortTest.java b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/RandomPortTest.java new file mode 100644 index 0000000..07d1996 --- /dev/null +++ b/server/protocols/webadmin/src/test/java/org/apache/james/webadmin/RandomPortTest.java @@ -0,0 +1,34 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class RandomPortTest { + + @Test + public void toIntShouldReturnTwoTimeTheSameResult() { + RandomPort testee = new RandomPort(); + assertThat(testee.toInt()).isEqualTo(testee.toInt()); + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
