JAMES-2262 Factorize port operations

Many classes were doing port validation by themselves. We needed an helper 
centralizing this concern.

Concerning:
 - ElasticSearch ClientProviderImpl: Port checks are done in the underlying 
Host class, no need to keep duplicating
 - Webadmin: Previous Port class was more a PortProvider. This avoids a naming 
conflict.


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

Branch: refs/heads/master
Commit: 27b89085557b672cf82d9db0d042b58323b7ff75
Parents: d11a7e4
Author: benwa <[email protected]>
Authored: Mon Dec 18 10:06:16 2017 +0700
Committer: benwa <[email protected]>
Committed: Mon Dec 25 11:24:56 2017 +0700

----------------------------------------------------------------------
 .../james/backends/es/ClientProviderImpl.java   |  5 --
 .../james/mailbox/tika/TikaConfiguration.java   |  3 +
 .../java/org/apache/james/cli/ServerCmd.java    | 13 +--
 .../cli/exceptions/InvalidPortException.java    | 28 ------
 .../org/apache/james/cli/ServerCmdTest.java     |  7 +-
 .../modules/server/WebAdminServerModule.java    |  4 +-
 .../apache/james/utils/WebAdminGuiceProbe.java  |  2 +-
 server/container/jetty/pom.xml                  |  4 +
 .../apache/james/http/jetty/Configuration.java  |  8 +-
 .../james/http/jetty/JettyHttpServerTest.java   | 11 +--
 .../metrics/es/ESReporterConfiguration.java     |  5 ++
 .../main/java/org/apache/james/util/Host.java   |  2 +-
 .../main/java/org/apache/james/util/Port.java   | 54 ++++++++++++
 .../java/org/apache/james/util/PortTest.java    | 92 ++++++++++++++++++++
 .../james/transport/mailets/SpamAssassin.java   |  6 +-
 .../transport/mailets/SpamAssassinTest.java     |  6 +-
 .../routes/CassandraMigrationRoutesTest.java    |  2 +-
 .../org/apache/james/webadmin/FixedPort.java    | 54 ------------
 .../james/webadmin/FixedPortSupplier.java       | 53 +++++++++++
 .../java/org/apache/james/webadmin/Port.java    | 26 ------
 .../org/apache/james/webadmin/PortSupplier.java | 28 ++++++
 .../org/apache/james/webadmin/RandomPort.java   | 50 -----------
 .../james/webadmin/RandomPortSupplier.java      | 51 +++++++++++
 .../james/webadmin/WebAdminConfiguration.java   | 12 +--
 .../apache/james/webadmin/WebAdminServer.java   |  4 +-
 .../james/webadmin/FixedPortSupplierTest.java   | 57 ++++++++++++
 .../apache/james/webadmin/FixedPortTest.java    | 57 ------------
 .../james/webadmin/RandomPortSupplierTest.java  | 34 ++++++++
 .../apache/james/webadmin/RandomPortTest.java   | 34 --------
 .../webadmin/WebAdminConfigurationTest.java     |  2 +-
 .../apache/james/webadmin/WebAdminUtils.java    |  2 +-
 .../webadmin/routes/DomainsRoutesTest.java      |  2 +-
 .../james/webadmin/routes/GroupsRoutesTest.java |  2 +-
 .../james/webadmin/routes/UsersRoutesTest.java  |  2 +-
 .../webadmin/routes/GlobalQuotaRoutesTest.java  |  2 +-
 .../routes/UserMailboxesRoutesTest.java         |  2 +-
 .../james/webadmin/swagger/SwaggerParser.java   |  4 +-
 37 files changed, 422 insertions(+), 308 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ClientProviderImpl.java
----------------------------------------------------------------------
diff --git 
a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ClientProviderImpl.java
 
