public String getHost( String defaultHost ) { return getHostAddress( host, defaultHost ); }
public boolean matches( URI toMatch ) { boolean result = false; for ( int port = ports[0]; port <= ports[1]; port++ ) { if ( port == toMatch.getPort() ) { result = true; break; } } if ( host == null && toMatch.getHost() == null ) { return result; } else if ( host == null ) { return false; } // URI may contain IP, so make sure we check it too by converting ours, if necessary String toMatchHost = toMatch.getHost(); // this tries to match hostnames as they are at first, then tries to extract and match ip addresses of both return result && (host.equalsIgnoreCase( toMatchHost ) || getHost( null ).equalsIgnoreCase( getHostAddress( toMatchHost, toMatchHost ) )); }
@Test void testGetHostAddress() throws Exception { // Given String hostName = InetAddress.getLocalHost().getHostName(); // When & Then // should return default, when host is null assertThat( HostnamePort.getHostAddress( null, "default" ), equalTo( "default" ) ); // should return host ip address when host is known assertThat( HostnamePort.getHostAddress( hostName, "default" ), equalTo( hostName ) ); }
@Test void testGetHostAddressUnknown() { // Given String unknownHost = "unknownHost"; assertThrows( UnknownHostException.class, () -> InetAddress.getByName( unknownHost ) ); // should return hostname when it is unknown assertThat( HostnamePort.getHostAddress( unknownHost, "default" ), equalTo( unknownHost ) ); }
public String getHost( String defaultHost ) { return getHostAddress( host, defaultHost ); }
public boolean matches( URI toMatch ) { boolean result = false; for ( int port = ports[0]; port <= ports[1]; port++ ) { if ( port == toMatch.getPort() ) { result = true; break; } } if ( host == null && toMatch.getHost() == null ) { return result; } else if ( host == null ) { return false; } // URI may contain IP, so make sure we check it too by converting ours, if necessary String toMatchHost = toMatch.getHost(); // this tries to match hostnames as they are at first, then tries to extract and match ip addresses of both return result && (host.equalsIgnoreCase( toMatchHost ) || getHost( null ).equalsIgnoreCase( getHostAddress( toMatchHost, toMatchHost ) )); }