org.apache.http.conn.DnsResolver Java Examples

The following examples show how to use org.apache.http.conn.DnsResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EndpointsTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testSupplierFactory() throws Exception {
  final DnsResolver resolver = mock(DnsResolver.class);
  when(resolver.resolve("example.com")).thenReturn(IPS_1);
  when(resolver.resolve("example.net")).thenReturn(IPS_2);
  final Supplier<List<URI>> uriSupplier = Suppliers.ofInstance(uris);
  final Supplier<List<Endpoint>> endpointSupplier = Endpoints.of(uriSupplier, resolver);
  final List<Endpoint> endpoints = endpointSupplier.get();

  assertThat(endpoints.size(), equalTo(4));
  assertThat(endpoints.get(0).getUri(), equalTo(uri1));
  assertThat(endpoints.get(0).getIp(), equalTo(IP_A));
  assertThat(endpoints.get(1).getUri(), equalTo(uri1));
  assertThat(endpoints.get(1).getIp(), equalTo(IP_B));
  assertThat(endpoints.get(2).getUri(), equalTo(uri2));
  assertThat(endpoints.get(2).getIp(), equalTo(IP_C));
  assertThat(endpoints.get(3).getUri(), equalTo(uri2));
  assertThat(endpoints.get(3).getIp(), equalTo(IP_D));
}
 
Example #2
Source File: EndpointsTest.java    From helios with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactory() throws Exception {
  final DnsResolver resolver = mock(DnsResolver.class);
  when(resolver.resolve("example.com")).thenReturn(IPS_1);
  when(resolver.resolve("example.net")).thenReturn(IPS_2);
  final List<Endpoint> endpoints = Endpoints.of(uris, resolver);

  assertThat(endpoints.size(), equalTo(4));
  assertThat(endpoints.get(0).getUri(), equalTo(uri1));
  assertThat(endpoints.get(0).getIp(), equalTo(IP_A));
  assertThat(endpoints.get(1).getUri(), equalTo(uri1));
  assertThat(endpoints.get(1).getIp(), equalTo(IP_B));
  assertThat(endpoints.get(2).getUri(), equalTo(uri2));
  assertThat(endpoints.get(2).getIp(), equalTo(IP_C));
  assertThat(endpoints.get(3).getUri(), equalTo(uri2));
  assertThat(endpoints.get(3).getIp(), equalTo(IP_D));
}
 
Example #3
Source File: HttpClientTest.java    From netcrusher-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    reactor = new NioReactor();

    crusher = TcpCrusherBuilder.builder()
            .withReactor(reactor)
            .withBindAddress("127.0.0.1", CRUSHER_PORT)
            .withConnectAddress(REMOTE_HOST, REMOTE_PORT)
            .buildAndOpen();

    DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
        @Override
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            if (host.equalsIgnoreCase(REMOTE_HOST)) {
                return new InetAddress[] { InetAddress.getByAddress(new byte[] {127, 0, 0, 1}) };
            } else {
                return super.resolve(host);
            }
        }
    };

    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> httpConnectionFactory =
            new ManagedHttpClientConnectionFactory();

    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry, httpConnectionFactory, dnsResolver);

    http = HttpClients.createMinimal(connectionManager);
}
 
Example #4
Source File: GalaxyFDSClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
public GalaxyFDSClient(GalaxyFDSCredential credential, FDSClientConfiguration fdsConfig,
    DnsResolver dnsResolver) {
  this.credential = credential;
  this.fdsConfig = fdsConfig;
  this.dnsResolver = dnsResolver;
  init();
}
 
Example #5
Source File: FDSHttpClient.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
public FDSHttpClient(FDSClientConfiguration fdsConfig, GalaxyFDSCredential credential,
    GalaxyFDSClient fdsClient, DnsResolver dnsResolver) {
  this.fdsConfig = fdsConfig;
  this.credential = credential;
  this.dnsResolver = dnsResolver;
  this.fdsClient = fdsClient;
  init();
}
 
Example #6
Source File: TestHttpClientDNSBlackList.java    From galaxy-fds-sdk-java with Apache License 2.0 5 votes vote down vote up
public TestHttpClientDNSBlackList() {

    super(new BasicFDSCredential("AUTH_AK", "AUTH_SK"), defaultFdsConfig,
        new DnsResolver() {
          @Override
          public InetAddress[] resolve(String host) throws UnknownHostException {
            ++dnsResolvedCnt;
            return localIpAddress;
          }
        });
  }
 
