Search in sources :

Example 6 with ConnectionConfig

use of org.apache.http.config.ConnectionConfig in project gateway-dubbox by zhuzhong.

the class OpenApiHttpAsynClientServiceImpl method initHttpAsynClient.

private void initHttpAsynClient() throws IOReactorException {
    // Use custom message parser / writer to customize the way HTTP
    // messages are parsed from and written out to the data stream.
    NHttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory() {

        @Override
        public NHttpMessageParser<HttpResponse> create(final SessionInputBuffer buffer, final MessageConstraints constraints) {
            LineParser lineParser = new BasicLineParser() {

                @Override
                public Header parseHeader(final CharArrayBuffer buffer) {
                    try {
                        return super.parseHeader(buffer);
                    } catch (ParseException ex) {
                        return new BasicHeader(buffer.toString(), null);
                    }
                }
            };
            return new DefaultHttpResponseParser(buffer, lineParser, DefaultHttpResponseFactory.INSTANCE, constraints);
        }
    };
    NHttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory();
    // Use a custom connection factory to customize the process of
    // initialization of outgoing HTTP connections. Beside standard
    // connection
    // configuration parameters HTTP connection factory can define message
    // parser / writer routines to be employed by individual connections.
    NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory(requestWriterFactory, responseParserFactory, HeapByteBufferAllocator.INSTANCE);
    // Client HTTP connection objects when fully initialized can be bound to
    // an arbitrary network socket. The process of network socket
    // initialization,
    // its connection to a remote address and binding to a local one is
    // controlled
    // by a connection socket factory.
    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    // SSLContext sslcontext = org.apache.http.ssl.SSLContexts.createSystemDefault();
    // SSLContext sslcontext = org.apache.http.ssl.SSLContexts.createDefault();
    SSLContext sslcontext = null;
    try {
        sslcontext = this.createIgnoreVerifySSL();
    } catch (KeyManagementException | NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Use custom hostname verifier to customize SSL hostname verification.
    HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
    // Create a registry of custom connection session strategies for
    // supported
    // protocol schemes.
    Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", new SSLIOSessionStrategy(sslcontext)).build();
    // .register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
    // Use custom DNS resolver to override the system DNS resolution.
    DnsResolver dnsResolver = new SystemDefaultDnsResolver() {

        @Override
        public InetAddress[] resolve(final String host) throws UnknownHostException {
            if (host.equalsIgnoreCase("myhost")) {
                return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) };
            } else {
                return super.resolve(host);
            }
        }
    };
    // Create I/O reactor configuration
    IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).setConnectTimeout(30000).setSoTimeout(30000).build();
    // Create a custom I/O reactort
    ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
    // Create a connection manager with custom configuration.
    PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(ioReactor, connFactory, sessionStrategyRegistry, dnsResolver);
    // Create message constraints
    MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200).setMaxLineLength(2000).build();
    // Create connection configuration
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE).setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).setMessageConstraints(messageConstraints).build();
    // Configure the connection manager to use connection configuration
    // either
    // by default or for a specific host.
    connManager.setDefaultConnectionConfig(connectionConfig);
    // connManager.setConnectionConfig(new HttpHost("somehost", 80),
    // ConnectionConfig.DEFAULT);
    // 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(100);
    connManager.setDefaultMaxPerRoute(10);
    // connManager.setMaxPerRoute(new HttpRoute(new HttpHost("somehost",
    // 80)), 20);
    // Use custom cookie store if necessary.
    CookieStore cookieStore = new BasicCookieStore();
    // Use custom credentials provider if necessary.
    // CredentialsProvider credentialsProvider = new
    // BasicCredentialsProvider();
    // credentialsProvider.setCredentials(new AuthScope("localhost", 8889),
    // new UsernamePasswordCredentials("squid", "nopassword"));
    // Create global request configuration
    RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
    // Create an HttpClient with the given custom dependencies and
    // configuration.
    // CloseableHttpAsyncClient
    httpAsyncClient = HttpAsyncClients.custom().setConnectionManager(connManager).build();
}
Also used : ConnectingIOReactor(org.apache.http.nio.reactor.ConnectingIOReactor) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) KeyManagementException(java.security.KeyManagementException) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) BasicLineParser(org.apache.http.message.BasicLineParser) LineParser(org.apache.http.message.LineParser) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) DefaultHttpResponseParser(org.apache.http.impl.nio.codecs.DefaultHttpResponseParser) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) ConnectionConfig(org.apache.http.config.ConnectionConfig) HttpRequest(org.apache.http.HttpRequest) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) DnsResolver(org.apache.http.conn.DnsResolver) RequestConfig(org.apache.http.client.config.RequestConfig) SessionInputBuffer(org.apache.http.nio.reactor.SessionInputBuffer) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) ManagedNHttpClientConnection(org.apache.http.nio.conn.ManagedNHttpClientConnection) HttpResponse(org.apache.http.HttpResponse) BasicLineParser(org.apache.http.message.BasicLineParser) SSLContext(javax.net.ssl.SSLContext) ManagedNHttpClientConnectionFactory(org.apache.http.impl.nio.conn.ManagedNHttpClientConnectionFactory) DefaultHttpRequestWriterFactory(org.apache.http.impl.nio.codecs.DefaultHttpRequestWriterFactory) HostnameVerifier(javax.net.ssl.HostnameVerifier) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) DefaultHttpResponseParserFactory(org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) MessageConstraints(org.apache.http.config.MessageConstraints) ParseException(org.apache.http.ParseException) InetAddress(java.net.InetAddress) BasicHeader(org.apache.http.message.BasicHeader)

