Java 类org.apache.http.impl.conn.SystemDefaultDnsResolver 实例源码

项目:netcrusher-java    文件:HttpClientTest.java   
@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);
}
项目:Bastion    文件:TestWithProxiedEmbeddedServer.java   
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);
            }
        }
    };
}
项目:raven    文件:HttpConnectionAdaptor.java   
private CloseableHttpClient createHttpClient() throws NSPException {
    try {
        initSocketFactory();

        DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
            public InetAddress[] resolve(String host)
                    throws UnknownHostException {
                if (host.equalsIgnoreCase("localhost")) {
                    return new InetAddress[] { InetAddress
                            .getByAddress(new byte[] { 127, 0, 0, 1 }) };
                }

                return super.resolve(host);
            }
        };
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
                this.socketFactoryRegistry, dnsResolver);

        SocketConfig socketConfig = SocketConfig.custom()
                .setTcpNoDelay(true).setSoKeepAlive(true).setSoLinger(0)
                .setSoReuseAddress(true).build();

        connManager.setDefaultSocketConfig(socketConfig);

        ConnectionConfig connectionConfig = ConnectionConfig.custom()
                .setCharset(Consts.UTF_8).build();
        connManager.setDefaultConnectionConfig(connectionConfig);
        connManager.setConnectionConfig(new HttpHost("localhost", 80),
                ConnectionConfig.DEFAULT);

        connManager.setMaxTotal(this.mMaxConnections);

        if ((this.mMaxConnections <= this.mConnectionsPerRoute)
                && (this.mMaxConnections > 0)) {
            this.mConnectionsPerRoute = this.mMaxConnections;
        }

        connManager.setDefaultMaxPerRoute(this.mConnectionsPerRoute);

        connManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost",
                80)), 100);

        CloseableHttpClient httpclient = HttpClients
                .custom()
                .setConnectionManager(connManager)
                .setDefaultRequestConfig(
                        RequestConfig.copy(defaultRequestConfig).build())
                .build();

        return ((CloseableHttpClient) new WeakReference(httpclient).get());
    } catch (Exception e) {
        throw new NSPException(2, "Service unavailable.", e);
    }
}
项目:java-restclient    文件:HTTPCBuilder.java   
protected ExecCallbackAsyncREST<HttpResponse> buildAsyncClient(RESTPool pool) throws IOException {
    SSLContext sslContext;
    try {
        sslContext = SSLContext.getDefault();
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    }

    Registry<SchemeIOSessionStrategy> socketRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
            .register("http", NoopIOSessionStrategy.INSTANCE)
            .register("https", new SSLIOSessionStrategy(sslContext, NoopHostnameVerifier.INSTANCE))
            .build();

    IOReactorConfig socketConfig = IOReactorConfig.custom()
            .setIoThreadCount(pool.getReactorThreadCount())
            .setSoTimeout(new Long(pool.getSocketTimeout()).intValue())
            .setTcpNoDelay(true)
            .setSoKeepAlive(true)
            .setSelectInterval(REACTOR_SELECT_INTERVAL)
            .build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setCharset(StandardCharsets.UTF_8)
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .build();

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue())
            .setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue())
            .setExpectContinueEnabled(pool.expectContinue())
            .setRedirectsEnabled(false)
            .setStaleConnectionCheckEnabled(pool.getValidationOnInactivity() >= 0)
            .build();

    NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory(
            new org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory(),
            new org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory(),
            HeapByteBufferAllocator.INSTANCE
    );

    //TODO set validateAfterInactivity when supported
    PoolingNHttpClientConnectionManager ccm = new PoolingNHttpClientConnectionManager(
            new DefaultConnectingIOReactor(socketConfig),
            connFactory,
            socketRegistry,
            new SystemDefaultDnsResolver()
    );

    ccm.setMaxTotal(pool.getMaxTotal());
    ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute());
    ccm.setDefaultConnectionConfig(connectionConfig);

    HttpAsyncClientBuilder builder = HttpAsyncClients.custom()
            .setConnectionManager(ccm)
            .setDefaultRequestConfig(requestConfig)
            .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
            .disableCookieManagement();

    IdleAsyncConnectionEvictor evictor = new IdleAsyncConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS);

    addProxy(pool, builder);

    handleRedirects(pool, builder);

    CloseableHttpAsyncClient servClient = builder.build();

    servClient.start();

    HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCAsyncClientMonitor(pool.getName(), ccm) : null;

    return new HTTPCAsyncClient(servClient, evictor, monitor);
}
项目:mandrel    文件:ApacheHttpRequester.java   
public void init() {

        available = new Semaphore(maxParallel(), true);

        SSLContext sslContext = SSLContexts.createSystemDefault();
        HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();

        Registry<ConnectionSocketFactory> sessionStrategyRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build();

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

        // Create a connection manager with custom configuration.
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(sessionStrategyRegistry, dnsResolver);

        // Create message constraints
        MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(maxHeaderCount).setMaxLineLength(maxLineLength).build();

        // Create connection configuration
        ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
                .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).setMessageConstraints(messageConstraints).build();
        connManager.setDefaultConnectionConfig(connectionConfig);

        // Configure total max or per route limits for persistent connections
        // that can be kept in the pool or leased by the connection manager.
        connManager.setMaxTotal(maxPersistentConnections());
        connManager.setDefaultMaxPerRoute(maxPersistentConnections());

        // TODO
        // Use custom credentials provider if necessary.
        // CredentialsProvider credentialsProvider = new
        // BasicCredentialsProvider();

        // Create global request configuration
        defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setStaleConnectionCheckEnabled(true)
                .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setMaxRedirects(maxRedirects()).setSocketTimeout(socketTimeout())
                .setConnectTimeout(connectTimeout()).setConnectionRequestTimeout(requestTimeOut()).setRedirectsEnabled(followRedirects()).build();

        // Create an HttpClient with the given custom dependencies and
        // configuration.
        client = HttpClients.custom().setConnectionManager(connManager)
        // .setDefaultCredentialsProvider(credentialsProvider)
                .setDefaultRequestConfig(defaultRequestConfig).build();
    }