Example #7
Source File: Endpoints.java    From helios with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link Supplier} of a list of {@link Endpoint}.
 *
 * @param uriSupplier A Supplier of a list of URIs.
 * @param dnsResolver An instance of {@link DnsResolver}
 *
 * @return A Supplier of a list of Endpoints.
 */
static Supplier<List<Endpoint>> of(final Supplier<List<URI>> uriSupplier,
                                   final DnsResolver dnsResolver) {
  return new Supplier<List<Endpoint>>() {
    @Override
    public List<Endpoint> get() {
      return of(uriSupplier.get(), dnsResolver);
    }
  };
}
 
Example #8
Source File: TestWithProxiedEmbeddedServer.java    From Bastion with GNU General Public License v3.0 5 votes vote down vote up
private static DnsResolver prepareProxiedDnsResolver() {
    return new SystemDefaultDnsResolver() {
        @Override
        public InetAddress[] resolve(String host) throws UnknownHostException {
            if (host.equalsIgnoreCase("sushi-shop.test")) {
                return new InetAddress[]{InetAddress.getByName("127.0.0.1")};
            } else {
                return super.resolve(host);
            }
        }
    };
}
 
Example #9
Source File: TestWithProxiedEmbeddedServer.java    From Bastion with GNU General Public License v3.0 5 votes vote down vote up
private static BasicHttpClientConnectionManager prepareConnectionManager(DnsResolver dnsResolver, DefaultSchemePortResolver schemePortResolver) {
    return new BasicHttpClientConnectionManager(
            RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", SSLConnectionSocketFactory.getSocketFactory())
                    .build(),
            null,
            schemePortResolver,
            dnsResolver
    );
}
 
Example #10
Source File: TestWithProxiedEmbeddedServer.java    From Bastion with GNU General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void setupProxying() {
    DnsResolver dnsResolver = prepareProxiedDnsResolver();
    DefaultSchemePortResolver schemePortResolver = prepareSchemePortResolver();
    BasicHttpClientConnectionManager connManager = prepareConnectionManager(dnsResolver, schemePortResolver);
    HttpClient httpClient = prepareHttpClient(connManager);
    originalHttpClient = (HttpClient) Options.getOption(Option.HTTPCLIENT);
    Unirest.setHttpClient(httpClient);
}
 
Example #11
Source File: Endpoints.java    From helios with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of {@link Endpoint}.
 *
 * @param uris        A list of URIs.
 * @param dnsResolver An instance of {@link DnsResolver}
 *
 * @return A list of Endpoints.
 */
static List<Endpoint> of(final List<URI> uris, final DnsResolver dnsResolver) {
  final ImmutableList.Builder<Endpoint> endpoints = ImmutableList.builder();
  for (final URI uri : uris) {
    try {
      for (final InetAddress ip : dnsResolver.resolve(uri.getHost())) {
        endpoints.add(new DefaultEndpoint(uri, ip));
      }
    } catch (UnknownHostException e) {
      log.warn("Unable to resolve hostname {} into IP address", uri.getHost(), e);
    }
  }

  return endpoints.build();
}
 
Example #12
Source File: ExtendedConnectionOperator.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
public ExtendedConnectionOperator(
    Lookup<ConnectionSocketFactory> socketFactoryRegistry,
    SchemePortResolver schemePortResolver,
    DnsResolver dnsResolver
) {
  this.socketFactoryRegistry = socketFactoryRegistry;
  this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
  this.dnsResolver = dnsResolver != null ? dnsResolver : SystemDefaultDnsResolver.INSTANCE;
}
 
Example #13
Source File: EndpointsTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnableToResolve() throws Exception {
  final DnsResolver resolver = mock(DnsResolver.class);
  when(resolver.resolve("example.com")).thenThrow(new UnknownHostException());
  when(resolver.resolve("example.net")).thenThrow(new UnknownHostException());
  final List<Endpoint> endpoints = Endpoints.of(uris, resolver);

  assertThat(endpoints.size(), equalTo(0));
}
 
Example #14
Source File: EndpointsTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidUri_NoScheme() throws Exception {
  final DnsResolver resolver = mock(DnsResolver.class);
  when(resolver.resolve("example.com")).thenReturn(IPS_1);
  exception.expect(IllegalArgumentException.class);
  Endpoints.of(ImmutableList.of(new URI(null, "example.com", null, null)), resolver);
}
 