Example 7 with ConnectionConfig

use of org.apache.http.config.ConnectionConfig in project zm-mailbox by Zimbra.

the class FeedManager method retrieveRemoteData.

private static RemoteDataInfo retrieveRemoteData(String url, Folder.SyncData fsd) throws ServiceException, HttpException, IOException {
    assert !Strings.isNullOrEmpty(url);
    HttpClientBuilder clientBuilder = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpProxyUtil.configureProxy(clientBuilder);
    // cannot set connection timeout because it'll affect all HttpClients associated with the conn mgr.
    // see comments in ZimbraHttpConnectionManager
    // client.setConnectionTimeout(10000);
    SocketConfig config = SocketConfig.custom().setSoTimeout(60000).build();
    clientBuilder.setDefaultSocketConfig(config);
    ConnectionConfig connConfig = ConnectionConfig.custom().setCharset(Charset.forName(MimeConstants.P_CHARSET_UTF8)).build();
    clientBuilder.setDefaultConnectionConfig(connConfig);
    HttpGet get = null;
    BufferedInputStream content = null;
    long lastModified = 0;
    String expectedCharset = MimeConstants.P_CHARSET_UTF8;
    int redirects = 0;
    int statusCode = HttpServletResponse.SC_NOT_FOUND;
    try {
        do {
            String lcurl = url.toLowerCase();
            if (lcurl.startsWith("webcal:")) {
                url = "http:" + url.substring(7);
            } else if (lcurl.startsWith("feed:")) {
                url = "http:" + url.substring(5);
            } else if (!lcurl.startsWith("http:") && !lcurl.startsWith("https:")) {
                throw ServiceException.INVALID_REQUEST("url must begin with http: or https:", null);
            }
            // Add AuthCache to the execution context
            HttpClientContext context = HttpClientContext.create();
            URIBuilder httpurl;
            try {
                httpurl = new URIBuilder(url);
            } catch (URISyntaxException e1) {
                throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, e1);
            }
            // validate target address (also handles followed `location` header addresses)
            if (isBlockedFeedAddress(httpurl)) {
                ZimbraLog.misc.info("Feed ip address blocked: %s. See localconfig for comma-separated ip list configuration: " + "zimbra_feed_manager_blacklist, zimbra_feed_manager_whitelist", url);
                throw ServiceException.INVALID_REQUEST(String.format("Address for feed is blocked: %s. See mailbox logs for details.", url), null);
            }
            // username and password are encoded in the URL as http://user:pass@host/...
            if (url.indexOf('@') != -1) {
                if (httpurl.getUserInfo() != null) {
                    String user = httpurl.getUserInfo();
                    if (user.indexOf('%') != -1) {
                        try {
                            user = URLDecoder.decode(user, "UTF-8");
                        } catch (OutOfMemoryError e) {
                            Zimbra.halt("out of memory", e);
                        } catch (Throwable t) {
                        }
                    }
                    int index = user.indexOf(':');
                    String userName = user.substring(0, index);
                    String password = user.substring(index + 1);
                    CredentialsProvider provider = new BasicCredentialsProvider();
                    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
                    provider.setCredentials(AuthScope.ANY, credentials);
                    clientBuilder.setDefaultCredentialsProvider(provider);
                    // Create AuthCache instance
                    AuthCache authCache = new BasicAuthCache();
                    // Generate BASIC scheme object and add it to the local auth cache
                    BasicScheme basicAuth = new BasicScheme();
                    authCache.put(new HttpHost(httpurl.getHost()), basicAuth);
                    // Add AuthCache to the execution context
                    context.setCredentialsProvider(provider);
                    context.setAuthCache(authCache);
                }
            }
            try {
                get = new HttpGet(url);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("out of memory", e);
                return null;
            } catch (Throwable t) {
                ZimbraLog.misc.warnQuietly(String.format("invalid url for feed: %s", url), t);
                throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, null);
            }
            DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
            clientBuilder.setRedirectStrategy(redirectStrategy);
            get.addHeader("User-Agent", HTTP_USER_AGENT);
            get.addHeader("Accept", HTTP_ACCEPT);
            if (fsd != null && fsd.getLastSyncDate() > 0) {
                String lastSyncAt = DateUtils.formatDate(new Date(fsd.getLastSyncDate()));
                get.addHeader("If-Modified-Since", lastSyncAt);
            }
            HttpClient client = clientBuilder.build();
            HttpResponse response = HttpClientUtil.executeMethod(client, get, context);
            Header locationHeader = response.getFirstHeader("location");
            if (locationHeader != null) {
                // update our target URL and loop again to do another HTTP GET
                url = locationHeader.getValue();
                get.releaseConnection();
            } else {
                statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpServletResponse.SC_OK) {
                    Header contentEncoding = response.getFirstHeader("Content-Encoding");
                    InputStream respInputStream = response.getEntity().getContent();
                    if (contentEncoding != null) {
                        if (contentEncoding.getValue().indexOf("gzip") != -1) {
                            respInputStream = new GZIPInputStream(respInputStream);
                        }
                    }
                    content = new BufferedInputStream(respInputStream);
                    org.apache.http.entity.ContentType contentType = org.apache.http.entity.ContentType.getOrDefault(response.getEntity());
                    if (contentType != null && contentType.getCharset() != null) {
                        expectedCharset = contentType.getCharset().name();
                    }
                    Header lastModHdr = response.getFirstHeader("Last-Modified");
                    if (lastModHdr == null) {
                        lastModHdr = response.getFirstHeader("Date");
                    }
                    if (lastModHdr != null) {
                        Date d = DateUtils.parseDate(lastModHdr.getValue());
                        lastModified = d.getTime();
                    } else {
                        lastModified = System.currentTimeMillis();
                    }
                } else if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
                    ZimbraLog.misc.debug("Remote data at " + url + " not modified since last sync");
                    return new RemoteDataInfo(statusCode, redirects, null, expectedCharset, lastModified);
                } else {
                    throw ServiceException.RESOURCE_UNREACHABLE(response.getStatusLine().toString(), null);
                }
                break;
            }
        } while (++redirects <= MAX_REDIRECTS);
    } catch (ServiceException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    } catch (HttpException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    } catch (IOException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    }
    RemoteDataInfo rdi = new RemoteDataInfo(statusCode, redirects, content, expectedCharset, lastModified);
    rdi.setGetMethod(get);
    return rdi;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) URISyntaxException(java.net.URISyntaxException) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) HttpHost(org.apache.http.HttpHost) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) HttpException(org.apache.http.HttpException) ConnectionConfig(org.apache.http.config.ConnectionConfig) BasicScheme(org.apache.http.impl.auth.BasicScheme) SocketConfig(org.apache.http.config.SocketConfig) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) Date(java.util.Date) URIBuilder(org.apache.http.client.utils.URIBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) ServiceException(com.zimbra.common.service.ServiceException) HttpClient(org.apache.http.client.HttpClient)

