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();
}
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;
}
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();
}
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();
}
}
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();
}
}
}
}
Aggregations