Skip to content

Commit f8347eb

Browse files
Merge pull request #842 from cortlepp/fix/AXIS2-5858
Fix AXIS2-5858
2 parents 759db92 + 7c2181f commit f8347eb

File tree

3 files changed

+120
-52
lines changed

3 files changed

+120
-52
lines changed

modules/kernel/src/org/apache/axis2/util/Utils.java

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,23 @@
6161
import java.lang.reflect.InvocationTargetException;
6262
import java.lang.reflect.Method;
6363
import java.lang.reflect.Modifier;
64+
import java.net.Inet4Address;
65+
import java.net.Inet6Address;
6466
import java.net.InetAddress;
6567
import java.net.NetworkInterface;
6668
import java.net.SocketException;
69+
import java.net.UnknownHostException;
6770
import java.security.AccessController;
6871
import java.security.PrivilegedAction;
6972
import java.security.PrivilegedExceptionAction;
7073
import java.util.Enumeration;
7174
import java.util.HashMap;
7275
import java.util.Iterator;
7376
import java.util.Map;
77+
import java.util.List;
78+
import java.util.ArrayList;
79+
import java.util.Comparator;
80+
import java.util.function.Function;
7481

7582
public class Utils {
7683
private static final Log log = LogFactory.getLog(Utils.class);
@@ -569,6 +576,93 @@ public static int getMtomThreshold(MessageContext msgCtxt){
569576
}
570577
return threshold;
571578
}
579+
/**
580+
* Returns all <code>InetAddress</code> objects encapsulating what are most likely the machine's
581+
* LAN IP addresses. This method was copied from apache-commons-jcs HostNameUtil.java.
582+
* <p>
583+
* This method will scan all IP addresses on all network interfaces on the host machine to
584+
* determine the IP addresses most likely to be the machine's LAN addresses.
585+
* <p>
586+
* @return List<InetAddress>
587+
* @throws IllegalStateException If the LAN address of the machine cannot be found.
588+
*/
589+
public static List<InetAddress> getLocalHostLANAddresses() throws SocketException
590+
{
591+
final List<InetAddress> addresses = new ArrayList<>();
592+
593+
try
594+
{
595+
InetAddress candidateAddress = null;
596+
// Iterate all NICs (network interface cards)...
597+
final Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
598+
while ( ifaces.hasMoreElements() )
599+
{
600+
final NetworkInterface iface = ifaces.nextElement();
601+
602+
// Skip loopback interfaces
603+
if (iface.isLoopback() || !iface.isUp())
604+
{
605+
continue;
606+
}
607+
608+
// Iterate all IP addresses assigned to each card...
609+
for ( final Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); )
610+
{
611+
final InetAddress inetAddr = inetAddrs.nextElement();
612+
if ( !inetAddr.isLoopbackAddress() )
613+
{
614+
if (!inetAddr.isLinkLocalAddress())
615+
{
616+
if (inetAddr instanceof Inet6Address) {
617+
// we ignore the site-local attribute for IPv6 because
618+
// it has been deprecated, see https://www.ietf.org/rfc/rfc3879.txt
619+
addresses.add(inetAddr);
620+
} else if (inetAddr instanceof Inet4Address && inetAddr.isSiteLocalAddress()) {
621+
// check site-local
622+
addresses.add(inetAddr);
623+
}
624+
}
625+
626+
if ( candidateAddress == null )
627+
{
628+
// Found non-loopback address, but not necessarily site-local.
629+
// Store it as a candidate to be returned if site-local address is not subsequently found...
630+
candidateAddress = inetAddr;
631+
// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
632+
// only the first. For subsequent iterations, candidate will be non-null.
633+
}
634+
}
635+
}
636+
}
637+
if (candidateAddress != null && addresses.isEmpty())
638+
{
639+
// We did not find a site-local address, but we found some other non-loopback address.
640+
// Server might have a non-site-local address assigned to its NIC (or it might be running
641+
// IPv6 which deprecates the "site-local" concept).
642+
addresses.add(candidateAddress);
643+
}
644+
// At this point, we did not find a non-loopback address.
645+
// Fall back to returning whatever InetAddress.getLocalHost() returns...
646+
if (addresses.isEmpty())
647+
{
648+
final InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
649+
if ( jdkSuppliedAddress == null )
650+
{
651+
throw new IllegalStateException( "The JDK InetAddress.getLocalHost() method unexpectedly returned null." );
652+
}
653+
addresses.add(jdkSuppliedAddress);
654+
}
655+
}
656+
catch (UnknownHostException e )
657+
{
658+
var throwable = new SocketException("Failed to determine LAN address");
659+
throwable.initCause(e);
660+
throw throwable;
661+
}
662+
663+
return addresses;
664+
}
665+
572666
/**
573667
* Returns the ip address to be used for the replyto epr
574668
* CAUTION:
@@ -582,25 +676,13 @@ public static int getMtomThreshold(MessageContext msgCtxt){
582676
* - Obtain the ip to be used here from the Call API
583677
*
584678
* @return Returns String.
585-
* @throws java.net.SocketException
586679
*/
587680
public static String getIpAddress() throws SocketException {
588-
Enumeration e = NetworkInterface.getNetworkInterfaces();
589-
String address = "127.0.0.1";
590-
591-
while (e.hasMoreElements()) {
592-
NetworkInterface netface = (NetworkInterface) e.nextElement();
593-
Enumeration addresses = netface.getInetAddresses();
594-
595-
while (addresses.hasMoreElements()) {
596-
InetAddress ip = (InetAddress) addresses.nextElement();
597-
if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
598-
return ip.getHostAddress();
599-
}
600-
}
601-
}
602-
603-
return address;
681+
//prefer ipv4 for backwards compatibility, we used to only consider ipv4 addresses
682+
Function<InetAddress,Integer> preferIpv4 = (i) -> i instanceof Inet4Address ? 1 : 0;
683+
return getLocalHostLANAddresses().stream()
684+
.max(Comparator.comparing(preferIpv4))
685+
.map(InetAddress::getHostAddress).orElse("127.0.0.1");
604686
}
605687

