Skip to content

Commit 99c3712

Browse files
committed
[bugfix] Make local loop detection more reliable
1 parent 263271d commit 99c3712

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

exist-core/src/main/java/org/exist/management/client/JMXServlet.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.nio.file.Path;
3333
import java.nio.file.Paths;
3434
import java.util.*;
35+
import java.util.function.Predicate;
3536
import javax.management.*;
3637
import jakarta.servlet.ServletConfig;
3738
import jakarta.servlet.ServletException;
@@ -92,7 +93,7 @@ public class JMXServlet extends HttpServlet {
9293

9394
private Path dataDir;
9495
private Path tokenFile;
95-
96+
9697
@Override
9798
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
9899

@@ -116,7 +117,7 @@ protected void doGet(final HttpServletRequest request, final HttpServletResponse
116117
}
117118

118119
private void writeXmlData(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
119-
Element root;
120+
final Element root;
120121

121122
final String operation = request.getParameter("operation");
122123
if ("ping".equals(operation)) {
@@ -215,23 +216,21 @@ public void init(final ServletConfig config) throws ServletException {
215216
void registerLocalHostAddresses() {
216217

217218
try {
218-
for (final NetworkInterface networkInterface : NetworkInterface.networkInterfaces().toList()) {
219-
if (networkInterface.isLoopback()) {
220-
final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
221-
while (inetAddresses.hasMoreElements()) {
222-
final String hostAddress = inetAddresses.nextElement().getHostAddress();
223-
if (hostAddress != null) {
224-
if (hostAddress.contains("%")) {
225-
final int position = hostAddress.indexOf("%");
226-
localhostAddresses.add(hostAddress.substring(0, position));
227-
} else {
228-
localhostAddresses.add(hostAddress);
229-
}
230-
}
231-
}
232-
219+
final Predicate<NetworkInterface> isLoopback = networkInterface -> {
220+
try {
221+
return networkInterface.isLoopback();
222+
} catch (final SocketException e) {
223+
LOG.error("Unable to determine if {} is a loopback interface", e.getMessage(), e);
224+
return false;
233225
}
234-
}
226+
};
227+
228+
NetworkInterface.networkInterfaces()
229+
.filter(isLoopback)
230+
.flatMap(NetworkInterface::inetAddresses)
231+
.map(InetAddress::getHostAddress)
232+
.map(a -> a.replaceFirst("%.+", ""))
233+
.forEach(localhostAddresses::add);
235234

236235
} catch (final SocketException e) {
237236
LOG.error("Unable to determine localhost loopback addresses: {} ", e.getMessage(), e);

0 commit comments

Comments
 (0)