Example #15
Source File: EndpointsTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidUri_NoPort() throws Exception {
  final DnsResolver resolver = mock(DnsResolver.class);
  when(resolver.resolve("example.com")).thenReturn(IPS_1);
  exception.expect(IllegalArgumentException.class);
  Endpoints.of(ImmutableList.of(new URI("http", "example.com", null, null)), resolver);
}
 
Example #16
Source File: AbstractNativeClientBuilder.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private DnsResolver nullDnsResolver() {
    return new DnsResolver() {
        @Override
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            return new InetAddress[] {null};
        }
    };
}
 
Example #17
Source File: HttpClientFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildHttpClientAllPossible()
{
    String baseUrl = "http://somewh.ere/";
    config.setBaseUrl(baseUrl);
    config.setHeadersMap(HEADERS);
    config.setCredentials(CREDS);
    config.setAuthScope(AUTH_SCOPE);
    config.setSslCertificateCheckEnabled(false);
    config.setSkipResponseEntity(true);
    CookieStore cookieStore = new BasicCookieStore();
    config.setCookieStore(cookieStore);
    config.setSslHostnameVerificationEnabled(false);
    DnsResolver resolver = mock(DnsResolver.class);
    config.setDnsResolver(resolver);

    SSLContext mockedSSLContext = mock(SSLContext.class);
    when(mockedSSLContextManager.getSslContext(SSLConnectionSocketFactory.SSL, true))
            .thenReturn(Optional.of(mockedSSLContext));

    prepareClientBuilderUtilsMock();

    testBuildHttpClientUsingConfig();
    verify(mockedHttpClientBuilder).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
    verify(mockedHttpClient).setHttpHost(HttpHost.create(baseUrl));
    verify(mockedHttpClient).setSkipResponseEntity(config.isSkipResponseEntity());
    verify(mockedHttpClientBuilder).setSSLContext(mockedSSLContext);
    verify(mockedHttpClientBuilder).setDefaultCredentialsProvider(credentialsProvider);
    verify(mockedHttpClientBuilder).setDefaultCookieStore(cookieStore);
    verify(mockedHttpClientBuilder).setDnsResolver(resolver);
    verifyDefaultHeaderSetting(HEADERS.entrySet().iterator().next());
    PowerMockito.verifyStatic(ClientBuilderUtils.class);
    ClientBuilderUtils.createCredentialsProvider(AUTH_SCOPE, CREDS);
}
 
Example #18
Source File: HttpClientFactoryTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildDnsResolver()
{
    DnsResolver resolver = mock(DnsResolver.class);
    config.setDnsResolver(resolver);
    testBuildHttpClientUsingConfig();
    verify(mockedHttpClientBuilder).setDnsResolver(resolver);
}
 
Example #19
Source File: HttpClientConfigTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetAndSetDnsResolver()
{
    DnsResolver resolver = mock(DnsResolver.class);
    config.setDnsResolver(resolver);
    assertEquals(resolver, config.getDnsResolver());
}
 
Example #20
Source File: AbstractNativeClientBuilder.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private  DnsResolver nullDnsResolver() {
    return new DnsResolver() {
        @Override
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            return new InetAddress[] {null};
        }
    };
}
 
Example #21
Source File: DNS.java    From esigate with Apache License 2.0 4 votes vote down vote up
public DnsResolver getDnsResolver() {
    return dnsResolver;
}
 
Example #22
Source File: HttpClientConfig.java    From vividus with Apache License 2.0 4 votes vote down vote up
public DnsResolver getDnsResolver()
{
    return dnsResolver;
}
 
Example #23
Source File: RoundRobinDNSResolver.java    From galaxy-fds-sdk-java with Apache License 2.0 4 votes vote down vote up
public RoundRobinDNSResolver(DnsResolver dnsResolver) {
  this.dnsResolver = dnsResolver;
  this.random = new Random();
}
 
Example #24
Source File: InternalSiteBlackListDNSResolver.java    From galaxy-fds-sdk-java with Apache License 2.0 4 votes vote down vote up
public InternalSiteBlackListDNSResolver(IPAddressBlackList blackList,
    DnsResolver dnsResolver) {
  this.blackList = blackList;
  this.dnsResolver = dnsResolver;
}
 