606688
/**
@@ -639,10 +721,6 @@ public static String getHostname(AxisConfiguration axisConfiguration) {
639721
return null;
640722
}
641723

642-
private static boolean isIP(String hostAddress) {
643-
return hostAddress.split("[.]").length == 4;
644-
}
645-
646724
/**
647725
* Get the scheme part from a URI (or URL).
648726
*

modules/kernel/src/org/apache/axis2/util/WSDLSerializationUtil.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.axiom.om.OMFactory;
2424
import org.apache.axiom.om.OMNamespace;
2525
import org.apache.axiom.om.OMNode;
26-
import org.apache.axis2.AxisFault;
2726
import org.apache.axis2.addressing.AddressingConstants;
2827
import org.apache.axis2.description.*;
2928
import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
@@ -36,12 +35,15 @@
3635
import org.apache.neethi.PolicyReference;
3736

3837
import javax.xml.namespace.QName;
38+
import java.net.URI;
39+
import java.net.URISyntaxException;
3940
import java.util.*;
4041

4142
/**
4243
* Helps the AxisService to WSDL process
4344
*/
4445
public class WSDLSerializationUtil {
46+
private static final OnDemandLogger log = new OnDemandLogger(WSDLSerializationUtil.class);
4547

4648
public static final String CDATA_START = "<![CDATA[";
4749
public static final String CDATA_START_REGEX = "<!\\[CDATA\\[";
@@ -422,29 +424,31 @@ private static AxisService getAxisService(AxisDescription description) {
422424
}
423425
}
424426

425-
public static String extractHostIP(String serviceURL){
426-
427+
public static String extractHostIP(String serviceURL) {
428+
try {
429+
return new URI(serviceURL).getHost();
430+
} catch (URISyntaxException | NullPointerException e) {
431+
log.debug("encountered invalid URI when trying to extract host ip, will try to parse manually now", e);
427432
String ip = null;
428-
433+
429434
if (serviceURL != null) {
430-
435+
431436
int ipindex = serviceURL.indexOf("//");
432-
437+
433438
if (ipindex >= 0) {
434-
ip = serviceURL.substring(ipindex + 2, serviceURL.length());
439+
ip = serviceURL.substring(ipindex + 2);
435440
int seperatorIndex = ip.indexOf(":");
436441
int slashIndex = ip.indexOf("/");
437-
442+
438443
if (seperatorIndex >= 0) {
439444
ip = ip.substring(0, seperatorIndex);
440445
} else {
441446
ip = ip.substring(0, slashIndex);
442-
}
447+
}
443448
}
444449
}
445-
446-
return ip;
447-
}
448450

449-
451+
return ip;
452+
}
453+
}
450454
}

modules/transport/http/src/main/java/org/apache/axis2/transport/http/ListingAgent.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
import org.apache.axis2.description.Parameter;
2828
import org.apache.axis2.description.PolicyInclude;
2929
import org.apache.axis2.transport.http.server.HttpUtils;
30-
import org.apache.axis2.util.ExternalPolicySerializer;
31-
import org.apache.axis2.util.IOUtils;
32-
import org.apache.axis2.util.JavaUtils;
33-
import org.apache.axis2.util.OnDemandLogger;
30+
import org.apache.axis2.util.*;
3431
import org.apache.neethi.Policy;
3532
import org.apache.neethi.PolicyComponent;
3633
import org.apache.neethi.PolicyRegistry;
@@ -46,11 +43,12 @@
4643
import java.io.IOException;
4744
import java.io.InputStream;
4845
import java.io.OutputStream;
46+
import java.net.URI;
47+
import java.net.URISyntaxException;
4948
import java.util.Collection;
5049
import java.util.Enumeration;
5150
import java.util.HashMap;
5251
import java.util.Iterator;
53-
import java.util.List;
5452

5553
public class ListingAgent extends AbstractAgent {
5654

@@ -99,19 +97,7 @@ protected void processIndex(HttpServletRequest httpServletRequest,
9997
}
10098

10199
private String extractHost(String filePart) {
102-
int ipindex = filePart.indexOf("//");
103-
String ip = null;
104-
if (ipindex >= 0) {
105-
ip = filePart.substring(ipindex + 2, filePart.length());
106-
int seperatorIndex = ip.indexOf(":");
107-
int slashIndex = ip.indexOf("/");
108-
if (seperatorIndex >= 0) {
109-
ip = ip.substring(0, seperatorIndex);
110-
} else {
111-
ip = ip.substring(0, slashIndex);
112-
}
113-
}
114-
return ip;
100+
return WSDLSerializationUtil.extractHostIP(filePart);
115101
}
116102

117103
public void processExplicitSchemaAndWSDL(HttpServletRequest req,

0 commit comments

Comments
 (0)