This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit b3c7d91503d45b0d8bc1bff66f01300d33fcf5a6 Author: Andy Seaborne <[email protected]> AuthorDate: Thu Jan 16 22:17:59 2025 +0000 GH-2963: Cope with IPv6 address for the local machine --- .../src/main/java/org/apache/jena/atlas/net/Host.java | 19 ++++++++++++++----- .../test/java/org/apache/jena/atlas/net/TestHost.java | 2 +- .../apache/jena/fuseki/mod/shiro/TestModShiro.java | 10 ++++++++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/jena-base/src/main/java/org/apache/jena/atlas/net/Host.java b/jena-base/src/main/java/org/apache/jena/atlas/net/Host.java index 12449dbd71..30914d0ca4 100644 --- a/jena-base/src/main/java/org/apache/jena/atlas/net/Host.java +++ b/jena-base/src/main/java/org/apache/jena/atlas/net/Host.java @@ -19,6 +19,7 @@ package org.apache.jena.atlas.net; import java.io.IOException; +import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; @@ -29,16 +30,24 @@ import org.apache.jena.atlas.io.IOX; public class Host { /** - * Returns a host address, for what is most likely the machine's LAN IP address - * (not a loopback address, 127.0.0.x or ::1). If there is no other choice, it returns - * <code>a local host address</code>. + * Returns a host address in a form suitable for an IRI authority host. + * This is not a loopback address, 127.0.0.x or ::1. */ - public static String getHostAddress() { + public static String getHostAddressForIRI() { try { InetAddress addr = getLocalHostLANAddress$(); if ( addr == null ) return null; - return addr.getHostAddress(); + String hostAddress = addr.getHostAddress(); + if ( addr instanceof Inet6Address ) { + // Remove zoneId + hostAddress = hostAddress.replaceFirst("%.*",""); + // If you do want the zone id, the % needs encoding. + // hostAddress = hostAddress.replaceFirst("%","%25"); + // Wrap in "[ ... ]" + hostAddress = "["+hostAddress+"]"; + } + return hostAddress; } catch ( UnknownHostException ex) { return null; } diff --git a/jena-base/src/test/java/org/apache/jena/atlas/net/TestHost.java b/jena-base/src/test/java/org/apache/jena/atlas/net/TestHost.java index 4cd94688d7..95f1714fdb 100644 --- a/jena-base/src/test/java/org/apache/jena/atlas/net/TestHost.java +++ b/jena-base/src/test/java/org/apache/jena/atlas/net/TestHost.java @@ -47,7 +47,7 @@ public class TestHost { // Assumes the machine has some kind of IP networking. try { // InetAddress.toString is "host/address" - String addr = Host.getHostAddress(); + String addr = Host.getHostAddressForIRI(); assertNotNull(addr); assertFalse(addr.contains("/")); } catch ( RuntimeIOException ex) { diff --git a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/mod/shiro/TestModShiro.java b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/mod/shiro/TestModShiro.java index 30b12be6ee..e0bfd9e47a 100644 --- a/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/mod/shiro/TestModShiro.java +++ b/jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/mod/shiro/TestModShiro.java @@ -51,7 +51,7 @@ import org.apache.jena.sparql.exec.http.GSP; import org.apache.jena.sparql.exec.http.QueryExecHTTP; public class TestModShiro { - static final String unlocal = Host.getHostAddress(); + static final String unlocal = determineUnlocal(); static final String localRE = Pattern.quote("localhost"); static { @@ -72,10 +72,16 @@ public class TestModShiro { FusekiServerCtl.clearUpSystemState(); } + private static String determineUnlocal() { + // Get a string for the host in a URL that names this machine but isn't localhost. + return Host.getHostAddressForIRI(); + } + private String unlocalhost(FusekiServer server, String dataset) { String local = server.datasetURL(dataset); - if ( unlocal != null ) + if ( unlocal != null ) { local = local.replaceFirst(localRE, unlocal); + } return local; }