Example #25
Source File: RestTemplateConfig.java    From spring-boot-cookbook with Apache License 2.0 4 votes vote down vote up
private CloseableHttpClient getHttpClient() {
        //注册访问协议相关的Socket工厂
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();

        //HttpConnectionFactory:配置写请求/解析响应处理器
        HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory(
                DefaultHttpRequestWriterFactory.INSTANCE,
                DefaultHttpResponseParserFactory.INSTANCE
        );

        //DNS解析器
        DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
        //创建连接池管理器
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver);
        //设置默认的socket参数
        manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());
        manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待
        //路由是对MaxTotal的细分。
        // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。
        // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool
        manager.setDefaultMaxPerRoute(200);//每个路由的最大连接
        manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s

        //配置默认的请求参数
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setConnectTimeout(2 * 1000)//连接超时设置为2s
                .setSocketTimeout(5 * 1000)//等待数据超时设置为5s
                .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s
//                .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理
                .build();

        CloseableHttpClient closeableHttpClient = HttpClients.custom()
                .setConnectionManager(manager)
                .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享
                .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接
                .evictExpiredConnections()//回收过期连接
                .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定
                .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数
                .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive
                .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间
                .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉
                .build();

        /**
         *JVM停止或重启时,关闭连接池释放掉连接
         */
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    LOGGER.info("closing http client");
                    closeableHttpClient.close();
                    LOGGER.info("http client closed");
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        });
        return closeableHttpClient;
    }
 
Example #26
Source File: RestTemplateConfig.java    From spring-boot-cookbook with Apache License 2.0 4 votes vote down vote up
private CloseableHttpClient getHttpClient() {
        //注册访问协议相关的Socket工厂
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();

        //HttpConnectionFactory:配置写请求/解析响应处理器
        HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory = new ManagedHttpClientConnectionFactory(
                DefaultHttpRequestWriterFactory.INSTANCE,
                DefaultHttpResponseParserFactory.INSTANCE
        );

        //DNS解析器
        DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
        //创建连接池管理器
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connectionFactory, dnsResolver);
        //设置默认的socket参数
        manager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());
        manager.setMaxTotal(300);//设置最大连接数。高于这个值时,新连接请求,需要阻塞,排队等待
        //路由是对MaxTotal的细分。
        // 每个路由实际最大连接数默认值是由DefaultMaxPerRoute控制。
        // MaxPerRoute设置的过小,无法支持大并发:ConnectionPoolTimeoutException:Timeout waiting for connection from pool
        manager.setDefaultMaxPerRoute(200);//每个路由的最大连接
        manager.setValidateAfterInactivity(5 * 1000);//在从连接池获取连接时,连接不活跃多长时间后需要进行一次验证,默认为2s

        //配置默认的请求参数
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setConnectTimeout(2 * 1000)//连接超时设置为2s
                .setSocketTimeout(5 * 1000)//等待数据超时设置为5s
                .setConnectionRequestTimeout(2 * 1000)//从连接池获取连接的等待超时时间设置为2s
//                .setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.2", 1234))) //设置代理
                .build();

        CloseableHttpClient closeableHttpClient = HttpClients.custom()
                .setConnectionManager(manager)
                .setConnectionManagerShared(false)//连接池不是共享模式,这个共享是指与其它httpClient是否共享
                .evictIdleConnections(60, TimeUnit.SECONDS)//定期回收空闲连接
                .evictExpiredConnections()//回收过期连接
                .setConnectionTimeToLive(60, TimeUnit.SECONDS)//连接存活时间,如果不设置,则根据长连接信息决定
                .setDefaultRequestConfig(defaultRequestConfig)//设置默认的请求参数
                .setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE)//连接重用策略,即是否能keepAlive
                .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)//长连接配置,即获取长连接生产多长时间
                .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))//设置重试次数,默认为3次;当前是禁用掉
                .build();

        /**
         *JVM停止或重启时,关闭连接池释放掉连接
         */
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    closeableHttpClient.close();
                    log.info("http client closed");
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        });
        return closeableHttpClient;
    }
 
Example #27
Source File: FetchingThread.java    From BUbiNG with Apache License 2.0 4 votes vote down vote up
public BasicHttpClientConnectionManagerWithAlternateDNS(final DnsResolver dnsResolver) {
	super(getDefaultRegistry(), null, null, dnsResolver);
}
 
Example #28
Source File: DelegatingDnsResolver.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
public DelegatingDnsResolver(com.ibm.cloud.objectstorage.DnsResolver delegate) {
    this.delegate = delegate;
}
 
Example #29
Source File: ApacheHttpDnsClientBuilder.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public void setDnsResover(DnsResolver dnsResover) {
  this.dnsResover = dnsResover;
}
 
Example #30
Source File: ApacheHttpDnsClientBuilder.java    From weixin-java-tools with Apache License 2.0 4 votes vote down vote up
public DnsResolver getDnsResover() {
  return dnsResover;
}