Example 8 with ConnectionConfig

use of org.apache.http.config.ConnectionConfig in project mycore by MyCoRe-Org.

the class MCRHttpUtils method getHttpClient.

public static CloseableHttpClient getHttpClient(HttpClientConnectionManager connectionManager, int maxConnections) {
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build();
    SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoReuseAddress(true).build();
    String userAgent = MessageFormat.format("MyCoRe/{0} ({1}; java {2})", MCRCoreVersion.getCompleteVersion(), MCRConfiguration.instance().getString("MCR.NameOfProject", "undefined"), System.getProperty("java.version"));
    // setup http client
    return HttpClients.custom().setConnectionManager(connectionManager).setUserAgent(userAgent).setRetryHandler(new MCRRetryHandler(maxConnections)).setDefaultRequestConfig(requestConfig).setDefaultConnectionConfig(connectionConfig).setDefaultSocketConfig(socketConfig).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) SocketConfig(org.apache.http.config.SocketConfig) ConnectionConfig(org.apache.http.config.ConnectionConfig)

Example 9 with ConnectionConfig

use of org.apache.http.config.ConnectionConfig in project knox by apache.

the class KnoxSession method createClient.

@SuppressForbidden
protected CloseableHttpClient createClient(ClientContext clientContext) throws GeneralSecurityException {
    // SSL
    HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    TrustStrategy trustStrategy = null;
    if (clientContext.connection().secure()) {
        hostnameVerifier = SSLConnectionSocketFactory.getDefaultHostnameVerifier();
    } else {
        trustStrategy = TrustSelfSignedStrategy.INSTANCE;
        System.out.println("**************** WARNING ******************\n" + "This is an insecure client instance and may\n" + "leave the interactions subject to a man in\n" + "the middle attack. Please use the login()\n" + "method instead of loginInsecure() for any\n" + "sensitive or production usecases.\n" + "*******************************************");
    }
    KeyStore trustStore = getTrustStore(clientContext);
    SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore, trustStrategy).build();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build();
    // Pool
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(clientContext.pool().maxTotal());
    connectionManager.setDefaultMaxPerRoute(clientContext.pool().defaultMaxPerRoute());
    ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(clientContext.connection().bufferSize()).build();
    connectionManager.setDefaultConnectionConfig(connectionConfig);
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(clientContext.socket().keepalive()).setSoLinger(clientContext.socket().linger()).setSoReuseAddress(clientContext.socket().reuseAddress()).setSoTimeout(clientContext.socket().timeout()).setTcpNoDelay(clientContext.socket().tcpNoDelay()).build();
    connectionManager.setDefaultSocketConfig(socketConfig);
    // Auth
    URI uri = URI.create(clientContext.url());
    host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
    /* kerberos auth */
    if (clientContext.kerberos().enable()) {
        isKerberos = true;
        /* set up system properties */
        if (!StringUtils.isBlank(clientContext.kerberos().krb5Conf())) {
            System.setProperty("java.security.krb5.conf", clientContext.kerberos().krb5Conf());
        }
        if (!StringUtils.isBlank(clientContext.kerberos().jaasConf())) {
            File f = new File(clientContext.kerberos().jaasConf());
            if (f.exists()) {
                try {
                    jaasConfigURL = f.getCanonicalFile().toURI().toURL();
                    LOG.jaasConfigurationLocation(jaasConfigURL.toExternalForm());
                } catch (IOException e) {
                    LOG.failedToLocateJAASConfiguration(e.getMessage());
                }
            } else {
                LOG.jaasConfigurationDoesNotExist(f.getAbsolutePath());
            }
        }
        // Fall back to the default JAAS config
        if (jaasConfigURL == null) {
            LOG.usingDefaultJAASConfiguration();
            jaasConfigURL = getClass().getResource(DEFAULT_JAAS_FILE);
            LOG.jaasConfigurationLocation(jaasConfigURL.toExternalForm());
        }
        if (clientContext.kerberos().debug()) {
            System.setProperty("sun.security.krb5.debug", "true");
            System.setProperty("sun.security.jgss.debug", "true");
        }
        // (KNOX-2001) Log a warning if the useSubjectCredsOnly restriction is "relaxed"
        String useSubjectCredsOnly = System.getProperty("javax.security.auth.useSubjectCredsOnly");
        if (useSubjectCredsOnly != null && !Boolean.parseBoolean(useSubjectCredsOnly)) {
            LOG.useSubjectCredsOnlyIsFalse();
        }
        final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
        return HttpClients.custom().setConnectionManager(connectionManager).setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCredentialsProvider(EMPTY_CREDENTIALS_PROVIDER).build();
    } else {
        AuthCache authCache = new BasicAuthCache();
        BasicScheme authScheme = new BasicScheme();
        authCache.put(host, authScheme);
        context = new BasicHttpContext();
        context.setAttribute(org.apache.http.client.protocol.HttpClientContext.AUTH_CACHE, authCache);
        CredentialsProvider credentialsProvider = null;
        if (clientContext.username() != null && clientContext.password() != null) {
            credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(host.getHostName(), host.getPort()), new UsernamePasswordCredentials(clientContext.username(), clientContext.password()));
        }
        return HttpClients.custom().setConnectionManager(connectionManager).setDefaultCredentialsProvider(credentialsProvider).build();
    }
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) URI(java.net.URI) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) HttpHost(org.apache.http.HttpHost) ConnectionConfig(org.apache.http.config.ConnectionConfig) BasicScheme(org.apache.http.impl.auth.BasicScheme) SocketConfig(org.apache.http.config.SocketConfig) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) KeyStore(java.security.KeyStore) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) AuthScope(org.apache.http.auth.AuthScope) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) File(java.io.File) SuppressForbidden(de.thetaphi.forbiddenapis.SuppressForbidden)

