[NO ISSUE][HYR][NET] NetworkUtil enhhancements
Change-Id: I9c0d24100b653015233fc5a08554983d0a0185e1
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/6544
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Michael Blow <mblow@apache.org>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java
index 1b56440..1780d9c 100644
--- a/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-util/src/main/java/org/apache/hyracks/util/NetworkUtil.java
@@ -32,6 +32,7 @@
import javax.net.ssl.SSLEngine;
+import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpHost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.util.InetAddressUtils;
@@ -104,13 +105,30 @@
}
public static String defaultPort(String maybeHostPort, int defaultPort) {
- String encodedInput = encodeIPv6LiteralHost(maybeHostPort);
- int lastColon = encodedInput.lastIndexOf(':');
- int closeBracket = encodedInput.lastIndexOf(']');
- if (lastColon > 0 && lastColon > closeBracket) {
- return maybeHostPort;
- } else {
- return encodedInput + ":" + defaultPort;
+ Pair<String, Integer> decoded = extractHostPort(maybeHostPort, defaultPort);
+ return encodeIPv6LiteralHost(decoded.getLeft()) + ":" + decoded.getRight();
+ }
+
+ public static Pair<String, Integer> extractHostPort(String maybeHostPort, int defaultPort) {
+ try {
+ int lastColon = maybeHostPort.lastIndexOf(':');
+ if (lastColon == -1) {
+ return Pair.of(maybeHostPort, defaultPort);
+ }
+ int closeBracket = maybeHostPort.lastIndexOf(']');
+ if (closeBracket == -1) {
+ if (InetAddressUtils.isIPv6Address(maybeHostPort)) {
+ return Pair.of(maybeHostPort, defaultPort);
+ }
+ return Pair.of(maybeHostPort.substring(0, lastColon),
+ Integer.parseInt(maybeHostPort.substring(lastColon + 1)));
+ } else if (closeBracket < lastColon) {
+ return Pair.of(decodeIPv6LiteralHost(maybeHostPort.substring(0, lastColon)),
+ Integer.parseInt(maybeHostPort.substring(lastColon + 1)));
+ }
+ return Pair.of(decodeIPv6LiteralHost(maybeHostPort), defaultPort);
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
}
}