HTTPCLIENT-1123: Support for pluggable DNS resolvers (default resolver).

Contributed by Alin Vasile <alinachegalati at yahoo dot com>



git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1165863 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-09-06 21:27:48 +00:00
parent d65b529e08
commit 57ea057594
4 changed files with 67 additions and 14 deletions

View File

@ -105,7 +105,7 @@ public DefaultClientConnectionOperator(final SchemeRegistry schemes) {
throw new IllegalArgumentException("Scheme registry amy not be null"); throw new IllegalArgumentException("Scheme registry amy not be null");
} }
this.schemeRegistry = schemes; this.schemeRegistry = schemes;
this.dnsResolver = null; this.dnsResolver = new SystemDefaultDnsResolver();
} }
/** /**
@ -120,7 +120,11 @@ public DefaultClientConnectionOperator(final SchemeRegistry schemes) {
public DefaultClientConnectionOperator(final SchemeRegistry schemes,final DnsResolver dnsResolver) { public DefaultClientConnectionOperator(final SchemeRegistry schemes,final DnsResolver dnsResolver) {
if (schemes == null) { if (schemes == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Scheme registry amy not be null"); "Scheme registry may not be null");
}
if(dnsResolver == null){
throw new IllegalArgumentException("DNS resolver may not be null");
} }
this.schemeRegistry = schemes; this.schemeRegistry = schemes;
@ -256,24 +260,19 @@ protected void prepareSocket(
/** /**
* Resolves the given host name to an array of corresponding IP addresses, based on the * Resolves the given host name to an array of corresponding IP addresses, based on the
* configured name service on the system. * configured name service on the provided DNS resolver. If one wasn't provided, the system
* * configuration is used.
* If a custom DNS resolver is provided, the given host will be searched in
* it first. If the host is not configured, the default OS DNS-lookup
* mechanism is used.
* *
* @param host host name to resolve * @param host host name to resolve
* @return array of IP addresses * @return array of IP addresses
* @exception UnknownHostException if no IP address for the host could be determined. * @exception UnknownHostException if no IP address for the host could be determined.
* *
* @see {@link DnsResolver}
* @see {@link SystemDefaultDnsResolver}
* @since 4.1 * @since 4.1
*/ */
protected InetAddress[] resolveHostname(final String host) throws UnknownHostException { protected InetAddress[] resolveHostname(final String host) throws UnknownHostException {
if (dnsResolver != null) {
return dnsResolver.resolve(host); return dnsResolver.resolve(host);
} else {
return InetAddress.getAllByName(host);
}
} }
} }

View File

@ -101,7 +101,7 @@ public PoolingClientConnectionManager(
throw new IllegalArgumentException("Scheme registry may not be null"); throw new IllegalArgumentException("Scheme registry may not be null");
} }
this.schemeRegistry = schemeRegistry; this.schemeRegistry = schemeRegistry;
this.dnsResolver = null; this.dnsResolver = new SystemDefaultDnsResolver();
this.operator = createConnectionOperator(schemeRegistry); this.operator = createConnectionOperator(schemeRegistry);
this.pool = new HttpConnPool(this.log, 2, 20, timeToLive, tunit); this.pool = new HttpConnPool(this.log, 2, 20, timeToLive, tunit);
} }
@ -114,7 +114,7 @@ public PoolingClientConnectionManager(final SchemeRegistry schemeRegistry,
throw new IllegalArgumentException("Scheme registry may not be null"); throw new IllegalArgumentException("Scheme registry may not be null");
} }
this.schemeRegistry = schemeRegistry; this.schemeRegistry = schemeRegistry;
this.dnsResolver = dnsResolver; this.dnsResolver = new SystemDefaultDnsResolver();
this.operator = createConnectionOperator(schemeRegistry); this.operator = createConnectionOperator(schemeRegistry);
this.pool = new HttpConnPool(this.log, 2, 20, timeToLive, tunit); this.pool = new HttpConnPool(this.log, 2, 20, timeToLive, tunit);
} }

View File

@ -0,0 +1,47 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.http.conn.DnsResolver;
/**
* DNS resolver that uses the default OS implementation for resolving host names.
*
*/
public class SystemDefaultDnsResolver implements DnsResolver {
/**
* {@inheritDoc}
*/
public InetAddress[] resolve(String host) throws UnknownHostException {
return InetAddress.getAllByName(host);
}
}

View File

@ -65,6 +65,13 @@ public void testDnsResolverUnknownHost() throws Exception {
operator.resolveHostname("unknown.example.com"); operator.resolveHostname("unknown.example.com");
} }
@Test
public void testDefaultLocalHost() throws Exception {
DefaultClientConnectionOperator operator = new DefaultClientConnectionOperator(
SchemeRegistryFactory.createDefault());
operator.resolveHostname("localhost");
}
private InetAddress[] translateIp(String ip) throws UnknownHostException { private InetAddress[] translateIp(String ip) throws UnknownHostException {
String[] ipParts = ip.split("\\."); String[] ipParts = ip.split("\\.");