From ee797168051c4aa24aa9220016b644bca20f2496 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Wed, 08 Jun 2016 13:13:18 +0000
Subject: [PATCH] OPENDJ-2801 Extend HostPort.valueOf to allow a default port

---
 opendj-server-legacy/src/main/java/org/opends/server/util/ServerConstants.java   |    4 +
 opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java     |   72 +++++++++++++++++++++++
 opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java         |   40 +++++++++++--
 opendj-server-legacy/src/main/java/org/opends/server/core/CoreConfigManager.java |   10 +--
 4 files changed, 112 insertions(+), 14 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/core/CoreConfigManager.java b/opendj-server-legacy/src/main/java/org/opends/server/core/CoreConfigManager.java
index 67c38d5..6329e22 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/core/CoreConfigManager.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/core/CoreConfigManager.java
@@ -77,8 +77,7 @@
     globalConfig.addChangeListener(this);
 
 
-    // If there are any STMP servers specified, then make sure that if the value
-    // contains a colon that the portion after it is an integer between 1 and 65535.
+    // Validate any specified SMTP servers
     Set<String> smtpServers = globalConfig.getSMTPServer();
     if (smtpServers != null)
     {
@@ -86,8 +85,7 @@
       {
         try
         {
-          // validate provided string
-          HostPort.valueOf(server);
+          HostPort.valueOf(server, SMTP_DEFAULT_PORT);
         }
         catch (RuntimeException e)
         {
@@ -208,7 +206,7 @@
         final Properties properties = new Properties();
         try
         {
-          final HostPort hp = HostPort.valueOf(smtpServer);
+          final HostPort hp = HostPort.valueOf(smtpServer, SMTP_DEFAULT_PORT);
 
           properties.setProperty(SMTP_PROPERTY_HOST, hp.getHost());
           properties.setProperty(SMTP_PROPERTY_PORT,
@@ -313,7 +311,7 @@
         try
         {
           // validate provided string
-          HostPort.valueOf(server);
+          HostPort.valueOf(server, SMTP_DEFAULT_PORT);
         }
         catch (RuntimeException e)
         {
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java b/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
index 279996d..f5dbf92 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/types/HostPort.java
@@ -198,24 +198,42 @@
     this.port = normalizePort(port, host);
   }
 
-
-
   /**
-   * Creates a new {@code HostPort} object by parsing the supplied
-   * "hostName:port" String URL. This method also accepts IPV6 style
-   * "[hostAddress]:port" String URLs.
+   * Creates a new {@code HostPort} object by parsing the supplied "hostName:port" String URL.
+   * This method also accepts IPV6 style "[hostAddress]:port" String URLs.
    *
    * @param hostPort
    *          a String representing the URL made of a host and a port.
    * @return a new {@link HostPort} built from the supplied string.
    * @throws NumberFormatException
-   *           If the "port" in the supplied string cannot be converted to an
-   *           int
+   *           If the "port" in the supplied string cannot be converted to an int
    * @throws IllegalArgumentException
    *           if no port could be found in the supplied string, or if the port
    *           is not a valid port number
    */
   public static HostPort valueOf(String hostPort) throws NumberFormatException,
+          IllegalArgumentException
+  {
+    return HostPort.valueOf(hostPort, null);
+  }
+
+  /**
+   * Creates a new {@code HostPort} object by parsing the supplied "hostName:port" String URL.
+   * This method also accepts IPV6 style "[hostAddress]:port" String URLs. Values without ports
+   * are allowed if a default port is provided.
+   *
+   * @param hostPort
+   *          a String representing the URL made of a host and a port.
+   * @param defaultPort
+   *          if not {@code null} then a default port to use if none is present in the string.
+   * @return a new {@link HostPort} built from the supplied string.
+   * @throws NumberFormatException
+   *           If the "port" in the supplied string cannot be converted to an int
+   * @throws IllegalArgumentException
+   *           if no port could be found in the supplied string, or if the port
+   *           is not a valid port number
+   */
+  public static HostPort valueOf(String hostPort, Integer defaultPort) throws NumberFormatException,
       IllegalArgumentException
   {
     final int sepIndex = hostPort.lastIndexOf(':');
@@ -223,6 +241,10 @@
         && hostPort.charAt(hostPort.length() - 1) == ']')
         || sepIndex == -1)
     {
+      if (defaultPort != null)
+      {
+        return new HostPort(hostPort, defaultPort.intValue());
+      }
       throw new IllegalArgumentException(
           "Invalid host/port string: no network port was provided in '"
               + hostPort + "'");
@@ -236,6 +258,10 @@
     else if (hostPort.lastIndexOf(':', sepIndex - 1) != -1
         && (hostPort.charAt(0) != '[' || hostPort.charAt(sepIndex - 1) != ']'))
     {
+      if (defaultPort != null)
+      {
+        return new HostPort(hostPort, defaultPort.intValue());
+      }
       throw new IllegalArgumentException(
           "Invalid host/port string: Suspected IPv6 address provided in '"
               + hostPort + "'. The only allowed format for providing IPv6 "
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/ServerConstants.java b/opendj-server-legacy/src/main/java/org/opends/server/util/ServerConstants.java
index c5e6282..5139667 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/ServerConstants.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/ServerConstants.java
@@ -3003,6 +3003,10 @@
 
 
 
+  /** The default SMTP port to use */
+  public static final int SMTP_DEFAULT_PORT = 25;
+
+
   /**
    * The name of the JavaMail property that can be used to specify the address
    * of the SMTP server.
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java b/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
index dbdc9bb..e03df59 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/types/HostPortTest.java
@@ -11,7 +11,7 @@
  * Header, with the fields enclosed by brackets [] replaced by your own identifying
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
- * Copyright 2013-2014 ForgeRock AS.
+ * Copyright 2013-2016 ForgeRock AS.
  */
 package org.opends.server.types;
 
@@ -38,6 +38,26 @@
   }
 
   @Test
+  public void valueOfHostNameDefaultPort()
+  {
+    final String serverURL = "home";
+    final HostPort hp = HostPort.valueOf(serverURL, 1);
+    assertThat(hp.getHost()).isEqualTo("home");
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL + ":1");
+  }
+
+  @Test
+  public void valueOfHostNameOverridingDefaultPort()
+  {
+    final String serverURL = "home:1";
+    final HostPort hp = HostPort.valueOf(serverURL, 2);
+    assertThat(hp.getHost()).isEqualTo("home");
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL);
+  }
+
+  @Test
   public void valueOfIPv4()
   {
     final String serverURL = "192.168.1.1:1";
@@ -48,6 +68,56 @@
   }
 
   @Test
+  public void valueOfIPv4DefaultPort()
+  {
+    final String serverURL = "192.168.1.1";
+    final HostPort hp = HostPort.valueOf(serverURL, 1);
+    assertThat(hp.getHost()).isEqualTo("192.168.1.1");
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL + ":1");
+  }
+
+  @Test
+  public void valueOfIPv4OverridingDefaultPort()
+  {
+    final String serverURL = "192.168.1.1:1";
+    final HostPort hp = HostPort.valueOf(serverURL, 2);
+    assertThat(hp.getHost()).isEqualTo("192.168.1.1");
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL);
+  }
+
+  @Test
+  public void valueOfIPv6()
+  {
+    final String serverURL = "[" + IPV6_ADDRESS + "]:1";
+    final HostPort hp = HostPort.valueOf(serverURL);
+    assertThat(hp.getHost()).isEqualTo(IPV6_ADDRESS);
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL);
+  }
+
+  @Test
+  public void valueOfIPv6DefaultPort()
+  {
+    final String serverURL = "[" + IPV6_ADDRESS + "]";
+    final HostPort hp = HostPort.valueOf(serverURL, 1);
+    assertThat(hp.getHost()).isEqualTo(IPV6_ADDRESS);
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL + ":1");
+  }
+
+  @Test
+  public void valueOfIPv6OverridingDefaultPort()
+  {
+    final String serverURL = "[" + IPV6_ADDRESS + "]:1";
+    final HostPort hp = HostPort.valueOf(serverURL, 2);
+    assertThat(hp.getHost()).isEqualTo(IPV6_ADDRESS);
+    assertThat(hp.getPort()).isEqualTo(1);
+    assertThat(hp.toString()).isEqualTo(serverURL);
+  }
+
+  @Test
   public void undefinedHostPort()
   {
     final HostPort hp = new HostPort(null, 0);

--
Gitblit v1.10.0