b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ClientProviderImpl.java
index 6118739..d4d3e4b 100644
--- 
a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ClientProviderImpl.java
+++ 
b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/ClientProviderImpl.java
@@ -33,7 +33,6 @@ import com.google.common.collect.ImmutableList;
 public class ClientProviderImpl implements ClientProvider {
 
     public static ClientProviderImpl forHost(String address, Integer port) {
-        isValidPort(port);
         return new ClientProviderImpl(ImmutableList.of(Host.from(address, 
port)));
     }
 
@@ -47,10 +46,6 @@ public class ClientProviderImpl implements ClientProvider {
         return new ClientProviderImpl(hosts);
     }
 
-    private static boolean isValidPort(Integer port) {
-        return port > 0 && port <= 65535;
-    }
-
     private final ImmutableList<Host> hosts;
 
     private ClientProviderImpl(ImmutableList<Host> hosts) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/TikaConfiguration.java
----------------------------------------------------------------------
diff --git 
a/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/TikaConfiguration.java
 
b/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/TikaConfiguration.java
index 1915fb6..633afc3 100644
--- 
a/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/TikaConfiguration.java
+++ 
b/mailbox/tika/src/main/java/org/apache/james/mailbox/tika/TikaConfiguration.java
@@ -21,6 +21,8 @@ package org.apache.james.mailbox.tika;
 
 import java.util.Optional;
 
+import org.apache.james.util.Port;
+
 import com.google.common.base.Preconditions;
 
 public class TikaConfiguration {
@@ -60,6 +62,7 @@ public class TikaConfiguration {
             Preconditions.checkState(host.isPresent(), "'host' is mandatory");
             Preconditions.checkState(port.isPresent(), "'port' is mandatory");
             Preconditions.checkState(timeoutInMillis.isPresent(), 
"'timeoutInMillis' is mandatory");
+            Port.assertValid(port.get());
 
             return new TikaConfiguration(host.get(), port.get(), 
timeoutInMillis.get());
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
----------------------------------------------------------------------
diff --git 
a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java 
b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
index 9a9ae83..2c4d73f 100644
--- a/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
+++ b/server/container/cli/src/main/java/org/apache/james/cli/ServerCmd.java
@@ -34,7 +34,6 @@ import org.apache.commons.cli.PosixParser;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.time.StopWatch;
 import org.apache.james.cli.exceptions.InvalidArgumentNumberException;
-import org.apache.james.cli.exceptions.InvalidPortException;
 import org.apache.james.cli.exceptions.JamesCliException;
 import org.apache.james.cli.exceptions.MissingCommandException;
 import org.apache.james.cli.exceptions.UnrecognizedCommandException;
@@ -52,6 +51,7 @@ import org.apache.james.mailbox.store.probe.QuotaProbe;
 import org.apache.james.mailbox.store.probe.SieveProbe;
 import org.apache.james.probe.DataProbe;
 import org.apache.james.rrt.lib.Mappings;
+import org.apache.james.util.Port;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -159,7 +159,9 @@ public class ServerCmd {
         String portNum = cmd.getOptionValue(PORT_OPT_LONG);
         if (!Strings.isNullOrEmpty(portNum)) {
             try {
-                return validatePortNumber(Integer.parseInt(portNum));
+                int portNumber = Integer.parseInt(portNum);
+                Port.assertValid(portNumber);
+                return portNumber;
             } catch (NumberFormatException e) {
                 throw new ParseException("Port must be a number");
             }
@@ -167,13 +169,6 @@ public class ServerCmd {
         return DEFAULT_PORT;
     }
 
-    private static int validatePortNumber(int portNumber) {
-        if (portNumber < 1 || portNumber > 65535) {
-            throw new InvalidPortException(portNumber);
-        }
-        return portNumber;
-    }
-
     private static void failWithMessage(String s) {
         System.err.println(s);
         printUsage();

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
----------------------------------------------------------------------
diff --git 
a/server/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
 
b/server/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
deleted file mode 100644
index bd659fb..0000000
--- 
a/server/container/cli/src/main/java/org/apache/james/cli/exceptions/InvalidPortException.java
+++ /dev/null
@@ -1,28 +0,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.                                           *
- ****************************************************************/
-
-package org.apache.james.cli.exceptions;
-
-public class InvalidPortException extends JamesCliException {
-
-    public InvalidPortException(int invalidPortNumber) {
-        super( invalidPortNumber + " is not a valid port number. Please 
provide one between 1 and 65535");
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java 
b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
index b797683..aa24c29 100644
--- a/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
+++ b/server/container/cli/src/test/java/org/apache/james/cli/ServerCmdTest.java
@@ -31,7 +31,6 @@ import java.util.HashMap;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.ParseException;
 import org.apache.james.cli.exceptions.InvalidArgumentNumberException;
-import org.apache.james.cli.exceptions.InvalidPortException;
 import org.apache.james.cli.exceptions.MissingCommandException;
 import org.apache.james.cli.exceptions.UnrecognizedCommandException;
 import org.apache.james.cli.type.CmdType;
@@ -1364,7 +1363,7 @@ public class ServerCmdTest {
         CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
 
         assertThatThrownBy(() -> ServerCmd.getPort(commandLine))
-            .isInstanceOf(InvalidPortException.class);
+            .isInstanceOf(IllegalArgumentException.class);
     }
 
     @Test
@@ -1373,7 +1372,7 @@ public class ServerCmdTest {
         CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
 
         assertThatThrownBy(() -> ServerCmd.getPort(commandLine))
-            .isInstanceOf(InvalidPortException.class);
+            .isInstanceOf(IllegalArgumentException.class);
     }
 
     @Test
@@ -1382,7 +1381,7 @@ public class ServerCmdTest {
         CommandLine commandLine = ServerCmd.parseCommandLine(arguments);
 
         assertThatThrownBy(() -> ServerCmd.getPort(commandLine))
-            .isInstanceOf(InvalidPortException.class);
+            .isInstanceOf(IllegalArgumentException.class);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java
 
b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java
index 88c1cd6..1a140ee 100644
--- 
a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java
+++ 
b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/modules/server/WebAdminServerModule.java
@@ -34,7 +34,7 @@ import org.apache.james.utils.ConfigurationPerformer;
 import org.apache.james.utils.GuiceProbe;
 import org.apache.james.utils.PropertiesProvider;
 import org.apache.james.utils.WebAdminGuiceProbe;
-import org.apache.james.webadmin.FixedPort;
+import org.apache.james.webadmin.FixedPortSupplier;
 import org.apache.james.webadmin.TlsConfiguration;
 import org.apache.james.webadmin.WebAdminConfiguration;
 import org.apache.james.webadmin.WebAdminServer;
@@ -82,7 +82,7 @@ public class WebAdminServerModule extends AbstractModule {
             PropertiesConfiguration configurationFile = 
propertiesProvider.getConfiguration("webadmin");
             return WebAdminConfiguration.builder()
                 .enable(configurationFile.getBoolean("enabled", 
DEFAULT_DISABLED))
-                .port(new FixedPort(configurationFile.getInt("port", 
WebAdminServer.DEFAULT_PORT)))
+                .port(new FixedPortSupplier(configurationFile.getInt("port", 
WebAdminServer.DEFAULT_PORT)))
                 .tls(readHttpsConfiguration(configurationFile))
                 .enableCORS(configurationFile.getBoolean("cors.enable", 
DEFAULT_CORS_DISABLED))
                 .urlCORSOrigin(configurationFile.getString("cors.origin", 
DEFAULT_NO_CORS_ORIGIN))

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java
----------------------------------------------------------------------
diff --git 
a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java
 
b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java
index a68155c..40cde64 100644
--- 
a/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java
+++ 
b/server/container/guice/protocols/webadmin/src/main/java/org/apache/james/utils/WebAdminGuiceProbe.java
@@ -32,6 +32,6 @@ public class WebAdminGuiceProbe implements GuiceProbe {
     }
 
     public int getWebAdminPort() {
-        return webAdminServer.getPort().toInt();
+        return webAdminServer.getPort().get().getValue();
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/jetty/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/jetty/pom.xml b/server/container/jetty/pom.xml
index ae4a65c..1ec9a8b 100644
--- a/server/container/jetty/pom.xml
+++ b/server/container/jetty/pom.xml
@@ -34,6 +34,10 @@
 
     <dependencies>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>james-core</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.github.fge</groupId>
             <artifactId>throwing-lambdas</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
----------------------------------------------------------------------
diff --git 
a/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
 
b/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
index f638e9f..13b43af 100644
--- 
a/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
+++ 
b/server/container/jetty/src/main/java/org/apache/james/http/jetty/Configuration.java
@@ -24,11 +24,12 @@ import java.util.Optional;
 import javax.servlet.Filter;
 import javax.servlet.Servlet;
 
+import org.apache.james.util.Port;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableListMultimap;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Range;
 
 public class Configuration {
 
@@ -41,8 +42,7 @@ public class Configuration {
     }
     
     public static class Builder {
-        
-        private static final Range<Integer> VALID_PORT_RANGE = Range.closed(1, 
65535);
+
         private static final String TEMPLATE_LEVEL1 = "/*";
 
         private final ImmutableMap.Builder<String, Object> mappings;
@@ -130,7 +130,7 @@ public class Configuration {
         }
 
         public Builder port(int port) {
-            Preconditions.checkArgument(VALID_PORT_RANGE.contains(port));
+            Port.assertValid(port);
             this.port = Optional.of(port);
             return this;
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
 
b/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
index 07c4538..17cb1f4 100644
--- 
a/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
+++ 
b/server/container/jetty/src/test/java/org/apache/james/http/jetty/JettyHttpServerTest.java
@@ -23,7 +23,6 @@ import static com.jayway.restassured.RestAssured.when;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.IOException;
-import java.util.Random;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -35,6 +34,7 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.james.util.Port;
 import org.hamcrest.Matchers;
 import org.junit.After;
 import org.junit.Before;
@@ -102,18 +102,11 @@ public class JettyHttpServerTest {
     
     @Test
     public void shouldStartOnConfiguredPort() throws Exception {
-        int port = generateValidUnprivilegedPort();
+        int port = Port.generateValidUnprivilegedPort();
         testee = 
JettyHttpServer.create(configurationBuilder.port(port).build()).start();
         assertThat(testee.getPort()).isEqualTo(port);
     }
     
-
-    private int generateValidUnprivilegedPort() {
-        int portBound = 65535;
-        int privilegedPortBound = 1024;
-        return new Random().nextInt(portBound - privilegedPortBound) + 
privilegedPortBound;
-    }
-    
     @Test
     public void shouldReturn404WhenNoServletConfigured() throws Exception {
         testee = JettyHttpServer.create(configurationBuilder.build()).start();

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java
----------------------------------------------------------------------
diff --git 
a/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java
 
b/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java
index afed836..75aa0a7 100644
--- 
a/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java
+++ 
b/server/container/metrics/metrics-es-reporter/src/main/java/org/apache/james/metrics/es/ESReporterConfiguration.java
@@ -21,6 +21,8 @@ package org.apache.james.metrics.es;
 
 import java.util.Optional;
 
+import org.apache.james.util.Port;
+
 import com.google.common.base.Preconditions;
 
 public class ESReporterConfiguration {
@@ -66,6 +68,9 @@ public class ESReporterConfiguration {
             Preconditions.checkState(enabled.isPresent(), "You must specify 
either enabled or disabled");
             Preconditions.checkState(!enabled.get() || host.isPresent(), "You 
must specify host when enabled");
             Preconditions.checkState(!enabled.get() || port.isPresent(), "You 
must specify port when enabled");
+            if (enabled.get()) {
+                Port.assertValid(port.get());
+            }
             return new ESReporterConfiguration(host, port, enabled.get(), 
index, periodInSecond);
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/main/java/org/apache/james/util/Host.java 
b/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
index 9adc41c..4337aa3 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
@@ -101,7 +101,7 @@ public class Host {
     Host(String hostName, int port) {
         Preconditions.checkNotNull(hostName, "Hostname could not be null");
         Preconditions.checkArgument(!Strings.isNullOrEmpty(hostName), 
"Hostname could not be empty");
-        Preconditions.checkArgument(port > 0 && port <= 65535, "Port should be 
between 0 and 65535");
+        Port.assertValid(port);
         this.hostName = hostName;
         this.port = port;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/util-java8/src/main/java/org/apache/james/util/Port.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/main/java/org/apache/james/util/Port.java 
b/server/container/util-java8/src/main/java/org/apache/james/util/Port.java
new file mode 100644
index 0000000..2acc4b6
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/Port.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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;
+
+import java.util.Random;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Range;
+
+public class Port {
+    public static final int MAX_PORT_VALUE = 65535;
+    public static final int PRIVILEGED_PORT_BOUND = 1024;
+    private static final Range<Integer> VALID_PORT_RANGE = Range.closed(1, 
MAX_PORT_VALUE);
+
+    public static int generateValidUnprivilegedPort() {
+        return new Random().nextInt(Port.MAX_PORT_VALUE - 
PRIVILEGED_PORT_BOUND) + PRIVILEGED_PORT_BOUND;
+    }
+
+    public static void assertValid(int port) {
+        Preconditions.checkArgument(isValid(port), "Port should be between 1 
and 65535");
+    }
+
+    public static boolean isValid(int port) {
+        return VALID_PORT_RANGE.contains(port);
+    }
+
+    private final int value;
+
+    public Port(int value) {
+        assertValid(value);
+        this.value = value;
+    }
+
+    public int getValue() {
+        return value;
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java
----------------------------------------------------------------------
diff --git 
a/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java 
b/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java
new file mode 100644
index 0000000..2f13e60
--- /dev/null
+++ 
b/server/container/util-java8/src/test/java/org/apache/james/util/PortTest.java
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+public class PortTest {
+    @Test
+    public void assertValidShouldThrowOnNegativePort() {
+        assertThatThrownBy(() -> Port.assertValid(-1))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void assertValidShouldThrowOnZeroPort() {
+        assertThatThrownBy(() -> Port.assertValid(0))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void assertValidShouldAcceptOne() {
+        Port.assertValid(1);
+    }
+
+    @Test
+    public void assertValidShouldAcceptMaxValue() {
+        Port.assertValid(Port.MAX_PORT_VALUE);
+    }
+
+    @Test
+    public void assertValidShouldThrowOnTooBigValue() {
+        assertThatThrownBy(() -> Port.assertValid(Port.MAX_PORT_VALUE + 1))
+            .isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void isValidShouldReturnFalseWhenNegative() {
+        assertThat(Port.isValid(-1))
+            .isFalse();
+    }
+
+    @Test
+    public void isValidShouldReturnFalseWhenZero() {
+        assertThat(Port.isValid(0))
+            .isFalse();
+    }
+
+    @Test
+    public void isValidShouldReturnTrueWhenOne() {
+        assertThat(Port.isValid(1))
+            .isTrue();
+    }
+
+    @Test
+    public void isValidShouldReturnTrueWhenMaxValue() {
+        assertThat(Port.isValid(Port.MAX_PORT_VALUE))
+            .isTrue();
+    }
+
+    @Test
+    public void isValidShouldReturnFalseWhenAboveMaxValue() {
+        assertThat(Port.isValid(Port.MAX_PORT_VALUE + 1))
+            .isFalse();
+    }
+
+    @Test
+    public void generateValidUnprivilegedPortShouldReturnAValidPort() {
+        assertThat(Port.generateValidUnprivilegedPort())
+            .isStrictlyBetween(Port.PRIVILEGED_PORT_BOUND, 
Port.MAX_PORT_VALUE);
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SpamAssassin.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SpamAssassin.java
 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SpamAssassin.java
index 1b3de01..5d01cd5 100644
--- 
a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SpamAssassin.java
+++ 
b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/SpamAssassin.java
@@ -25,6 +25,7 @@ import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
 import org.apache.james.transport.mailets.managesieve.ManageSieveMailet;
+import org.apache.james.util.Port;
 import org.apache.james.util.scanner.SpamAssassinInvoker;
 import org.apache.mailet.Experimental;
 import org.apache.mailet.Mail;
@@ -67,7 +68,6 @@ public class SpamAssassin extends GenericMailet {
     public static final String SPAMD_PORT = "spamdPort";
     public static final String DEFAULT_HOST = "127.0.0.1";
     public static final int DEFAULT_PORT = 783;
-    public static final int MAX_AVAILABLE_PORT = 65535;
 
     private String spamdHost;
     private int spamdPort;
@@ -81,9 +81,7 @@ public class SpamAssassin extends GenericMailet {
             .orElse(DEFAULT_HOST);
 
         spamdPort = 
MailetUtil.getInitParameterAsStrictlyPositiveInteger(getInitParameter(SPAMD_PORT),
 DEFAULT_PORT);
-        if (spamdPort > MAX_AVAILABLE_PORT) {
-            throw new MessagingException("Please configure a valid port. Not 
valid: " + spamdPort);
-        }
+        Port.assertValid(spamdPort);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
----------------------------------------------------------------------
diff --git 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
index 71831d9..875a4f9 100644
--- 
a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
+++ 
b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/SpamAssassinTest.java
@@ -24,7 +24,9 @@ import static 
org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import javax.mail.MessagingException;
 
+import org.apache.james.smtpserver.mock.util.MockSpamd;
 import org.apache.james.smtpserver.mock.util.MockSpamdTestRule;
+import org.apache.james.util.Port;
 import org.apache.james.util.scanner.SpamAssassinInvoker;
 import org.apache.mailet.Mail;
 import org.apache.mailet.base.test.FakeMail;
@@ -117,8 +119,8 @@ public class SpamAssassinTest {
         assertThatThrownBy(() -> mailet.init(FakeMailetConfig.builder()
             .mailetName("SpamAssassin")
             .setProperty(SpamAssassin.SPAMD_PORT,
-                String.valueOf(SpamAssassin.MAX_AVAILABLE_PORT + 1))
-            .build())).isInstanceOf(MessagingException.class);
+                String.valueOf(Port.MAX_PORT_VALUE + 1))
+            .build())).isInstanceOf(IllegalArgumentException.class);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
 
b/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
index e475373..e64fabd 100644
--- 
a/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
@@ -85,7 +85,7 @@ public class CassandraMigrationRoutesTest {
             .setContentType(ContentType.JSON)
             .setAccept(ContentType.JSON)
             .setBasePath(CassandraMigrationRoutes.VERSION_BASE)
-            .setPort(webAdminServer.getPort().toInt())
+            .setPort(webAdminServer.getPort().get().getValue())
             
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
             .build();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPort.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPort.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPort.java
deleted file mode 100644
index 7ada8a1..0000000
--- 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPort.java
+++ /dev/null
@@ -1,54 +0,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.                                           *
- ****************************************************************/
-
-package org.apache.james.webadmin;
-
-import java.util.Objects;
-
-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;
-    }
-
-    @Override
-    public final boolean equals(Object o) {
-        if (o instanceof FixedPort) {
-            FixedPort that = (FixedPort) o;
-
-            return Objects.equals(this.port, that.port);
-        }
-        return false;
-    }
-
-    @Override
-    public final int hashCode() {
-        return Objects.hash(port);
-    }
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPortSupplier.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPortSupplier.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPortSupplier.java
new file mode 100644
index 0000000..de35bcb
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/FixedPortSupplier.java
@@ -0,0 +1,53 @@
+/****************************************************************
+ * 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.util.Objects;
+
+import org.apache.james.util.Port;
+
+public class FixedPortSupplier implements PortSupplier {
+
+    private final Port port;
+
+    public FixedPortSupplier(int port) {
+        this.port = new Port(port);
+    }
+
+    @Override
+    public Port get() {
+        return port;
+    }
+
+    @Override
+    public final boolean equals(Object o) {
+        if (o instanceof FixedPortSupplier) {
+            FixedPortSupplier that = (FixedPortSupplier) o;
+
+            return Objects.equals(this.port, that.port);
+        }
+        return false;
+    }
+
+    @Override
+    public final int hashCode() {
+        return Objects.hash(port);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Port.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Port.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Port.java
deleted file mode 100644
index ff7a50b..0000000
--- 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Port.java
+++ /dev/null
@@ -1,26 +0,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.                                           *
- ****************************************************************/
-
-package org.apache.james.webadmin;
-
-public interface Port {
-
-    int toInt();
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/PortSupplier.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/PortSupplier.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/PortSupplier.java
new file mode 100644
index 0000000..b9a8afe
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/PortSupplier.java
@@ -0,0 +1,28 @@
+/****************************************************************
+ * 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.util.function.Supplier;
+
+import org.apache.james.util.Port;
+
+public interface PortSupplier extends Supplier<Port> {
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPort.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPort.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPort.java
deleted file mode 100644
index 2b3ab5d..0000000
--- 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPort.java
+++ /dev/null
@@ -1,50 +0,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.                                           *
- ****************************************************************/
-
-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/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java
new file mode 100644
index 0000000..6f21591
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/RandomPortSupplier.java
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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 org.apache.james.util.Port;
+
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+import com.google.common.base.Throwables;
+
+public class RandomPortSupplier implements PortSupplier {
+
+    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 RandomPortSupplier() {
+        portSupplier = Suppliers.memoize(RandomPortSupplier::findFreePort);
+    }
+
+    @Override
+    public Port get() {
+        return new Port(portSupplier.get());
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java
index 8db2976..45d0e18 100644
--- 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java
+++ 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminConfiguration.java
@@ -42,7 +42,7 @@ public class WebAdminConfiguration {
 
     public static class Builder {
         private Optional<Boolean> enabled = Optional.empty();
-        private Optional<Port> port = Optional.empty();
+        private Optional<PortSupplier> port = Optional.empty();
         private Optional<Boolean> enableCORS = Optional.empty();
         private Optional<TlsConfiguration> tlsConfiguration = Optional.empty();
         private Optional<String> urlCORSOrigin = Optional.empty();
@@ -58,8 +58,8 @@ public class WebAdminConfiguration {
             return this;
         }
 
-        public Builder port(Port port) {
-            this.port = Optional.of(port);
+        public Builder port(PortSupplier portSupplier) {
+            this.port = Optional.of(portSupplier);
             return this;
         }
 
@@ -111,14 +111,14 @@ public class WebAdminConfiguration {
     }
 
     private final boolean enabled;
-    private final Optional<Port> port;
+    private final Optional<PortSupplier> port;
     private final Optional<TlsConfiguration> tlsConfiguration;
     private final boolean enableCORS;
     private final String urlCORSOrigin;
     private final String host;
 
     @VisibleForTesting
-    WebAdminConfiguration(boolean enabled, Optional<Port> port, 
Optional<TlsConfiguration> tlsConfiguration,
+    WebAdminConfiguration(boolean enabled, Optional<PortSupplier> port, 
Optional<TlsConfiguration> tlsConfiguration,
                           boolean enableCORS, String urlCORSOrigin, String 
host) {
         this.enabled = enabled;
         this.port = port;
@@ -136,7 +136,7 @@ public class WebAdminConfiguration {
         return urlCORSOrigin;
     }
 
-    public Port getPort() {
+    public PortSupplier getPort() {
         return port.orElseThrow(() -> new IllegalStateException("No port was 
specified"));
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
index fe5d22a..e544022 100644
--- 
a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
+++ 
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/WebAdminServer.java
@@ -67,7 +67,7 @@ public class WebAdminServer implements Configurable {
     @Override
     public void configure(HierarchicalConfiguration config) throws 
ConfigurationException {
         if (configuration.isEnabled()) {
-            service.port(configuration.getPort().toInt());
+            service.port(configuration.getPort().get().getValue());
             configureHTTPS();
             configureCORS();
             configureMetrics();
@@ -123,7 +123,7 @@ public class WebAdminServer implements Configurable {
         service.awaitInitialization();
     }
 
-    public Port getPort() {
+    public PortSupplier getPort() {
         return configuration.getPort();
     }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortSupplierTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortSupplierTest.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortSupplierTest.java
new file mode 100644
index 0000000..304295d
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortSupplierTest.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * 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;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+public class FixedPortSupplierTest {
+
+    @Test
+    public void toIntShouldThrowOnNegativePort() {
+        assertThatThrownBy(() -> new 
FixedPortSupplier(-1)).isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void toIntShouldThrowOnNullPort() {
+        assertThatThrownBy(() -> new 
FixedPortSupplier(0)).isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void toIntShouldThrowOnTooBigNumbers() {
+        assertThatThrownBy(() -> new 
FixedPortSupplier(65536)).isInstanceOf(IllegalArgumentException.class);
+    }
+
+    @Test
+    public void toIntShouldReturnedDesiredPort() {
+        int expectedPort = 452;
+        assertThat(new 
FixedPortSupplier(expectedPort).get().getValue()).isEqualTo(expectedPort);
+    }
+
+    @Test
+    public void shouldMatchBeanContract() {
+        EqualsVerifier.forClass(FixedPortSupplier.class).verify();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortTest.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortTest.java
deleted file mode 100644
index 2712daa..0000000
--- 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/FixedPortTest.java
+++ /dev/null
@@ -1,57 +0,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.                                           *
- ****************************************************************/
-
-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;
-
-import nl.jqno.equalsverifier.EqualsVerifier;
-
-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);
-    }
-
-    @Test
-    public void shouldMatchBeanContract() {
-        EqualsVerifier.forClass(FixedPort.class).verify();
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortSupplierTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortSupplierTest.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortSupplierTest.java
new file mode 100644
index 0000000..de821f1
--- /dev/null
+++ 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortSupplierTest.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 RandomPortSupplierTest {
+
+    @Test
+    public void toIntShouldReturnTwoTimeTheSameResult() {
+        RandomPortSupplier testee = new RandomPortSupplier();
+        assertThat(testee.get().getValue()).isEqualTo(testee.get().getValue());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortTest.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortTest.java
deleted file mode 100644
index 07d1996..0000000
--- 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/RandomPortTest.java
+++ /dev/null
@@ -1,34 +0,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.                                           *
- ****************************************************************/
-
-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());
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java
index ab0649f..8fdf930 100644
--- 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java
+++ 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminConfigurationTest.java
@@ -29,7 +29,7 @@ import nl.jqno.equalsverifier.EqualsVerifier;
 
 public class WebAdminConfigurationTest {
 
-    public static final FixedPort PORT = new FixedPort(80);
+    public static final FixedPortSupplier PORT = new FixedPortSupplier(80);
 
     @Rule
     public ExpectedException expectedException = ExpectedException.none();

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
index 3ff8c02..89a5310 100644
--- 
a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
+++ 
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
@@ -31,7 +31,7 @@ public class WebAdminUtils {
     public static WebAdminConfiguration webAdminConfigurationForTesting() {
         return WebAdminConfiguration.builder()
             .enabled()
-            .port(new RandomPort())
+            .port(new RandomPortSupplier())
             .build();
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
index 1de056d..cfa3875 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/DomainsRoutesTest.java
@@ -74,7 +74,7 @@ public class DomainsRoutesTest {
                        .setContentType(ContentType.JSON)
                        .setAccept(ContentType.JSON)
                        
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-                       .setPort(webAdminServer.getPort().toInt())
+                       .setPort(webAdminServer.getPort().get().getValue())
                        .setBasePath(DomainsRoutes.DOMAINS)
                        .build();
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/GroupsRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/GroupsRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/GroupsRoutesTest.java
index a0eae10..ccbe64a 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/GroupsRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/GroupsRoutesTest.java
@@ -90,7 +90,7 @@ public class GroupsRoutesTest {
             .setContentType(ContentType.JSON)
             .setAccept(ContentType.JSON)
             
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-            .setPort(webAdminServer.getPort().toInt())
+            .setPort(webAdminServer.getPort().get().getValue())
             .setBasePath(GroupsRoutes.ROOT_PATH)
             .log(LogDetail.ALL)
             .build();

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsersRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsersRoutesTest.java
 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsersRoutesTest.java
index c2874db..bcc2d1f 100644
--- 
a/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsersRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-data/src/test/java/org/apache/james/webadmin/routes/UsersRoutesTest.java
@@ -76,7 +76,7 @@ public class UsersRoutesTest {
                        .setContentType(ContentType.JSON)
                        .setAccept(ContentType.JSON)
                        
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(StandardCharsets.UTF_8)))
-                       .setPort(webAdminServer.getPort().toInt())
+                       .setPort(webAdminServer.getPort().get().getValue())
                        .setBasePath(UserRoutes.USERS)
                        .build();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
 
b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
index 67e8fd5..24a84cb 100644
--- 
a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/GlobalQuotaRoutesTest.java
@@ -62,7 +62,7 @@ public class GlobalQuotaRoutesTest {
             .setContentType(ContentType.JSON)
             .setAccept(ContentType.JSON)
             
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
-            .setPort(webAdminServer.getPort().toInt())
+            .setPort(webAdminServer.getPort().get().getValue())
             .build();
     }
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserMailboxesRoutesTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserMailboxesRoutesTest.java
 
b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserMailboxesRoutesTest.java
index 5d34e26..46c493f 100644
--- 
a/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserMailboxesRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-mailbox/src/test/java/org/apache/james/webadmin/routes/UserMailboxesRoutesTest.java
@@ -92,7 +92,7 @@ public class UserMailboxesRoutesTest {
             .setContentType(ContentType.JSON)
             .setAccept(ContentType.JSON)
             .setBasePath(USERS_BASE + SEPARATOR + USERNAME + SEPARATOR + 
UserMailboxesRoutes.MAILBOXES)
-            .setPort(webAdminServer.getPort().toInt())
+            .setPort(webAdminServer.getPort().get().getValue())
             
.setConfig(newConfig().encoderConfig(encoderConfig().defaultContentCharset(Charsets.UTF_8)))
             .build();
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/27b89085/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
 
b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
index 7bdec19..730caa6 100644
--- 
a/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
+++ 
b/server/protocols/webadmin/webadmin-swagger/src/main/java/org/apache/james/webadmin/swagger/SwaggerParser.java
@@ -22,6 +22,7 @@ package org.apache.james.webadmin.swagger;
 import javax.inject.Inject;
 
 import org.apache.james.webadmin.WebAdminConfiguration;
+import org.reflections.Reflections;
 
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -32,7 +33,6 @@ import io.swagger.annotations.SwaggerDefinition;
 import io.swagger.jaxrs.Reader;
 import io.swagger.jaxrs.config.BeanConfig;
 import io.swagger.models.Swagger;
-import org.reflections.Reflections;
 
 @SwaggerDefinition
 public class SwaggerParser {
@@ -68,7 +68,7 @@ public class SwaggerParser {
                beanConfig.setVersion(API_DOC_VERSION);
                beanConfig.setTitle(API_DOC_TITLE);
                beanConfig.setDescription(API_DOC_DESCRIPTION);
-               beanConfig.setHost(configuration.getHost() + 
HOST_PORT_SEPARATOR + configuration.getPort().toInt());
+               beanConfig.setHost(configuration.getHost() + 
HOST_PORT_SEPARATOR + configuration.getPort().get().getValue());
                beanConfig.setSchemes(SCHEMES);
                beanConfig.setScan(true);
                beanConfig.scanAndRead();


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

Reply via email to