[BAEL-6954] internet address resolution spi (#16245)
* [BAEL-6954] internet address resolution spi * [BAEL-6954] internet address resolution spi * [BAEL-6954] internet address resolution spi * [BAEL-6954] internet address resolution spi * [BAEL-6954] internet address resolution spi * [BAEL-6954] internet address resolution spi
This commit is contained in:
parent
d1e36a0b4e
commit
5d38f245cd
@ -0,0 +1,44 @@
|
||||
package com.baeldung.inetspi;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.baeldung.inetspi.providers.CustomAddressResolverImpl;
|
||||
|
||||
public class InetAddressSPI {
|
||||
public String usingGetByName(String host) throws UnknownHostException {
|
||||
InetAddress inetAddress = InetAddress.getByName(host);
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
|
||||
public String[] usingGetAllByName(String host) throws UnknownHostException {
|
||||
InetAddress[] inetAddresses = InetAddress.getAllByName(host);
|
||||
return Arrays.stream(inetAddresses).map(InetAddress::getHostAddress).toArray(String[]::new);
|
||||
}
|
||||
|
||||
public String usingGetByIp(byte[] ip) throws UnknownHostException {
|
||||
InetAddress inetAddress = InetAddress.getByAddress(ip);
|
||||
|
||||
return inetAddress.getHostName();
|
||||
}
|
||||
|
||||
public String usingGetByIpAndReturnsCannonName(byte[] ip) throws UnknownHostException {
|
||||
InetAddress inetAddress = InetAddress.getByAddress(ip);
|
||||
|
||||
return inetAddress.getCanonicalHostName();
|
||||
}
|
||||
|
||||
public String getHostUsingCustomImpl(byte[] ip) throws UnknownHostException {
|
||||
|
||||
CustomAddressResolverImpl imp = new CustomAddressResolverImpl();
|
||||
return imp.get(null).lookupByAddress(ip);
|
||||
}
|
||||
|
||||
public Stream<InetAddress> getIpUsingCustomImpl(String host) throws UnknownHostException {
|
||||
|
||||
CustomAddressResolverImpl imp = new CustomAddressResolverImpl();
|
||||
return imp.get(null).lookupByName(host, null);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.baeldung.inetspi;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Registry {
|
||||
private final Map<String, List<byte[]>> registry;
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Registry.class.getName());
|
||||
|
||||
public Registry() {
|
||||
registry = loadMapWithData();
|
||||
}
|
||||
|
||||
public Stream<InetAddress> getAddressesfromHost(String host) throws UnknownHostException {
|
||||
LOGGER.info("Performing Forward Lookup for HOST : " + host);
|
||||
if (!registry.containsKey(host)) {
|
||||
throw new UnknownHostException("Missing Host information in Resolver");
|
||||
}
|
||||
return registry.get(host)
|
||||
.stream()
|
||||
.map(add -> constructInetAddress(host, add))
|
||||
.filter(Objects::nonNull);
|
||||
}
|
||||
|
||||
public String getHostFromAddress(byte[] arr) throws UnknownHostException {
|
||||
LOGGER.info("Performing Reverse Lookup for Address : " + Arrays.toString(arr));
|
||||
for (Map.Entry<String, List<byte[]>> entry : registry.entrySet()) {
|
||||
if (entry.getValue()
|
||||
.stream()
|
||||
.anyMatch(ba -> Arrays.equals(ba, arr))) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
throw new UnknownHostException("Address Not Found");
|
||||
}
|
||||
|
||||
private Map<String, List<byte[]>> loadMapWithData() {
|
||||
return Map.of("baeldung-local.org", List.of(new byte[] { 1, 2, 3, 4 }));
|
||||
}
|
||||
|
||||
private static InetAddress constructInetAddress(String host, byte[] address) {
|
||||
try {
|
||||
return InetAddress.getByAddress(host, address);
|
||||
} catch (UnknownHostException unknownHostException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.inetspi.providers;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.spi.InetAddressResolver;
|
||||
import java.net.spi.InetAddressResolverProvider;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.baeldung.inetspi.Registry;
|
||||
|
||||
public class CustomAddressResolverImpl extends InetAddressResolverProvider {
|
||||
|
||||
private static Logger LOGGER = Logger.getLogger(CustomAddressResolverImpl.class.getName());
|
||||
|
||||
private static Registry registry = new Registry();
|
||||
|
||||
@Override
|
||||
public InetAddressResolver get(Configuration configuration) {
|
||||
LOGGER.info("Using Custom Address Resolver :: " + this.name());
|
||||
LOGGER.info("Registry initialised");
|
||||
return new InetAddressResolver() {
|
||||
@Override
|
||||
public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy) throws UnknownHostException {
|
||||
return registry.getAddressesfromHost(host);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookupByAddress(byte[] addr) throws UnknownHostException {
|
||||
return registry.getHostFromAddress(addr);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "CustomInternetAddressResolverImpl";
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.inetspi;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InetAddressSPIUnitTest {
|
||||
@Test
|
||||
public void givenInetAddress_whenUsingInetAddress_thenPerformResolution() throws UnknownHostException {
|
||||
InetAddressSPI spi = new InetAddressSPI();
|
||||
Assert.assertNotNull(spi.usingGetByName("www.google.com"));
|
||||
Assert.assertTrue(spi.usingGetAllByName("www.google.com").length > 1);
|
||||
Assert.assertNotNull(spi.usingGetByIp(InetAddress.getByName("www.google.com")
|
||||
.getAddress()));
|
||||
Assert.assertNotNull(spi.usingGetByIpAndReturnsCannonName(InetAddress.getByName("www.google.com")
|
||||
.getAddress()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomInetAddressImplementation_whenUsingInetAddress_thenPerformResolution() throws UnknownHostException {
|
||||
InetAddressSPI spi = new InetAddressSPI();
|
||||
Assert.assertEquals("baeldung-local.org", spi.getHostUsingCustomImpl(new byte[] { 1, 2, 3, 4 }));
|
||||
Stream<InetAddress> response = spi.getIpUsingCustomImpl("baeldung-local.org");
|
||||
Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.findFirst()
|
||||
.get()
|
||||
.getAddress());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user