Hi,
I had some situations when working directly with http-client or JMeter in
restrictive environments, where I wasn't able to edit the local /etc/hosts
file for resolving some custom hostnames. Based on the HttpUnit solution
[1], I was thinking that would be great if httpclient can lookup a hostname
in a custom DNS Resolver, editable or programable in the Java environment,
and fall-back to the default system address resolver if this isn't found in
the first place. For example the above snippet will resolve the "
www.alin.com" hostname to the one of the Google IPs:
interface DnsResolver{
InetAddress[] resolve(String hostname);
}
class InMemoryDnsResolverImpl implements DnsResolver{
public InetAddress[] resolve(final String hostname){
try {
if ("www.alin.com".equals(hostname)) {
String ip = "74.125.39.105";
String[] ipParts = ip.split("\\.");
byte[] byteIpAddress = new byte[4];
for (int i = 0; i < 4; i++) {
byteIpAddress[i] = Byte.parseByte(ipParts[i]);
}
return new InetAddress[] { InetAddress
.getByAddress(byteIpAddress) };
} else {
return null;
}
} catch (UnknownHostException e) {
// log exception
return null;
}
}
}
public class DefaultClientConnectionOperator implements
ClientConnectionOperator {
....
private DnsResolver dnsResolver;
...
protected InetAddress[] resolveHostname(final String host) throws
UnknownHostException {
InetAddress[] resolvedAddress = null;
if(dnResolver != null){
resolvedAddress = dnResolver.resolve(host);
}
if(resolverAddress==null){
resolvedAddress = InetAddress.getAllByName(host);
}
return resolvedAddress;
}
}
This functionality can be extended to get the IP addresses from a
properties file through an PropertyFileDnsResolverImpl and so on. What do
you think?
Regards,
Alin
[1] http://httpunit.sourceforge.net/