项目:galaxy-fds-sdk-java    文件:GalaxyFDSClient.java   
private HttpClient createHttpClient(FDSClientConfiguration config) {
  RequestConfig requestConfig = RequestConfig.custom()
      .setConnectTimeout(config.getConnectionTimeoutMs())
      .setSocketTimeout(config.getSocketTimeoutMs())
      .build();

  RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
  registryBuilder.register("http", new ConnectionInfoRecorderSocketFactory(
      new PlainConnectionSocketFactory()));

  if (config.isHttpsEnabled()) {
    SSLContext sslContext = SSLContexts.createSystemDefault();
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
        sslContext,
        SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    registryBuilder.register("https", new ConnectionInfoRecorderSocketFactory(sslsf));
  }
  ipBlackList = new TimeBasedIpAddressBlackList(config.getIpAddressNegativeDurationMillsec());
  connectionManager = new PoolingHttpClientConnectionManager(registryBuilder.build(),
      null,
      null,
      new RoundRobinDNSResolver(new InternalSiteBlackListDNSResolver(ipBlackList,
          this.dnsResolver == null ?
              SystemDefaultDnsResolver.INSTANCE : this.dnsResolver)),
      config.getHTTPKeepAliveTimeoutMS(), TimeUnit.MILLISECONDS);
  connectionManager.setDefaultMaxPerRoute(config.getMaxConnection());
  connectionManager.setMaxTotal(config.getMaxConnection());

  FDSBlackListEnabledHostChecker fdsBlackListEnabledHostChecker = new FDSBlackListEnabledHostChecker();
  retryHandler = new InternalIpBlackListRetryHandler(config.getRetryCount(),
      ipBlackList, fdsBlackListEnabledHostChecker);
  HttpClient httpClient = HttpClients.custom()
      .setRetryHandler(retryHandler)
      .setServiceUnavailableRetryStrategy(new ServiceUnavailableDNSBlackListStrategy(
          config.getRetryCount(),
          config.getRetryIntervalMilliSec(),
          ipBlackList,
          fdsBlackListEnabledHostChecker))
      .setConnectionManager(connectionManager)
      .setDefaultRequestConfig(requestConfig)
      .build();
  return httpClient;
}
项目:ms-nos    文件:DnsResolverWithTimeout.java   
public DnsResolverWithTimeout() {
    this(DNS_RESOLVE_EXECUTOR, new SystemDefaultDnsResolver(), getDnsTimeout());
}