Example 10 with ConnectionConfig

use of org.apache.http.config.ConnectionConfig in project sagacity-sqltoy by chenrenfei.

the class ElasticEndpoint method initRestClient.

/**
 * @param restClient the restClient to set
 */
public void initRestClient() {
    if (StringUtil.isBlank(this.getUrl())) {
        return;
    }
    if (restClient == null) {
        // 替换全角字符
        String[] urls = this.getUrl().replaceAll("\\;", ";").replaceAll("\\,", ",").replaceAll("\\;", ",").split("\\,");
        // 当为单一地址时使用httpclient直接调用
        if (urls.length < 2) {
            return;
        }
        List<HttpHost> hosts = new ArrayList<HttpHost>();
        for (String urlStr : urls) {
            try {
                if (StringUtil.isNotBlank(urlStr)) {
                    URL url = new java.net.URL(urlStr.trim());
                    hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        if (!hosts.isEmpty()) {
            HttpHost[] hostAry = new HttpHost[hosts.size()];
            hosts.toArray(hostAry);
            RestClientBuilder builder = RestClient.builder(hostAry);
            final ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build();
            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout).setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build();
            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
            final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername()) && StringUtil.isNotBlank(getPassword())) ? true : false;
            // 是否ssl证书模式
            final boolean hasSsl = StringUtil.isNotBlank(this.keyStore);
            // 凭据提供器
            if (hasCrede) {
                credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
                new UsernamePasswordCredentials(getUsername(), getPassword()));
            }
            SSLContextBuilder sslBuilder = null;
            try {
                if (hasSsl) {
                    KeyStore truststore = KeyStore.getInstance(StringUtil.isBlank(keyStoreType) ? KeyStore.getDefaultType() : keyStoreType);
                    truststore.load(FileUtil.getFileInputStream(keyStore), (keyStorePass == null) ? null : keyStorePass.toCharArray());
                    sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, keyStoreSelfSign ? new TrustSelfSignedStrategy() : null);
                }
                final SSLContext sslContext = (sslBuilder == null) ? null : sslBuilder.build();
                final boolean disableAuthCaching = !authCaching;
                builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.setDefaultConnectionConfig(connectionConfig).setDefaultRequestConfig(requestConfig);
                        // 禁用抢占式身份验证
                        if (disableAuthCaching) {
                            httpClientBuilder.disableAuthCaching();
                        }
                        // 用户名密码
                        if (hasCrede) {
                            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
                        }
                        // 证书
                        if (hasSsl) {
                            httpClientBuilder.setSSLContext(sslContext);
                        }
                        return httpClientBuilder;
                    }
                });
                restClient = builder.build();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArrayList(java.util.ArrayList) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ConnectionConfig(org.apache.http.config.ConnectionConfig) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

ConnectionConfig (org.apache.http.config.ConnectionConfig)10 SSLContext (javax.net.ssl.SSLContext)6 HttpHost (org.apache.http.HttpHost)6 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)5 CredentialsProvider (org.apache.http.client.CredentialsProvider)5 RequestConfig (org.apache.http.client.config.RequestConfig)5 SocketConfig (org.apache.http.config.SocketConfig)5 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)5 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)4 IOException (java.io.IOException)3 KeyStore (java.security.KeyStore)3 HostnameVerifier (javax.net.ssl.HostnameVerifier)3 HttpResponse (org.apache.http.HttpResponse)3 AuthCache (org.apache.http.client.AuthCache)3 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)3 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)3 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)3 BasicScheme (org.apache.http.impl.auth.BasicScheme)3 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)3 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)3