Here are the examples of the java api org.apache.http.config.Registry taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
120 Examples
19
Source : HttpClientUtil.java
with Apache License 2.0
from yfh0918
with Apache License 2.0
from yfh0918
/**
* 创建HttpClient对象
*
* @return
* @author
* @create
*/
public static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int retryCount) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP, plainsf).register(HTTPS, sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
// 请求重试处理
CloseableHttpClient httpClient = null;
if (retryCount > 0) {
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= retryCount) {
// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {
// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {
// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {
// 超时
return false;
}
if (exception instanceof UnknownHostException) {
// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {
// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {
// SSL握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEnreplacedyEnclosingRequest)) {
return true;
}
return false;
}
};
httpClient = HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
} else {
httpClient = HttpClients.custom().setConnectionManager(cm).build();
}
return httpClient;
}
19
Source : HttpClientDownloader.java
with MIT License
from xiepuhuan
with MIT License
from xiepuhuan
private PoolingHttpClientConnectionManager buildConnectionManager() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", buildSSLConnectionSocketFactory()).build();
clientConnectionManager = new PoolingHttpClientConnectionManager(registry);
clientConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build());
clientConnectionManager.setDefaultMaxPerRoute(config.getDefaultMaxPerRoute());
return clientConnectionManager;
}
19
Source : NetUtil.java
with Apache License 2.0
from TremoloSecurity
with Apache License 2.0
from TremoloSecurity
/**
* NetUtil
*/
public clreplaced NetUtil {
private static KeyStore ks;
private static String ksPreplacedword;
private static KeyManagerFactory kmf;
private static Registry<ConnectionSocketFactory> httpClientRegistry;
private static RequestConfig globalHttpClientConfig;
private static String pathToMoreCerts;
public static void reinit() throws Exception {
initialize(pathToMoreCerts);
}
public static void initialize(String pathToMoreCerts) throws Exception {
NetUtil.pathToMoreCerts = pathToMoreCerts;
ksPreplacedword = UUID.randomUUID().toString();
ks = KeyStore.getInstance("PKCS12");
ks.load(null, ksPreplacedword.toCharArray());
File moreCerts = new File(pathToMoreCerts);
if (moreCerts.exists() && moreCerts.isDirectory()) {
for (File certFile : moreCerts.listFiles()) {
System.out.println("Processing - '" + certFile.getAbsolutePath() + "'");
if (certFile.isDirectory() || !certFile.getAbsolutePath().toLowerCase().endsWith(".pem")) {
System.out.println("not a pem, sipping");
continue;
}
String certPem = new String(Files.readAllBytes(Paths.get(certFile.getAbsolutePath())), StandardCharsets.UTF_8);
String alias = certFile.getName().substring(0, certFile.getName().indexOf('.'));
CertUtils.importCertificate(ks, ksPreplacedword, alias, certPem);
}
}
KeyStore cacerts = KeyStore.getInstance(KeyStore.getDefaultType());
String cacertsPath = System.getProperty("javax.net.ssl.trustStore");
if (cacertsPath == null) {
cacertsPath = System.getProperty("java.home") + "/lib/security/cacerts";
}
cacerts.load(new FileInputStream(cacertsPath), null);
Enumeration<String> enumer = cacerts.aliases();
while (enumer.hasMoreElements()) {
String alias = enumer.nextElement();
java.security.cert.Certificate cert = cacerts.getCertificate(alias);
ks.setCertificateEntry(alias, cert);
}
initssl();
}
public static void initssl() throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, ksPreplacedword.toCharArray());
SSLContext sslctx = SSLContexts.custom().loadTrustMaterial(ks).loadKeyMaterial(ks, ksPreplacedword.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
httpClientRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", sf).register("https", sslsf).build();
globalHttpClientConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setRedirectsEnabled(false).setAuthenticationEnabled(false).build();
}
public static void addCertToStore(X509Certificate cert, String alias) throws KeyStoreException {
ks.setCertificateEntry(alias, cert);
}
private static HttpCon createClient() throws Exception {
ArrayList<Header> defheaders = new ArrayList<Header>();
defheaders.add(new BasicHeader("X-Csrf-Token", "1"));
BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(httpClientRegistry);
int numSecconds = 30;
RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).setConnectTimeout(numSecconds * 1000).setConnectionRequestTimeout(numSecconds * 1000).setSocketTimeout(numSecconds * 1000).build();
CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
HttpCon con = new HttpCon();
con.setBcm(bhcm);
con.setHttp(http);
return con;
}
public static String downloadFile(String url) throws Exception {
if (url.toLowerCase().startsWith("file://")) {
return downloadFileFromFS(url);
} else {
return downloadFileFromWeb(url);
}
}
private static String downloadFileFromFS(String url) throws Exception {
URL urlObj = new URL(url);
URLConnection conn = urlObj.openConnection();
String ret = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
ret = reader.lines().collect(Collectors.joining("\n"));
}
return ret;
}
/**
* Downloads a file from the given URL
*
* @param url
* @return
* @throws IOException
*/
public static String downloadFileFromWeb(String url) throws Exception {
HttpGet get = new HttpGet(url);
HttpCon con = createClient();
try {
HttpResponse resp = con.getHttp().execute(get);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resp.getEnreplacedy().getContent(), StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
}
} finally {
try {
con.getHttp().close();
} catch (Exception e) {
// do nothig
}
con.getBcm().shutdown();
con.getBcm().close();
}
}
/**
* Determine your IP address
* @return
* @throws SocketException
*/
public static String whatsMyIP() throws SocketException {
Enumeration<NetworkInterface> enumer = NetworkInterface.getNetworkInterfaces();
while (enumer.hasMoreElements()) {
NetworkInterface ni = enumer.nextElement();
Enumeration<InetAddress> enumeri = ni.getInetAddresses();
while (enumeri.hasMoreElements()) {
InetAddress addr = enumeri.nextElement();
if (!addr.getHostAddress().startsWith("127")) {
return addr.getHostAddress();
}
}
}
return "";
}
}
19
Source : HttpMethodsImpl.java
with Mozilla Public License 2.0
from secdec
with Mozilla Public License 2.0
from secdec
private static CloseableHttpClient createHttpClient() {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
SSLConnectionSocketFactory sslConnectionSocketFactory = null;
try {
// TODO: add option in UI so user can allow or deny self signed certs
// TODO: harden this, Override isTrusted(final X509Certificate[] chain, final String authType)
sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy());
// TODO: enable hostname verification of CA generated certs. Replace with DefaultHostnameVerifier.
sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), // This is basically the same as allow all
NoopHostnameVerifier.INSTANCE);
} catch (NoSuchAlgorithmException e) {
LOGGER.error("NoSuchAlgorithmException thrown while attempting to create HttpClient", e);
} catch (KeyStoreException e) {
LOGGER.error("KeyStoreException thrown while attempting to create HttpClient", e);
} catch (KeyManagementException e) {
LOGGER.error("KeyManagementException thrown while attempting to create HttpClient", e);
}
// TODO: Allow http option for dev purposes only. Add enable/disable option in settings.
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslConnectionSocketFactory).build();
HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).setConnectionManager(cm).build();
return httpClient;
}
19
Source : HttpConnectionManager.java
with GNU General Public License v2.0
from quhailong
with GNU General Public License v2.0
from quhailong
private void init() {
LayeredConnectionSocketFactory sslsf = null;
try {
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
}
19
Source : TurSolrConfiguration.java
with GNU General Public License v3.0
from openturing
with GNU General Public License v3.0
from openturing
@Bean
public CloseableHttpClient closeableHttpClient() {
Registry<ConnectionSocketFactory> schemaRegistry = HttpClientUtil.getSchemaRegisteryProvider().getSchemaRegistry();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(schemaRegistry);
cm.setMaxTotal(10000);
cm.setDefaultMaxPerRoute(10000);
cm.setValidateAfterInactivity(3000);
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setKeepAliveStrategy((response, context) -> -1).evictIdleConnections(50000, TimeUnit.MILLISECONDS).setDefaultRequestConfig(requestConfigBuilder.build()).setRetryHandler(new SolrHttpRequestRetryHandler(0)).disableContentCompression().useSystemProperties().setConnectionManager(cm);
return httpClientBuilder.build();
}
19
Source : AirtableExecutor.java
with Apache License 2.0
from fuxingloh
with Apache License 2.0
from fuxingloh
/**
* Created by: Fuxing
* Date: 2019-04-21
* Time: 18:58
*/
public final clreplaced AirtableExecutor {
final static PoolingHttpClientConnectionManager CONNECTION_MANAGER;
final static RequestConfig REQUEST_CONFIG;
final static HttpClient CLIENT;
final static Registry<ConnectionSocketFactory> SFR;
static {
LayeredConnectionSocketFactory ssl = null;
try {
ssl = SSLConnectionSocketFactory.getSystemSocketFactory();
} catch (final SSLInitializationException ex) {
final SSLContext sslcontext;
try {
sslcontext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
sslcontext.init(null, null, null);
ssl = new SSLConnectionSocketFactory(sslcontext);
} catch (final SecurityException | KeyManagementException | NoSuchAlgorithmException ignore) {
}
}
SFR = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", ssl != null ? ssl : SSLConnectionSocketFactory.getSocketFactory()).build();
CONNECTION_MANAGER = new PoolingHttpClientConnectionManager(SFR);
// No point having too many, or else it will cause 429 error rather quickly
CONNECTION_MANAGER.setMaxTotal(8);
CONNECTION_MANAGER.setDefaultMaxPerRoute(8);
CONNECTION_MANAGER.setValidateAfterInactivity(1000);
REQUEST_CONFIG = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();
CLIENT = HttpClientBuilder.create().setConnectionManager(CONNECTION_MANAGER).setServiceUnavailableRetryStrategy(new RetryStrategy(2)).setDefaultRequestConfig(REQUEST_CONFIG).build();
}
/**
* @return default Executor with 3 maximum retry before failing
*/
public static Executor newInstance() {
return newInstance(true, 3);
}
/**
* @param autoRetry whether auto try is enabled
* @param maxRetry maximum retry before failing
* @return Executor
*/
public static Executor newInstance(boolean autoRetry, int maxRetry) {
HttpClientBuilder builder = HttpClientBuilder.create().setConnectionManager(CONNECTION_MANAGER).setDefaultRequestConfig(REQUEST_CONFIG);
if (autoRetry) {
builder.setServiceUnavailableRetryStrategy(new RetryStrategy(maxRetry));
}
return Executor.newInstance(builder.build()).use(new CookieStore());
}
/**
* @return Executor with no retry, with 100 max conneciton pool
*/
public static Executor newInstanceTurbo() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(SFR);
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setValidateAfterInactivity(1000);
HttpClientBuilder builder = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(REQUEST_CONFIG);
return Executor.newInstance(builder.build()).use(new CookieStore());
}
public static final clreplaced CookieStore implements org.apache.http.client.CookieStore {
@Override
public void addCookie(Cookie cookie) {
}
@Override
public List<Cookie> getCookies() {
return Collections.emptyList();
}
@Override
public boolean clearExpired(Date date) {
return false;
}
@Override
public void clear() {
}
}
public static final clreplaced RetryStrategy implements ServiceUnavailableRetryStrategy {
private static final Logger logger = Logger.getLogger(RetryStrategy.clreplaced.getName());
private final int maxCount;
public RetryStrategy(int maxCount) {
this.maxCount = maxCount;
}
@Override
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 429 && executionCount < maxCount) {
logger.info("429: Airtable Retry, Sleeping, Count: " + executionCount);
sleep();
logger.info("429: Airtable Retry, Resuming, Count: " + executionCount);
return true;
}
return false;
}
@Override
public long getRetryInterval() {
return 0;
}
private void sleep() {
try {
Thread.sleep(30_001);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
19
Source : ClientConnection.java
with GNU Lesser General Public License v2.1
from fossasia
with GNU Lesser General Public License v2.1
from fossasia
private static HttpClientConnectionManager getConnctionManager(boolean trustAllCerts) {
Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
if (trustAllCerts) {
try {
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), new TrustAllHostNameVerifier());
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", trustSelfSignedSocketFactory).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
DAO.severe(e);
}
}
PoolingHttpClientConnectionManager cm = (trustAllCerts && socketFactoryRegistry != null) ? new PoolingHttpClientConnectionManager(socketFactoryRegistry) : new PoolingHttpClientConnectionManager();
// twitter specific options
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
return cm;
}
19
Source : ServiceCaller.java
with Apache License 2.0
from Endava
with Apache License 2.0
from Endava
@PostConstruct
public void initHttpClient() {
try {
SSLConnectionSocketFactory sslSocketFactory = this.buildSSLContextFactory();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(100);
HttpHost httpHost = getProxyConfig();
httpClient = HttpClients.custom().setConnectionManager(connectionManager).setSSLSocketFactory(sslSocketFactory).setProxy(httpHost).build();
} catch (GeneralSecurityException | IOException e) {
LOGGER.warning("Failed to configure HTTP CLIENT", e);
}
}
19
Source : TestMiniSolrCloudClusterSSL.java
with Apache License 2.0
from apache
with Apache License 2.0
from apache
/**
* Returns a new HttpClient that supports both HTTP and HTTPS (with the default test truststore), but
* has no keystore -- so servers requiring client authentication should fail.
*/
private static CloseableHttpClient getSslAwareClientWithNoClientCerts() throws Exception {
// NOTE: This method explicitly does *NOT* use HttpClientUtil code because that
// will muck with the global static HttpClientBuilder / SchemeRegistryProvider
// and we can't do that and still test the entire purpose of what we are trying to test here.
final SSLTestConfig clientConfig = new SSLTestConfig(true, false);
final SSLConnectionSocketFactory sslFactory = clientConfig.buildClientSSLConnectionSocketFactory();
replacedert null != sslFactory;
final Registry<ConnectionSocketFactory> socketFactoryReg = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslFactory).register("http", PlainConnectionSocketFactory.INSTANCE).build();
final HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryReg));
return builder.build();
}
18
Source : ClientConnection.java
with GNU Lesser General Public License v2.1
from yacy
with GNU Lesser General Public License v2.1
from yacy
public static PoolingHttpClientConnectionManager getConnctionManager() {
Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
try {
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), new TrustAllHostNameVerifier());
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", trustSelfSignedSocketFactory).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
Data.logger.warn("", e);
}
PoolingHttpClientConnectionManager cm = (socketFactoryRegistry != null) ? new PoolingHttpClientConnectionManager(socketFactoryRegistry) : new PoolingHttpClientConnectionManager();
// twitter specific options
cm.setMaxTotal(2000);
cm.setDefaultMaxPerRoute(200);
return cm;
}
18
Source : HttpClient.java
with GNU Lesser General Public License v3.0
from WeBankFinTech
with GNU Lesser General Public License v3.0
from WeBankFinTech
/**
* On-demand, create an http client.
*
* @param proxyHost proxy host url
* @param proxyPort proxy port url
* @param proxyUsername proxy username
* @param proxyPreplacedword proxy pwd
* @return http client
* @throws Exception any exception
*/
public static CloseableHttpClient createHttpClient(String proxyHost, int proxyPort, String proxyUsername, String proxyPreplacedword) throws Exception {
try {
HttpHost proxy = null;
if (StringUtils.isNotEmpty(proxyHost)) {
proxy = new HttpHost(proxyHost, proxyPort, "http");
}
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (StringUtils.isNotEmpty(proxyUsername) && StringUtils.isNotEmpty(proxyPreplacedword)) {
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePreplacedwordCredentials(proxyUsername, proxyPreplacedword));
}
SSLContext sslcontext = createIgnoreVerifySsL();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
CloseableHttpClient client = null;
if (proxy == null) {
client = HttpClients.custom().setConnectionManager(connManager).build();
} else {
client = HttpClients.custom().setConnectionManager(connManager).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
}
return client;
} catch (Exception e) {
logger.error("onWarning: create http client error" + e);
throw e;
}
}
18
Source : HttpComponentsHttpInvokerRequestExecutor.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private static HttpClient createDefaultHttpClient() {
Registry<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(schemeRegistry);
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
return HttpClientBuilder.create().setConnectionManager(connectionManager).build();
}
18
Source : K8sUtils.java
with Apache License 2.0
from TremoloSecurity
with Apache License 2.0
from TremoloSecurity
/**
* K8sUtils
*
* Utilities for interacting with the Kubernetes API server
*/
public clreplaced K8sUtils {
String token;
private KeyStore ks;
private String ksPreplacedword;
private KeyManagerFactory kmf;
private Registry<ConnectionSocketFactory> httpClientRegistry;
private RequestConfig globalHttpClientConfig;
String url;
String pathToCaCert;
String caCert;
ScriptEngine engine;
private Map<String, Object> additionalStatuses;
private boolean openShift;
private Map<String, String> extraCerts;
static HashSet<String> processedVersions = new HashSet<String>();
boolean fromKc;
String pathToToken;
/**
* Initialization
*
* @param pathToToken
* @param pathToCA
* @param pathToMoreCerts
* @param apiServerURL
* @throws Exception
*/
public K8sUtils(String pathToToken, String pathToCA, String pathToMoreCerts, String apiServerURL) throws Exception {
// get the token for talking to k8s
this.pathToToken = pathToToken;
this.token = null;
this.fromKc = false;
this.pathToCaCert = pathToCA;
this.url = apiServerURL;
this.extraCerts = new HashMap<String, String>();
this.ksPreplacedword = UUID.randomUUID().toString();
this.ks = KeyStore.getInstance("PKCS12");
this.ks.load(null, this.ksPreplacedword.toCharArray());
if (System.getenv().get("KUBECONFIG") != null) {
this.fromKc = true;
String pathToKubeConfig = System.getenv("KUBECONFIG");
System.out.println("******* OVERRIDING WITH KUBECONFIG FROM '" + pathToKubeConfig + "' ******************");
KubeConfig kc = KubeConfig.loadKubeConfig(new InputStreamReader(new FileInputStream(pathToKubeConfig)));
String context = kc.getCurrentContext();
this.token = kc.getAccessToken();
this.url = kc.getServer();
if (token == null) {
if (kc.getClientKeyData() != null) {
String pemKey = new String(Base64.getDecoder().decode(kc.getClientKeyData()));
String pemCert = new String(Base64.getDecoder().decode(kc.getClientCertificateData()));
CertUtils.importKeyPairAndCertPem(this.ks, this.ksPreplacedword, "k8sclient", pemKey, pemCert);
}
}
if (kc.getCertificateAuthorityData() != null) {
CertUtils.importCertificate(ks, ksPreplacedword, "k8s-master", new String(Base64.getDecoder().decode(kc.getCertificateAuthorityData())));
}
} else {
caCert = new String(Files.readAllBytes(Paths.get(pathToCA)), StandardCharsets.UTF_8);
CertUtils.importCertificate(ks, ksPreplacedword, "k8s-master", caCert);
}
File moreCerts = new File(pathToMoreCerts);
if (moreCerts.exists() && moreCerts.isDirectory()) {
for (File certFile : moreCerts.listFiles()) {
System.out.println("Processing - '" + certFile.getAbsolutePath() + "'");
if (certFile.isDirectory() || !certFile.getAbsolutePath().toLowerCase().endsWith(".pem")) {
System.out.println("not a pem, skipping");
continue;
}
String certPem = new String(Files.readAllBytes(Paths.get(certFile.getAbsolutePath())), StandardCharsets.UTF_8);
String alias = certFile.getName().substring(0, certFile.getName().indexOf('.'));
this.extraCerts.put(alias, certPem);
CertUtils.importCertificate(ks, ksPreplacedword, alias, certPem);
}
}
KeyStore cacerts = KeyStore.getInstance(KeyStore.getDefaultType());
String cacertsPath = System.getProperty("javax.net.ssl.trustStore");
if (cacertsPath == null) {
cacertsPath = System.getProperty("java.home") + "/lib/security/cacerts";
}
cacerts.load(new FileInputStream(cacertsPath), null);
Enumeration<String> enumer = cacerts.aliases();
while (enumer.hasMoreElements()) {
String alias = enumer.nextElement();
java.security.cert.Certificate cert = cacerts.getCertificate(alias);
ks.setCertificateEntry(alias, cert);
}
this.kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(this.ks, this.ksPreplacedword.toCharArray());
SSLContext sslctx = SSLContexts.custom().loadTrustMaterial(this.ks).loadKeyMaterial(this.ks, this.ksPreplacedword.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
this.httpClientRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", sf).register("https", sslsf).build();
this.globalHttpClientConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setRedirectsEnabled(false).setAuthenticationEnabled(false).build();
this.openShift = false;
Map<String, Object> res = this.callWS("/apis");
String json = (String) res.get("data");
JSONParser parser = new JSONParser();
JSONObject root = (JSONObject) parser.parse(json);
JSONArray groups = (JSONArray) root.get("groups");
for (Object obj : groups) {
JSONObject group = (JSONObject) obj;
String name = (String) group.get("name");
if (name.toLowerCase().contains("openshift")) {
this.openShift = true;
break;
}
}
additionalStatuses = new HashMap<String, Object>();
}
/**
* Generate an HTTP client pre-configured with the container's service-account
* token and trust of the api server's certificate
*
* @return
* @throws Exception
*/
public HttpCon createClient() throws Exception {
ArrayList<Header> defheaders = new ArrayList<Header>();
defheaders.add(new BasicHeader("X-Csrf-Token", "1"));
BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(this.httpClientRegistry);
RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
HttpCon con = new HttpCon();
con.setBcm(bhcm);
con.setHttp(http);
return con;
}
/**
* Call a kubernetes web service via GET
*
* @param uri
* @return
* @throws Exception
*/
public Map callWS(String uri) throws Exception {
return callWS(uri, null, 10);
}
/**
* Watch a Kubernetes object based on its URI
*
* @param uri
* @throws Exception
*/
public void watchURI(String uri, String functionName) throws Exception {
K8sUtils localK8s = new K8sUtils(Controller.tokenPath, Controller.rootCaPath, Controller.configMaps, Controller.kubernetesURL);
ScriptEngine localEngine = Controller.initializeJS(Controller.jsPath, Controller.namespace, localK8s);
localK8s.setEngine(localEngine);
StringBuffer b = new StringBuffer();
System.out.println(uri);
System.out.println("Is OpenShift : " + localK8s.isOpenShift());
b.append(this.getK8sUrl()).append(uri);
HttpGet get = new HttpGet(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
get.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(get);
BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEnreplacedy().getContent()));
String line = null;
while ((line = in.readLine()) != null) {
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(line);
JSONObject cr = (JSONObject) json.get("object");
JSONObject chkObj = new JSONObject();
chkObj.put("apiVersion", cr.get("apiVersion"));
chkObj.put("kind", cr.get("kind"));
chkObj.put("spec", cr.get("spec"));
JSONObject metadata = (JSONObject) parser.parse(((JSONObject) cr.get("metadata")).toJSONString());
String resourceVersion = (String) metadata.get("resourceVersion");
if (resourceVersion == null) {
System.out.println("unexpected json - " + line);
throw new Exception("No resourceVersion, restartinig watch");
}
System.out.println("Resource Version - " + resourceVersion + " - " + processedVersions.contains(resourceVersion));
if (processedVersions.contains(resourceVersion)) {
System.out.println("Resource - " + resourceVersion + " - already processed, skipping");
continue;
} else {
processedVersions.add(resourceVersion);
}
metadata.remove("creationTimestamp");
metadata.remove("generation");
metadata.remove("resourceVersion");
metadata.remove("managedFields");
chkObj.put("metadata", metadata);
String jsonForChecksum = chkObj.toJSONString();
MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");
digest.update(jsonForChecksum.getBytes("UTF-8"), 0, jsonForChecksum.getBytes("UTF-8").length);
byte[] digestBytes = digest.digest();
String digestBase64 = java.util.Base64.getEncoder().encodeToString(digestBytes);
if (json.get("type").equals("MODIFIED")) {
if (json.get("object") != null && ((JSONObject) json.get("object")).get("status") != null && ((JSONObject) ((JSONObject) json.get("object")).get("status")).get("digest") != null) {
String existingDigest = (String) ((JSONObject) ((JSONObject) json.get("object")).get("status")).get("digest");
if (existingDigest.equals(digestBase64)) {
continue;
}
}
}
Invocable invocable = (Invocable) localEngine;
boolean error = false;
String result = null;
try {
result = (String) invocable.invokeFunction(functionName, line);
} catch (Throwable t) {
System.err.println("Error on watch - " + uri);
t.printStackTrace(System.err);
error = true;
}
if (error) {
if (result != null) {
result = "error-" + result;
} else {
result = "error";
}
}
if (json.get("type").equals("MODIFIED") || json.get("type").equals("ADDED")) {
JSONObject patch = this.generateJsonStatus(result, digestBase64, localK8s.getAdditionalStatuses());
cr.put("status", patch);
String selfLink = (String) ((JSONObject) ((JSONObject) json.get("object")).get("metadata")).get("selfLink");
this.putWS(selfLink + "/status", cr.toJSONString());
}
}
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
private JSONObject generateJsonStatus(String errorMessage, String digest, Map<String, Object> additionalStatuses) {
JSONObject patch = new JSONObject();
patch.put("conditions", new JSONObject());
((JSONObject) patch.get("conditions")).put("lastTransitionTime", DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ssz").print(new DateTime()));
patch.put("digest", digest);
if (errorMessage == null) {
((JSONObject) patch.get("conditions")).put("status", "True");
((JSONObject) patch.get("conditions")).put("type", "Completed");
} else {
((JSONObject) patch.get("conditions")).put("status", "True");
((JSONObject) patch.get("conditions")).put("type", "Failed");
((JSONObject) patch.get("conditions")).put("status", "True");
((JSONObject) patch.get("conditions")).put("reason", errorMessage);
}
for (String extraStatus : additionalStatuses.keySet()) {
this.addToJson(patch, extraStatus, additionalStatuses.get(extraStatus));
}
return patch;
}
private void addToJson(JSONObject root, String name, Object value) {
if (value instanceof String) {
root.put(name, value);
} else if (value instanceof Map) {
JSONObject newRoot = new JSONObject();
root.put(name, newRoot);
Map<String, Object> rootSet = (Map<String, Object>) value;
for (String keyName : rootSet.keySet()) {
addToJson(newRoot, keyName, rootSet.get(keyName));
}
} else {
System.out.println("Unknown type" + value);
}
}
/**
* GET an API via its URI, with a test function for success and a number of
* attempted retries
*
* @param uri
* @param testFunction
* @param count
* @return
* @throws Exception
*/
public Map callWS(String uri, String testFunction, int count) throws Exception {
StringBuffer b = new StringBuffer();
b.append(this.getK8sUrl()).append(uri);
HttpGet get = new HttpGet(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
get.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(get);
String json = EnreplacedyUtils.toString(resp.getEnreplacedy());
Map ret = new HashMap();
ret.put("code", resp.getStatusLine().getStatusCode());
ret.put("data", json);
if (count >= 0 && (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() > 299)) {
System.err.println("Problem calling '" + uri + "' - " + resp.getStatusLine().getStatusCode());
System.err.println(json);
if (count > 0) {
System.err.println("Sleeping, then trying again");
Thread.sleep(10000);
System.err.println("trying again");
return callWS(uri, testFunction, --count);
}
}
if (testFunction != null && !testFunction.isEmpty()) {
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("check_ws_response", false);
engine.getBindings(ScriptContext.ENGINE_SCOPE).put("ws_response_json", json);
try {
engine.eval(testFunction);
} catch (Throwable t) {
System.err.println("Unable to verify '" + uri + "' / " + json);
t.printStackTrace();
if (count > 0) {
System.err.println("Sleeping, then trying again");
Thread.sleep(10000);
System.err.println("trying again");
return callWS(uri, testFunction, --count);
}
}
if (!((Boolean) engine.getBindings(ScriptContext.ENGINE_SCOPE).get("check_ws_response"))) {
System.err.println("Verification for '" + uri + "' failed / " + json);
if (count > 0) {
System.err.println("Sleeping, then trying again");
Thread.sleep(10000);
System.err.println("trying again");
return callWS(uri, testFunction, --count);
}
}
}
return ret;
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
public Map deleteWS(String uri) throws Exception {
return deleteWS(uri, true);
}
/**
* DELETE a Kubernetes object
*
* @param uri
* @param ignoreNotFound
* @return
* @throws Exception
*/
public Map deleteWS(String uri, boolean ignoreNotFound) throws Exception {
StringBuffer b = new StringBuffer();
b.append(this.getK8sUrl()).append(uri);
HttpDelete delete = new HttpDelete(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
delete.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(delete);
String json = EnreplacedyUtils.toString(resp.getEnreplacedy());
Map ret = new HashMap();
ret.put("code", resp.getStatusLine().getStatusCode());
ret.put("data", json);
if (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() > 299) {
if (!(resp.getStatusLine().getStatusCode() == 404 && ignoreNotFound)) {
System.err.println("Problem calling '" + uri + "' - " + resp.getStatusLine().getStatusCode());
System.err.println(json);
}
}
return ret;
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
/**
* POST to an API URI
*
* @param uri
* @param json
* @return
* @throws Exception
*/
public Map postWS(String uri, String json) throws Exception {
StringBuffer b = new StringBuffer();
b.append(this.getK8sUrl()).append(uri);
HttpPost post = new HttpPost(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
post.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
StringEnreplacedy str = new StringEnreplacedy(json, ContentType.APPLICATION_JSON);
post.setEnreplacedy(str);
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(post);
String jsonResponse = EnreplacedyUtils.toString(resp.getEnreplacedy());
Map ret = new HashMap();
ret.put("code", resp.getStatusLine().getStatusCode());
ret.put("data", jsonResponse);
if (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() > 299) {
System.err.println("Problem calling '" + uri + "' - " + resp.getStatusLine().getStatusCode());
System.err.println(json);
}
return ret;
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
/**
* PUT to a URI
*
* @param uri
* @param json
* @return
* @throws Exception
*/
public Map patchWS(String uri, String json) throws Exception {
StringBuffer b = new StringBuffer();
b.append(this.getK8sUrl()).append(uri);
HttpPatch patch = new HttpPatch(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
patch.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
patch.setEnreplacedy(EnreplacedyBuilder.create().setContentType(ContentType.create("application/merge-patch+json")).setText(json).build());
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(patch);
String jsonResponse = EnreplacedyUtils.toString(resp.getEnreplacedy());
Map ret = new HashMap();
ret.put("code", resp.getStatusLine().getStatusCode());
ret.put("data", jsonResponse);
if (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() > 299) {
System.err.println("Problem calling '" + uri + "' - " + resp.getStatusLine().getStatusCode());
System.err.println(json);
}
return ret;
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
/**
* PUT to a URI
*
* @param uri
* @param json
* @return
* @throws Exception
*/
public Map putWS(String uri, String json) throws Exception {
StringBuffer b = new StringBuffer();
b.append(this.getK8sUrl()).append(uri);
HttpPut post = new HttpPut(b.toString());
String ltoken = this.getAuthorizationToken();
if (ltoken != null) {
b.setLength(0);
b.append("Bearer ").append(ltoken);
post.addHeader(new BasicHeader("Authorization", "Bearer " + ltoken));
}
StringEnreplacedy str = new StringEnreplacedy(json, ContentType.APPLICATION_JSON);
post.setEnreplacedy(str);
HttpCon con = this.createClient();
try {
HttpResponse resp = con.getHttp().execute(post);
String jsonResponse = EnreplacedyUtils.toString(resp.getEnreplacedy());
Map ret = new HashMap();
ret.put("code", resp.getStatusLine().getStatusCode());
ret.put("data", jsonResponse);
if (resp.getStatusLine().getStatusCode() < 200 || resp.getStatusLine().getStatusCode() > 299) {
System.err.println("Problem calling '" + uri + "' - " + resp.getStatusLine().getStatusCode());
System.err.println(jsonResponse);
}
return ret;
} finally {
if (con != null) {
con.getBcm().shutdown();
}
}
}
/**
* Returns a certificate from the internal keystore
*
* @param name
* @return
* @throws KeyStoreException
*/
public X509Certificate getCertificate(String name) throws KeyStoreException {
return (X509Certificate) this.ks.getCertificate(name);
}
/**
* Base64 encode a Map of name/value pairs
*
* @param data
* @return
* @throws UnsupportedEncodingException
*/
public String encodeMap(Map data) throws UnsupportedEncodingException {
String vals = "";
for (Object k : data.keySet()) {
vals += k + "=" + data.get(k) + "\n";
}
vals = vals.substring(0, vals.length() - 1);
return Base64.getEncoder().encodeToString(vals.getBytes("UTF-8"));
}
/**
* Simple template processor replacing name/value pairs from the map to anything
* enclused in #[] so #[MY_VALUE] would be replaced with the value from the map
* replacedociated with the key MY_VALUE
*
* @param template
* @param vars
* @return
*/
public String processTemplate(String template, Map vars) {
StringBuffer newConfig = new StringBuffer();
newConfig.setLength(0);
int begin, end;
begin = 0;
end = 0;
String finalCfg = null;
begin = template.indexOf("#[");
while (begin > 0) {
if (end == 0) {
newConfig.append(template.substring(0, begin));
} else {
newConfig.append(template.substring(end, begin));
}
end = template.indexOf(']', begin + 2);
String envVarName = template.substring(begin + 2, end);
String value = (String) vars.get(envVarName);
if (value == null) {
value = "";
}
newConfig.append(value);
begin = template.indexOf("#[", end + 1);
end++;
}
if (end != 0) {
newConfig.append(template.substring(end));
}
return newConfig.toString();
}
/**
* Run kubectl create with the data preplaceded in using the service account of the
* container
*
* @param data
* @throws IOException
* @throws InterruptedException
*/
public void kubectlCreate(String data) throws IOException, InterruptedException {
String ltoken = this.getAuthorizationToken();
Process p = Runtime.getRuntime().exec(new String[] { "kubectl", "--token=" + ltoken, "--server=" + this.getK8sUrl(), "--certificate-authority=" + this.pathToCaCert, "create", "-f", "-" });
new Thread() {
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
try {
while ((line = in.readLine()) != null) {
System.err.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
PrintStream out = new PrintStream(p.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data.getBytes("UTF-8"))));
String line;
while ((line = in.readLine()) != null) {
out.println(line);
}
out.close();
System.out.println("waiting for completion");
p.waitFor();
}
/**
* @param engine the engine to set
*/
public void setEngine(ScriptEngine engine) {
this.engine = engine;
}
/**
* @return the engine
*/
public ScriptEngine getEngine() {
return engine;
}
public String json2yaml(String json) throws IOException {
JsonNode jsonNodeTree = new ObjectMapper().readTree(json);
String jsonAsYaml = new YAMLMapper().writeValuereplacedtring(jsonNodeTree);
return jsonAsYaml;
}
public String getCaCert() {
return this.caCert;
}
public boolean isOpenShift() {
return this.openShift;
}
/**
* @return the additionalStatuses
*/
public Map<String, Object> getAdditionalStatuses() {
return additionalStatuses;
}
public KeyStore getKs() {
return this.ks;
}
public String getKsPreplacedword() {
return this.ksPreplacedword;
}
public Map<String, String> getExtraCerts() {
return this.extraCerts;
}
public String getK8sUrl() {
if (this.url.equalsIgnoreCase("https://kubernetes.default.svc.cluster.local")) {
// we want to use the default URL. Instead of using it, we'll build it from
// the environment variables
return new StringBuilder().append("https://").append(System.getenv("KUBERNETES_SERVICE_HOST")).append(":").append(System.getenv("KUBERNETES_SERVICE_PORT")).toString();
} else {
return this.url;
}
}
public String getAuthorizationToken() throws IOException {
if (this.fromKc) {
return this.token;
} else {
return new String(Files.readAllBytes(Paths.get(pathToToken)), StandardCharsets.UTF_8);
}
}
}
18
Source : HttpClientFactory.java
with Apache License 2.0
from personium
with Apache License 2.0
from personium
/**
* HTTPClientオブジェクトを作成.
* @param type 通信タイプ
* @param connectionTimeout タイムアウト値(ミリ秒)。0の場合はデフォルト値を利用する。
* @return 作成したHttpClientクラスインスタンス
*/
public static HttpClient create(final String type, final int connectionTimeout) {
int timeout = TIMEOUT;
if (connectionTimeout != 0) {
timeout = connectionTimeout;
}
RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).setRedirectsEnabled(false).build();
if (TYPE_DEFAULT.equalsIgnoreCase(type)) {
return HttpClientBuilder.create().setDefaultRequestConfig(config).build();
} else if (!TYPE_INSECURE.equalsIgnoreCase(type)) {
return null;
}
SSLConnectionSocketFactory sf = null;
try {
sf = createInsecureSSLConnectionSocketFactory();
} catch (Exception e) {
return null;
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf).register("http", PlainConnectionSocketFactory.INSTANCE).build();
HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry);
HttpClient hc = HttpClientBuilder.create().setDefaultRequestConfig(config).setConnectionManager(cm).build();
return hc;
}
18
Source : HttpClientFactory.java
with Apache License 2.0
from personium
with Apache License 2.0
from personium
private static CloseableHttpClient createInsecure(RequestConfig config) {
SSLConnectionSocketFactory sf = null;
try {
sf = createInsecureSSLConnectionSocketFactory();
} catch (Exception e) {
return null;
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf).register("http", PlainConnectionSocketFactory.INSTANCE).build();
HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry);
return HttpClientBuilder.create().setDefaultRequestConfig(config).setConnectionManager(cm).useSystemProperties().build();
}
18
Source : HttpClientFactory.java
with Apache License 2.0
from personium
with Apache License 2.0
from personium
private static CloseableHttpClient createAlwaysLocal(RequestConfig config) {
SSLConnectionSocketFactory sf = null;
try {
sf = createInsecureSSLConnectionSocketFactory();
} catch (Exception e) {
return null;
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sf).register("http", PlainConnectionSocketFactory.INSTANCE).build();
/* Custom DNS resolver */
DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
// Always 127.0.0.1
return new InetAddress[] { InetAddress.getLoopbackAddress() };
}
};
HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry, null, /* Default ConnectionFactory */
null, /* Default SchemePortResolver */
dnsResolver);
return HttpClientBuilder.create().setDefaultRequestConfig(config).setConnectionManager(cm).useSystemProperties().build();
}
18
Source : WebhookMsgHandler.java
with Apache License 2.0
from osswangxining
with Apache License 2.0
from osswangxining
/**
* Create HttpClient with SSL
*/
private static CloseableHttpClient createHttpOrHttpsClient(String url) {
PoolingHttpClientConnectionManager cm = null;
if (url != null && url.trim().toLowerCase().startsWith("https")) {
Registry<ConnectionSocketFactory> sslSocketFactoryRegistry = createAcceptAllSSLSocketFactoryRegistry();
cm = new PoolingHttpClientConnectionManager(sslSocketFactoryRegistry);
} else {
cm = new PoolingHttpClientConnectionManager();
}
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
Builder scBuilder = SocketConfig.copy(SocketConfig.DEFAULT);
scBuilder.setSoTimeout(10000);
cm.setDefaultSocketConfig(scBuilder.build());
return HttpClients.custom().setKeepAliveStrategy(myStrategy).setConnectionManager(cm).build();
}
18
Source : HttpClientTest.java
with Apache License 2.0
from NetCrusherOrg
with Apache License 2.0
from NetCrusherOrg
@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);
}
18
Source : HttpProxyClient.java
with Apache License 2.0
from Intel-bigdata
with Apache License 2.0
from Intel-bigdata
private PoolingNHttpClientConnectionManager getAsyncConnectionManager() {
ConnectingIOReactor ioReactor = null;
PoolingNHttpClientConnectionManager cm = null;
try {
ioReactor = new DefaultConnectingIOReactor();
// ssl setup
SSLContext sslcontext = SSLContexts.createSystemDefault();
X509HostnameVerifier hostnameVerifier = new BrowserCompatHostnameVerifier();
@SuppressWarnings("deprecation")
Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE).register("https", new SSLIOSessionStrategy(sslcontext, hostnameVerifier)).build();
cm = new PoolingNHttpClientConnectionManager(ioReactor, sessionStrategyRegistry);
} catch (IOReactorException e) {
LOG.error("Couldn't initialize multi-threaded async client ", e);
return null;
}
return cm;
}
18
Source : HttpClient.java
with GNU Lesser General Public License v2.1
from fossasia
with GNU Lesser General Public License v2.1
from fossasia
public static PoolingHttpClientConnectionManager getConnctionManager(boolean useAuthentication) {
Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
/*
SSLConnectionSocketFactory trustSelfSignedSocketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
new TrustAllHostNameVerifier());
*/
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new PlainConnectionSocketFactory()).register("https", getSSLSocketFactory()).build();
PoolingHttpClientConnectionManager cm = socketFactoryRegistry != null ? new PoolingHttpClientConnectionManager(socketFactoryRegistry) : new PoolingHttpClientConnectionManager();
// twitter specific options
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
HttpHost twitter = new HttpHost("twitter.com", 443);
cm.setMaxPerRoute(new HttpRoute(twitter), 50);
return cm;
}
18
Source : HttpClientPool.java
with Apache License 2.0
from FederatedAI
with Apache License 2.0
from FederatedAI
public static void initPool() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(Dict.HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(Dict.HTTPS, sslsf).build();
poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolConnManager.setMaxTotal(500);
poolConnManager.setDefaultMaxPerRoute(200);
int socketTimeout = 10000;
int connectTimeout = 10000;
int connectionRequestTimeout = 10000;
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpClient = getConnection();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
logger.error("init http client pool failed:", ex);
}
}
18
Source : RestTemplateConfig.java
with Apache License 2.0
from etrace-io
with Apache License 2.0
from etrace-io
@Bean
public ClientHttpRequestFactory httpRequestFactory() {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(2);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(3000).build();
HttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
18
Source : HttpConnectionFactoryImpl.java
with Apache License 2.0
from capitalone
with Apache License 2.0
from capitalone
private HttpConnectionPool newConnectionPool(HttpConnectionConfig connectionConfig) {
SSLContext sslContext;
try {
sslContext = SSLContexts.custom().useProtocol(connectionConfig.getSslProtocol()).build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new SSLContextException(String.format("No such SSL protocol: %s", connectionConfig.getSslProtocol()), e);
}
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(sslContext)).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(connectionConfig.getMaxHttpConnections());
return new HttpConnectionPool(connectionManager, connectionConfig);
}
18
Source : HTTPSClient.java
with Apache License 2.0
from BSNDA
with Apache License 2.0
from BSNDA
/**
* 注册协议和端口, 此方法也可以被子类重写
*/
protected void regist() {
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", this.connectionSocketFactory).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 创建自定义的httpclient对象
this.client = HttpClients.custom().setConnectionManager(connManager).build();
}
18
Source : HttpDataAdapter.java
with Apache License 2.0
from binjr
with Apache License 2.0
from binjr
protected CloseableHttpClient httpClientFactory() throws CannotInitializeDataAdapterException {
try {
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(createSslCustomContext(), null, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
RegistryBuilder<AuthSchemeProvider> schemeProviderBuilder = RegistryBuilder.create();
schemeProviderBuilder.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() {
@Override
public Principal getUserPrincipal() {
return null;
}
@Override
public String getPreplacedword() {
return null;
}
});
var clientBuilder = HttpClients.custom().setDefaultAuthSchemeRegistry(schemeProviderBuilder.build()).setDefaultCredentialsProvider(credsProvider).setSSLSocketFactory(csf).setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());
if (UserPreferences.getInstance().httpPoolingEnabled.get()) {
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", csf).register("http", new PlainConnectionSocketFactory()).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setDefaultMaxPerRoute(16);
cm.setMaxTotal(100);
clientBuilder.setConnectionManager(cm);
logger.trace("Http client connection pooling is enabled");
}
return clientBuilder.build();
} catch (Exception e) {
throw new CannotInitializeDataAdapterException("Could not initialize adapter to source '" + this.getSourceName() + "': " + e.getMessage(), e);
}
}
18
Source : AbstractHttpCertSigner.java
with Apache License 2.0
from AthenZ
with Apache License 2.0
from AthenZ
/**
* Create a http client connection manager based on given ssl context
* @param sslContext ssl context containing keystore with client key/cert
* @return connection manager object
*/
PoolingHttpClientConnectionManager createConnectionPooling(SSLContext sslContext) {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
// route is host + port. Since we have only one, set the max and the route the same
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_POOL_PER_ROUTE);
poolingHttpClientConnectionManager.setMaxTotal(DEFAULT_MAX_POOL_TOTAL);
return poolingHttpClientConnectionManager;
}
18
Source : HttpDriver.java
with Apache License 2.0
from AthenZ
with Apache License 2.0
from AthenZ
protected PoolingHttpClientConnectionManager createConnectionPooling(SSLContext sslContext) {
if (sslContext == null) {
return null;
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", new SSLConnectionSocketFactory(sslContext)).register("http", new PlainConnectionSocketFactory()).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
// route is host + port. Since we have only one, set the max and the route the same
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(maxPoolPerRoute);
poolingHttpClientConnectionManager.setMaxTotal(maxPoolTotal);
return poolingHttpClientConnectionManager;
}
18
Source : HttpSyncClient.java
with Apache License 2.0
from 760515805
with Apache License 2.0
from 760515805
public CloseableHttpClient createClient() {
try {
// 添加对https的支持,该sslContext没有加载客户端证书
// 如果需要加载客户端证书,请使用如下sslContext,其中KEYSTORE_FILE和KEYSTORE_PreplacedWORD分别是你的证书路径和证书密码
// KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()
// FileInputStream instream = new FileInputStream(new File(KEYSTORE_FILE));
// keyStore.load(instream, KEYSTORE_PreplacedWORD.toCharArray());
// SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore,KEYSTORE_PreplacedWORD.toCharArray())
// .loadTrustMaterial(null, new TrustSelfSignedStrategy())
// .build();
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(maxConnNum);
cm.setDefaultMaxPerRoute(maxPerRoute);
} catch (Exception e) {
e.printStackTrace();
}
requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout).setConnectionRequestTimeout(5000).setSocketTimeout(socketTimeout).setRedirectsEnabled(true).build();
return HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
}
18
Source : HttpClientFactory.java
with Apache License 2.0
from 1991wangliang
with Apache License 2.0
from 1991wangliang
public static CloseableHttpClient createHttpClient() {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
ConnectionSocketFactory sslsf = new EasySSLConnectionSocketFactory();
// SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加到200
cm.setMaxTotal(200);
// 将每个路由基础的连接增加到20
cm.setDefaultMaxPerRoute(20);
// 请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {
// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {
// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {
// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {
// 超时
return false;
}
if (exception instanceof UnknownHostException) {
// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {
// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {
// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEnreplacedyEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
return httpClient;
}
17
Source : EchoConfig.java
with MIT License
from yizzuide
with MIT License
from yizzuide
@Bean
public CloseableHttpClient closeableHttpClient() {
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04"));
headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
// 配置HTTPS支持
Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
SSLContext sslContext = null;
try {
// 1. setup a Trust Strategy that allows all certificates.
TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
// 2. don't check Host names, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// 3. here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
} catch (Exception e) {
log.error("Echo create httpClient SSL error", e);
}
// 4. now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(Objects.requireNonNull(socketFactoryRegistry));
;
// 连接池最大连接数
poolingConnectionManager.setMaxTotal(echoProperties.getPoolMaxSize());
// 每个路由的并发
poolingConnectionManager.setDefaultMaxPerRoute(echoProperties.getDefaultMaxPerRoute());
// 自定义连接管理
return HttpClients.custom().setConnectionManager(poolingConnectionManager).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = echoProperties.getKeepAlive().toMillis();
}
return keepAlive;
}
}).setRetryHandler(new DefaultHttpRequestRetryHandler(echoProperties.getRetryCount(), echoProperties.isEnableRequestSentRetry())).setSSLContext(sslContext).setDefaultHeaders(headers).build();
}
17
Source : StatefulHttpClient.java
with Apache License 2.0
from whirlys
with Apache License 2.0
from whirlys
private void initCookieStore() {
PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
Registry<CookieSpecProvider> cookieSpecReg = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher)).register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider(publicSuffixMatcher)).build();
CookieStore cookieStore = new BasicCookieStore();
context = HttpClientContext.create();
context.setCookieSpecRegistry(cookieSpecReg);
context.setCookieStore(cookieStore);
}
17
Source : IgnoreSSLUtils.java
with Apache License 2.0
from taoweiji
with Apache License 2.0
from taoweiji
/**
* for ignoring verify SSL
*/
public static CloseableHttpClient createClient() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext sslcontext = createIgnoreVerifySSL();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sslcontext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
return HttpClients.custom().setConnectionManager(connManager).build();
}
17
Source : WechatHttpClientUtil.java
with Apache License 2.0
from openhkr
with Apache License 2.0
from openhkr
/**
* 加载证书并组装连接池
* @param keyStore
* @param certPwd
* @return
*/
private static CloseableHttpClient getHttpClient(KeyStore keyStore, String certPwd) {
CloseableHttpClient httpClient = null;
try {
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, certPwd.toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, new DefaultHostnameVerifier());
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("Content-Type", "text/xml"));
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(1000).setSocketTimeout(3000).build();
httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setDefaultHeaders(headers).setConnectionManager(cm).build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException | UnrecoverableKeyException e) {
log.error("", e);
throw new HkrServerException();
}
return httpClient;
}
17
Source : HttpClientUtil.java
with MIT License
from NerveNetwork
with MIT License
from NerveNetwork
/**
* 创建HttpClient对象
*
* @return
* @author SHANHY
* @create 2015年12月18日
*/
public static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
HttpHost httpHost = new HttpHost(hostname, port);
// 将目标主机的最大连接数增加
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 3) {
// 如果已经重试了3次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {
// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {
// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {
// 超时
return false;
}
if (exception instanceof UnknownHostException) {
// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {
// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {
// SSL握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEnreplacedyEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
return httpClient;
}
17
Source : HttpRequestUtil.java
with GNU Affero General Public License v3.0
from nekoteaparty
with GNU Affero General Public License v3.0
from nekoteaparty
private static synchronized void initClient() {
if (client != null) {
try {
client.close();
connectionManager.shutdown();
} catch (IOException ignore) {
}
}
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new ProxyConnectionSocketFactory()).register("https", new ProxySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();
connectionManager = new PoolingHttpClientConnectionManager(reg);
connectionManager.setMaxTotal(1000);
connectionManager.setDefaultMaxPerRoute(50);
client = HttpClients.custom().setConnectionManager(connectionManager).setConnectionManagerShared(true).build();
}
17
Source : HttpClientAdapter.java
with GNU General Public License v2.0
from mguessan
with GNU General Public License v2.0
from mguessan
public clreplaced HttpClientAdapter implements Closeable {
static final Logger LOGGER = Logger.getLogger("davmail.http.HttpClientAdapter");
static final String[] SUPPORTED_PROTOCOLS = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
static final Registry<ConnectionSocketFactory> SCHEME_REGISTRY;
static String WORKSTATION_NAME = "UNKNOWN";
static final int MAX_REDIRECTS = 10;
static {
// disable Client-initiated TLS renegotiation
System.setProperty("jdk.tls.rejectClientInitiatedRenegotiation", "true");
// force strong ephemeral Diffie-Hellman parameter
System.setProperty("jdk.tls.ephemeralDHKeySize", "2048");
Security.setProperty("ssl.SocketFactory.provider", "davmail.http.DavGatewaySSLSocketFactory");
// reenable basic proxy authentication on Java >= 1.8.111
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
RegistryBuilder<ConnectionSocketFactory> schemeRegistry = RegistryBuilder.create();
schemeRegistry.register("http", new PlainConnectionSocketFactory());
schemeRegistry.register("https", new SSLConnectionSocketFactory(new DavGatewaySSLSocketFactory(), SUPPORTED_PROTOCOLS, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()));
SCHEME_REGISTRY = schemeRegistry.build();
try {
WORKSTATION_NAME = InetAddress.getLocalHost().getHostName();
} catch (Throwable t) {
// ignore
}
// set system property *before* calling ProxySelector.getDefault()
if (Settings.getBooleanProperty("davmail.useSystemProxies", Boolean.FALSE)) {
System.setProperty("java.net.useSystemProxies", "true");
}
ProxySelector.setDefault(new DavGatewayProxySelector(ProxySelector.getDefault()));
}
/**
* Test if the response is gzip encoded
*
* @param response http response
* @return true if response is gzip encoded
*/
public static boolean isGzipEncoded(HttpResponse response) {
Header header = response.getFirstHeader("Content-Encoding");
return header != null && "gzip".equals(header.getValue());
}
HttpClientConnectionManager connectionManager;
CloseableHttpClient httpClient;
CredentialsProvider provider = new BasicCredentialsProvider();
BasicCookieStore cookieStore = new BasicCookieStore() {
@Override
public void addCookie(final Cookie cookie) {
LOGGER.debug("Add cookie " + cookie);
super.addCookie(cookie);
}
};
// current URI
URI uri;
String domain;
String userid;
String userEmail;
public HttpClientAdapter(String url) {
this(URI.create(url));
}
public HttpClientAdapter(String url, String username, String preplacedword) {
this(URI.create(url), username, preplacedword, false);
}
public HttpClientAdapter(String url, boolean enablePool) {
this(URI.create(url), null, null, enablePool);
}
public HttpClientAdapter(String url, String username, String preplacedword, boolean enablePool) {
this(URI.create(url), username, preplacedword, enablePool);
}
public HttpClientAdapter(URI uri) {
this(uri, null, null, false);
}
public HttpClientAdapter(URI uri, boolean enablePool) {
this(uri, null, null, enablePool);
}
public HttpClientAdapter(URI uri, String username, String preplacedword) {
this(uri, username, preplacedword, false);
}
public HttpClientAdapter(URI uri, String username, String preplacedword, boolean enablePool) {
// init current uri
this.uri = uri;
if (enablePool) {
connectionManager = new PoolingHttpClientConnectionManager(SCHEME_REGISTRY);
((PoolingHttpClientConnectionManager) connectionManager).setDefaultMaxPerRoute(5);
startEvictorThread();
} else {
connectionManager = new BasicHttpClientConnectionManager(SCHEME_REGISTRY);
}
HttpClientBuilder clientBuilder = HttpClientBuilder.create().disableRedirectHandling().setDefaultRequestConfig(getRequestConfig()).setUserAgent(getUserAgent()).setDefaultAuthSchemeRegistry(getAuthSchemeRegistry()).disableConnectionState().setConnectionManager(connectionManager);
SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
clientBuilder.setRoutePlanner(routePlanner);
clientBuilder.setDefaultCookieStore(cookieStore);
setCredentials(username, preplacedword);
boolean enableProxy = Settings.getBooleanProperty("davmail.enableProxy");
boolean useSystemProxies = Settings.getBooleanProperty("davmail.useSystemProxies", Boolean.FALSE);
String proxyHost = null;
int proxyPort = 0;
String proxyUser = null;
String proxyPreplacedword = null;
if (useSystemProxies) {
// get proxy for url from system settings
System.setProperty("java.net.useSystemProxies", "true");
List<Proxy> proxyList = getProxyForURI(uri);
if (!proxyList.isEmpty() && proxyList.get(0).address() != null) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) proxyList.get(0).address();
proxyHost = inetSocketAddress.getHostName();
proxyPort = inetSocketAddress.getPort();
// we may still need authentication credentials
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPreplacedword = Settings.getProperty("davmail.proxyPreplacedword");
}
} else if (isNoProxyFor(uri)) {
LOGGER.debug("no proxy for " + uri.getHost());
} else if (enableProxy) {
proxyHost = Settings.getProperty("davmail.proxyHost");
proxyPort = Settings.getIntProperty("davmail.proxyPort");
proxyUser = Settings.getProperty("davmail.proxyUser");
proxyPreplacedword = Settings.getProperty("davmail.proxyPreplacedword");
}
if (proxyHost != null && proxyHost.length() > 0) {
if (proxyUser != null && proxyUser.length() > 0) {
AuthScope authScope = new AuthScope(proxyHost, proxyPort, AuthScope.ANY_REALM);
if (provider == null) {
provider = new BasicCredentialsProvider();
}
// detect ntlm authentication (windows domain name in user name)
int backslashindex = proxyUser.indexOf('\\');
if (backslashindex > 0) {
provider.setCredentials(authScope, new NTCredentials(proxyUser.substring(backslashindex + 1), proxyPreplacedword, WORKSTATION_NAME, proxyUser.substring(0, backslashindex)));
} else {
provider.setCredentials(authScope, new NTCredentials(proxyUser, proxyPreplacedword, WORKSTATION_NAME, ""));
}
}
}
clientBuilder.setDefaultCredentialsProvider(provider);
httpClient = clientBuilder.build();
}
/**
* Get current uri host
*
* @return current host
*/
public String getHost() {
return uri.getHost();
}
/**
* Force current uri.
*
* @param uri new uri
*/
public void setUri(URI uri) {
this.uri = uri;
}
/**
* Current uri.
*
* @return current uri
*/
public URI getUri() {
return uri;
}
private Registry<AuthSchemeProvider> getAuthSchemeRegistry() {
final RegistryBuilder<AuthSchemeProvider> registryBuilder = RegistryBuilder.create();
registryBuilder.register(AuthSchemes.NTLM, new JCIFSNTLMSchemeFactory()).register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory());
if (Settings.getBooleanProperty("davmail.enableKerberos")) {
registryBuilder.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).register(AuthSchemes.KERBEROS, new KerberosSchemeFactory());
}
return registryBuilder.build();
}
private RequestConfig getRequestConfig() {
HashSet<String> authSchemes = new HashSet<>();
authSchemes.add(AuthSchemes.NTLM);
authSchemes.add(AuthSchemes.BASIC);
authSchemes.add(AuthSchemes.DIGEST);
if (Settings.getBooleanProperty("davmail.enableKerberos")) {
authSchemes.add(AuthSchemes.SPNEGO);
authSchemes.add(AuthSchemes.KERBEROS);
}
return RequestConfig.custom().setConnectTimeout(Settings.getIntProperty("davmail.exchange.connectionTimeout", 10) * 1000).setSocketTimeout(Settings.getIntProperty("davmail.exchange.soTimeout", 120) * 1000).setTargetPreferredAuthSchemes(authSchemes).build();
}
private void parseUserName(String username) {
if (username != null) {
int pipeIndex = username.indexOf("|");
if (pipeIndex >= 0) {
userid = username.substring(0, pipeIndex);
userEmail = username.substring(pipeIndex + 1);
} else {
userid = username;
userEmail = username;
}
// separate domain name
int backSlashIndex = userid.indexOf('\\');
if (backSlashIndex >= 0) {
// separate domain from username in credentials
domain = userid.substring(0, backSlashIndex);
userid = userid.substring(backSlashIndex + 1);
} else {
domain = Settings.getProperty("davmail.defaultDomain", "");
}
}
}
/**
* Retrieve Proxy Selector
*
* @param uri target uri
* @return proxy selector
*/
private static List<Proxy> getProxyForURI(java.net.URI uri) {
LOGGER.debug("get Default proxy selector");
ProxySelector proxySelector = ProxySelector.getDefault();
LOGGER.debug("getProxyForURI(" + uri + ')');
List<Proxy> proxies = proxySelector.select(uri);
LOGGER.debug("got system proxies:" + proxies);
return proxies;
}
protected static boolean isNoProxyFor(java.net.URI uri) {
final String noProxyFor = Settings.getProperty("davmail.noProxyFor");
if (noProxyFor != null) {
final String urihost = uri.getHost().toLowerCase();
final String[] domains = noProxyFor.toLowerCase().split(",\\s*");
for (String domain : domains) {
if (urihost.endsWith(domain)) {
// break;
return true;
}
}
}
return false;
}
public void startEvictorThread() {
DavMailIdleConnectionEvictor.addConnectionManager(connectionManager);
}
@Override
public void close() {
DavMailIdleConnectionEvictor.removeConnectionManager(connectionManager);
try {
httpClient.close();
} catch (IOException e) {
LOGGER.warn("Exception closing http client", e);
}
}
public static void close(HttpClientAdapter httpClientAdapter) {
if (httpClientAdapter != null) {
httpClientAdapter.close();
}
}
/**
* Execute request, do not follow redirects.
* if request is an instance of ResponseHandler, process and close response
*
* @param request Http request
* @return Http response
* @throws IOException on error
*/
public CloseableHttpResponse execute(HttpRequestBase request) throws IOException {
return execute(request, null);
}
/**
* Execute request, do not follow redirects.
* if request is an instance of ResponseHandler, process and close response
*
* @param request Http request
* @param context Http request context
* @return Http response
* @throws IOException on error
*/
public CloseableHttpResponse execute(HttpRequestBase request, HttpClientContext context) throws IOException {
// make sure request path is absolute
handleURI(request);
// execute request and return response
return httpClient.execute(request, context);
}
/**
* fix relative uri and update current uri.
*
* @param request http request
*/
private void handleURI(HttpRequestBase request) {
URI requestURI = request.getURI();
if (!requestURI.isAbsolute()) {
request.setURI(URIUtils.resolve(uri, requestURI));
}
uri = request.getURI();
}
public ResponseWrapper executeFollowRedirect(PostRequest request) throws IOException {
ResponseWrapper responseWrapper = request;
LOGGER.debug(request.getMethod() + " " + request.getURI().toString());
LOGGER.debug(request.getParameters());
int count = 0;
int maxRedirect = Settings.getIntProperty("davmail.httpMaxRedirects", MAX_REDIRECTS);
executePostRequest(request);
URI redirectLocation = request.getRedirectLocation();
while (count++ < maxRedirect && redirectLocation != null) {
LOGGER.debug("Redirect " + request.getURI() + " to " + redirectLocation);
// replace uri with target location
responseWrapper = new GetRequest(redirectLocation);
executeGetRequest((GetRequest) responseWrapper);
redirectLocation = ((GetRequest) responseWrapper).getRedirectLocation();
}
return responseWrapper;
}
public GetRequest executeFollowRedirect(GetRequest request) throws IOException {
GetRequest result = request;
LOGGER.debug(request.getMethod() + " " + request.getURI().toString());
int count = 0;
int maxRedirect = Settings.getIntProperty("davmail.httpMaxRedirects", MAX_REDIRECTS);
executeGetRequest(request);
URI redirectLocation = request.getRedirectLocation();
while (count++ < maxRedirect && redirectLocation != null) {
LOGGER.debug("Redirect " + request.getURI() + " to " + redirectLocation);
// replace uri with target location
result = new GetRequest(redirectLocation);
executeGetRequest(result);
redirectLocation = result.getRedirectLocation();
}
return result;
}
/**
* Execute get request and return response body as string.
*
* @param getRequest get request
* @return response body
* @throws IOException on error
*/
public String executeGetRequest(GetRequest getRequest) throws IOException {
handleURI(getRequest);
String responseBodyreplacedtring;
try (CloseableHttpResponse response = execute(getRequest)) {
responseBodyreplacedtring = getRequest.handleResponse(response);
}
return responseBodyreplacedtring;
}
/**
* Execute post request and return response body as string.
*
* @param postRequest post request
* @return response body
* @throws IOException on error
*/
public String executePostRequest(PostRequest postRequest) throws IOException {
handleURI(postRequest);
String responseBodyreplacedtring;
try (CloseableHttpResponse response = execute(postRequest)) {
responseBodyreplacedtring = postRequest.handleResponse(response);
}
return responseBodyreplacedtring;
}
public JSONObject executeRestRequest(RestRequest restRequest) throws IOException {
handleURI(restRequest);
JSONObject responseBody;
try (CloseableHttpResponse response = execute(restRequest)) {
responseBody = restRequest.handleResponse(response);
}
return responseBody;
}
/**
* Execute WebDav request
*
* @param request WebDav request
* @return multistatus response
* @throws IOException on error
*/
public MultiStatus executeDavRequest(BaseDavRequest request) throws IOException {
handleURI(request);
MultiStatus multiStatus = null;
try (CloseableHttpResponse response = execute(request)) {
request.checkSuccess(response);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_MULTI_STATUS) {
multiStatus = request.getResponseBodyAsMultiStatus(response);
}
} catch (DavException e) {
LOGGER.error(e.getMessage(), e);
throw new IOException(e.getErrorCode() + " " + e.getStatusPhrase(), e);
}
return multiStatus;
}
/**
* Execute Exchange WebDav request
*
* @param request WebDav request
* @return multistatus response
* @throws IOException on error
*/
public MultiStatusResponse[] executeDavRequest(ExchangeDavRequest request) throws IOException {
handleURI(request);
MultiStatusResponse[] responses;
try (CloseableHttpResponse response = execute(request)) {
List<MultiStatusResponse> responseList = request.handleResponse(response);
// TODO check error handling
// request.checkSuccess(response);
responses = responseList.toArray(new MultiStatusResponse[0]);
}
return responses;
}
/**
* Execute webdav search method.
*
* @param path <i>encoded</i> searched folder path
* @param searchStatement (SQL like) search statement
* @param maxCount max item count
* @return Responses enumeration
* @throws IOException on error
*/
public MultiStatusResponse[] executeSearchRequest(String path, String searchStatement, int maxCount) throws IOException {
ExchangeSearchRequest searchRequest = new ExchangeSearchRequest(path, searchStatement);
if (maxCount > 0) {
searchRequest.setHeader("Range", "rows=0-" + (maxCount - 1));
}
return executeDavRequest(searchRequest);
}
public static boolean isRedirect(HttpResponse response) {
return isRedirect(response.getStatusLine().getStatusCode());
}
/**
* Check if status is a redirect (various 30x values).
*
* @param status Http status
* @return true if status is a redirect
*/
public static boolean isRedirect(int status) {
return status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_SEE_OTHER || status == HttpStatus.SC_TEMPORARY_REDIRECT;
}
/**
* Get redirect location from header.
*
* @param response Http response
* @return URI target location
*/
public static URI getRedirectLocation(HttpResponse response) {
Header location = response.getFirstHeader("Location");
if (isRedirect(response.getStatusLine().getStatusCode()) && location != null) {
return URI.create(location.getValue());
}
return null;
}
public void setCredentials(String username, String preplacedword) {
parseUserName(username);
if (userid != null && preplacedword != null) {
NTCredentials credentials = new NTCredentials(userid, preplacedword, WORKSTATION_NAME, domain);
provider.setCredentials(AuthScope.ANY, credentials);
}
}
public List<Cookie> getCookies() {
return cookieStore.getCookies();
}
public void addCookie(Cookie cookie) {
cookieStore.addCookie(cookie);
}
public String getUserAgent() {
return Settings.getProperty("davmail.userAgent", DavGatewayHttpClientFacade.IE_USER_AGENT);
}
public static HttpResponseException buildHttpResponseException(HttpRequestBase request, HttpResponse response) {
return buildHttpResponseException(request, response.getStatusLine());
}
/**
* Build Http Exception from method status
*
* @param method Http Method
* @return Http Exception
*/
public static HttpResponseException buildHttpResponseException(HttpRequestBase method, StatusLine statusLine) {
int status = statusLine.getStatusCode();
StringBuilder message = new StringBuilder();
message.append(status).append(' ').append(statusLine.getReasonPhrase());
message.append(" at ").append(method.getURI());
if (method instanceof HttpCopy || method instanceof HttpMove) {
message.append(" to ").append(method.getFirstHeader("Destination"));
}
// 440 means forbidden on Exchange
if (status == 440) {
return new LoginTimeoutException(message.toString());
} else if (status == HttpStatus.SC_FORBIDDEN) {
return new HttpForbiddenException(message.toString());
} else if (status == HttpStatus.SC_NOT_FOUND) {
return new HttpNotFoundException(message.toString());
} else if (status == HttpStatus.SC_PRECONDITION_FAILED) {
return new HttpPreconditionFailedException(message.toString());
} else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) {
return new HttpServerErrorException(message.toString());
} else {
return new HttpResponseException(status, message.toString());
}
}
}
17
Source : HttpProtocolParent.java
with Apache License 2.0
from learningtcc
with Apache License 2.0
from learningtcc
private CloseableHttpClient createHttpClient(String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
HttpHost httpHost = new HttpHost(hostname, port);
// 将目标主机的最大连接数增加
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
}
17
Source : HC4CookieHandler.java
with Apache License 2.0
from johrstrom
with Apache License 2.0
from johrstrom
public clreplaced HC4CookieHandler implements CookieHandler {
private static final Logger log = LoggerFactory.getLogger(HC4CookieHandler.clreplaced);
// Needed by CookiePanel
public static String DEFAULT_POLICY_NAME = CookieSpecs.STANDARD;
public static final String[] AVAILABLE_POLICIES = new String[] { DEFAULT_POLICY_NAME, CookieSpecs.STANDARD_STRICT, CookieSpecs.IGNORE_COOKIES, CookieSpecs.NETSCAPE, CookieSpecs.DEFAULT, "rfc2109", "rfc2965", CookieSpecs.BEST_MATCH, CookieSpecs.BROWSER_COMPATIBILITY };
private final transient CookieSpec cookieSpec;
private static PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
private static Registry<CookieSpecProvider> registry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.BEST_MATCH, new DefaultCookieSpecProvider(publicSuffixMatcher)).register(CookieSpecs.BROWSER_COMPATIBILITY, new DefaultCookieSpecProvider(publicSuffixMatcher)).register(CookieSpecs.STANDARD, new RFC6265CookieSpecProvider()).register("rfc2109", // $NON-NLS-1$
new RFC2109SpecProvider(publicSuffixMatcher, true)).register("rfc2965", // $NON-NLS-1$
new RFC2965SpecProvider(publicSuffixMatcher, true)).register(CookieSpecs.STANDARD_STRICT, new RFC6265CookieSpecProvider(org.apache.http.impl.cookie.RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, null)).register(CookieSpecs.DEFAULT, new DefaultCookieSpecProvider(publicSuffixMatcher)).register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()).register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()).build();
public HC4CookieHandler(String policy) {
super();
if (policy.equals(org.apache.commons.httpclient.cookie.CookiePolicy.DEFAULT)) {
// tweak diff HC3 vs HC4
policy = CookieSpecs.DEFAULT;
}
HttpClientContext context = HttpClientContext.create();
this.cookieSpec = registry.lookup(policy).create(context);
}
@Override
public void addCookieFromHeader(CookieManager cookieManager, boolean checkCookies, String cookieHeader, URL url) {
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Received Cookie: " + cookieHeader + " From: " + url.toExternalForm());
}
String protocol = url.getProtocol();
String host = url.getHost();
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
String path = url.getPath();
boolean isSecure = HTTPSamplerBase.isSecure(protocol);
List<org.apache.http.cookie.Cookie> cookies = null;
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, isSecure);
BasicHeader basicHeader = new BasicHeader(HTTPConstants.HEADER_SET_COOKIE, cookieHeader);
try {
cookies = cookieSpec.parse(basicHeader, cookieOrigin);
} catch (MalformedCookieException e) {
log.error("Unable to add the cookie", e);
}
if (cookies == null) {
return;
}
for (org.apache.http.cookie.Cookie cookie : cookies) {
try {
if (checkCookies) {
cookieSpec.validate(cookie, cookieOrigin);
}
Date expiryDate = cookie.getExpiryDate();
long exp = 0;
if (expiryDate != null) {
exp = expiryDate.getTime();
}
Cookie newCookie = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.isSecure(), exp / 1000, ((BasicClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR), ((BasicClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR), cookie.getVersion());
// Store session cookies as well as unexpired ones
if (exp == 0 || exp >= System.currentTimeMillis()) {
// Has its own debug log; removes matching cookies
cookieManager.add(newCookie);
} else {
cookieManager.removeMatchingCookies(newCookie);
if (debugEnabled) {
log.info("Dropping expired Cookie: " + newCookie.toString());
}
}
} catch (MalformedCookieException e) {
// This means the cookie was wrong for the URL
log.warn("Not storing invalid cookie: <" + cookieHeader + "> for URL " + url + " (" + e.getLocalizedMessage() + ")");
} catch (IllegalArgumentException e) {
log.warn(cookieHeader + e.getLocalizedMessage());
}
}
}
@Override
public String getCookieHeaderForURL(CollectionProperty cookiesCP, URL url, boolean allowVariableCookie) {
List<org.apache.http.cookie.Cookie> c = getCookiesForUrl(cookiesCP, url, allowVariableCookie);
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("Found " + c.size() + " cookies for " + url.toExternalForm());
}
if (c.size() <= 0) {
return null;
}
List<Header> lstHdr = cookieSpec.formatCookies(c);
StringBuilder sbHdr = new StringBuilder();
for (Header header : lstHdr) {
sbHdr.append(header.getValue());
}
return sbHdr.toString();
}
/**
* Get array of valid HttpClient cookies for the URL
*
* @param cookiesCP property with all available cookies
* @param url the target URL
* @param allowVariableCookie flag whether cookies may contain jmeter variables
* @return array of HttpClient cookies
*/
List<org.apache.http.cookie.Cookie> getCookiesForUrl(CollectionProperty cookiesCP, URL url, boolean allowVariableCookie) {
List<org.apache.http.cookie.Cookie> cookies = new ArrayList<>();
for (JMeterProperty jMeterProperty : cookiesCP) {
Cookie jmcookie = (Cookie) jMeterProperty.getObjectValue();
// Set to running version, to allow function evaluation for the cookie values (bug 28715)
if (allowVariableCookie) {
jmcookie.setRunningVersion(true);
}
cookies.add(makeCookie(jmcookie));
if (allowVariableCookie) {
jmcookie.setRunningVersion(false);
}
}
String host = url.getHost();
String protocol = url.getProtocol();
int port = HTTPSamplerBase.getDefaultPort(protocol, url.getPort());
String path = url.getPath();
boolean secure = HTTPSamplerBase.isSecure(protocol);
CookieOrigin cookieOrigin = new CookieOrigin(host, port, path, secure);
List<org.apache.http.cookie.Cookie> cookiesValid = new ArrayList<>();
for (org.apache.http.cookie.Cookie cookie : cookies) {
if (cookieSpec.match(cookie, cookieOrigin)) {
cookiesValid.add(cookie);
}
}
return cookiesValid;
}
/**
* Create an HttpClient cookie from a JMeter cookie
*/
private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) {
long exp = jmc.getExpiresMillis();
BasicClientCookie ret = new BasicClientCookie(jmc.getName(), jmc.getValue());
ret.setDomain(jmc.getDomain());
ret.setPath(jmc.getPath());
// use null for no expiry
ret.setExpiryDate(exp > 0 ? new Date(exp) : null);
ret.setSecure(jmc.getSecure());
ret.setVersion(jmc.getVersion());
if (jmc.isDomainSpecified()) {
ret.setAttribute(ClientCookie.DOMAIN_ATTR, jmc.getDomain());
}
if (jmc.isPathSpecified()) {
ret.setAttribute(ClientCookie.PATH_ATTR, jmc.getPath());
}
return ret;
}
@Override
public String getDefaultPolicy() {
return DEFAULT_POLICY_NAME;
}
}
17
Source : ApacheConnector.java
with Apache License 2.0
from JeanRev
with Apache License 2.0
from JeanRev
private HttpClientConnectionManager createConnectionManager(final Client client, final Configuration config, final SSLContext sslContext, final boolean useSystemProperties) {
final String[] supportedProtocols = useSystemProperties ? split(System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = useSystemProperties ? split(System.getProperty("https.cipherSuites")) : null;
HostnameVerifier hostnameVerifier = client.getHostnameVerifier();
final LayeredConnectionSocketFactory sslSocketFactory;
if (sslContext != null) {
sslSocketFactory = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
if (useSystemProperties) {
sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
sslSocketFactory = new SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLContexts.createDefault(), hostnameVerifier);
}
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
final Integer chunkSize = ClientProperties.getValue(config.getProperties(), ClientProperties.CHUNKED_ENCODING_SIZE, ClientProperties.DEFAULT_CHUNK_SIZE, Integer.clreplaced);
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry, new ConnectionFactory(chunkSize));
if (useSystemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
connectionManager.setDefaultMaxPerRoute(max);
connectionManager.setMaxTotal(2 * max);
}
}
return connectionManager;
}
17
Source : HttpImpl.java
with MIT License
from heisedebaise
with MIT License
from heisedebaise
@Override
public void onContextRefreshed() {
try {
SSLContext sslContext = SSLContext.getInstance("SSLv3");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, null);
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslConnectionSocketFactory).build();
manager = new PoolingHttpClientConnectionManager(registry);
manager.setMaxTotal(max);
manager.setDefaultMaxPerRoute(max >> 3);
} catch (Exception e) {
logger.warn(e, "初始化HTTP/S客户端时发生异常!");
}
}
17
Source : MatrixNotifications.java
with MIT License
from fr1kin
with MIT License
from fr1kin
@RegisterMod(name = "MatrixNotifications", description = "Matrix notifications", category = Category.MISC)
@RequiredArgsConstructor
public clreplaced MatrixNotifications extends ToggleMod {
private static final Registry<ConnectionSocketFactory> SOCKET_FACTORY_REGISTRY;
static {
Registry<ConnectionSocketFactory> sf = null;
try {
SSLContextBuilder builder = SSLContexts.custom();
builder.loadTrustMaterial(null, (chain, authType) -> true);
SSLContext sslContext = builder.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new AllowAllHostsVerifier());
sf = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
e.printStackTrace();
} finally {
SOCKET_FACTORY_REGISTRY = sf;
}
}
@MapField(parentClreplaced = DisconnectedScreen.clreplaced, value = "message")
private final ReflectionField<ITextComponent> DisconnectedScreen_message;
@Injected("async")
private final ExecutorService async;
private final StringSetting url = newStringSetting().name("url").description("URL to the Matrix web hook").defaultTo("").build();
private final StringSetting user = newStringSetting().name("user").description("User to ping for high priority messages").defaultTo("").build();
private final StringSetting skin_server_url = newStringSetting().name("skin-server-url").description("URL to the skin server. If left empty then no image will be used.").defaultTo("https://visage.surgeplay.com/face/160/").build();
private final IntegerSetting queue_notify_pos = newIntegerSetting().name("queue-notify-pos").description("Position to start sending notifications at").defaultTo(5).build();
private final BooleanSetting on_connected = newBooleanSetting().name("on-connected").description("Message on connected to server").defaultTo(true).build();
private final BooleanSetting on_disconnected = newBooleanSetting().name("on-disconnected").description("Message on disconnected from server").defaultTo(true).build();
private final BooleanSetting on_queue_move = newBooleanSetting().name("on-queue-move").description("Message when player moves in the queue").defaultTo(true).build();
private boolean joined = false;
private boolean once = false;
private int position = 0;
private String serverName = null;
private static CloseableHttpClient createHttpClient() {
final RequestConfig req = RequestConfig.custom().setConnectTimeout(30 * 1000).setConnectionRequestTimeout(30 * 1000).build();
if (SOCKET_FACTORY_REGISTRY == null) {
return HttpClientBuilder.create().setDefaultRequestConfig(req).build();
} else {
return HttpClients.custom().setDefaultRequestConfig(req).setConnectionManager(new PoolingHttpClientConnectionManager(SOCKET_FACTORY_REGISTRY)).build();
}
}
private static HttpResponse post(final String url, final JsonElement json) throws IOException {
final Gson gson = new Gson();
try (CloseableHttpClient client = createHttpClient()) {
final HttpPost post = new HttpPost(url);
StringEnreplacedy enreplacedy = new StringEnreplacedy(gson.toJson(json));
post.setEnreplacedy(enreplacedy);
post.setHeader("Content-type", "application/json");
return client.execute(post);
}
}
private void postAsync(final String url, final JsonElement json) {
async.submit(() -> {
try {
HttpResponse res = post(url, json);
if (res.getStatusLine().getStatusCode() != 200) {
throw new Error("got response code " + res.getStatusLine().getStatusCode());
}
} catch (Throwable t) {
if (t.getCause() instanceof ValidatorException) {
Common.printError("Java JRE outdated. Change games to use the latest JRE.");
} else {
Common.printError("Failed to send message to url: " + t.getMessage());
}
t.printStackTrace();
}
});
}
private static String getServerName() {
return Optional.ofNullable(Common.MC.getCurrentServerData()).map(data -> data.serverName).orElse("server");
}
private static String getUriUuid() {
return Optional.of(Common.MC.getSession().getProfile()).map(GameProfile::getId).map(UUID::toString).map(id -> id.replaceAll("-", "")).orElse(null);
}
private void notify(String message) {
JsonObject object = new JsonObject();
object.addProperty("text", message);
object.addProperty("format", "plain");
object.addProperty("displayName", Common.MC.getSession().getUsername());
String id = getUriUuid();
if (!skin_server_url.getValue().isEmpty() && id != null) {
object.addProperty("avatarUrl", skin_server_url.getValue() + id);
}
postAsync(url.getValue(), object);
}
private void notify(String message, Object... args) {
notify(String.format(message, args));
}
private void ping(String message, Object... args) {
String msg = String.format(message, args);
if (user.getValue().isEmpty()) {
notify(msg);
} else {
notify("@" + user.getValue() + " " + msg);
}
}
@Override
protected void onEnabled() {
joined = once = false;
position = 0;
if (url.getValue().isEmpty()) {
Common.printError("Missing url");
}
if (SOCKET_FACTORY_REGISTRY == null) {
Common.printError("Custom socket factory has not been registered. All host SSL certificates must be trusted with the current JRE");
}
}
@SubscribeListener
public void onTick(LocalPlayerUpdateEvent event) {
joined = true;
if (!once) {
once = true;
if (on_connected.getValue()) {
BlockPos pos = Common.getLocalPlayer().getPosition();
if (pos.getX() != 0 && pos.getZ() != 0) {
ping("Connected to %s", getServerName());
} else {
notify("Connected to %s queue", getServerName());
}
}
}
}
@SubscribeListener
public void onWorldUnload(WorldLoadEvent event) {
once = false;
position = 0;
if (Common.MC.getCurrentServerData() != null) {
serverName = getServerName();
}
}
@SubscribeListener
public void onGuiOpened(GuiChangedEvent event) {
if (event.getGui() instanceof DisconnectedScreen && joined) {
joined = false;
if (on_disconnected.getValue()) {
String reason = Optional.ofNullable(DisconnectedScreen_message.get(event.getGui())).map(ITextComponent::getUnformattedComponentText).orElse("");
if (reason.isEmpty()) {
notify("Disconnected from %s", serverName);
} else {
notify("Disconnected from %s. Reason: %s", serverName, reason);
}
}
}
}
@SubscribeListener
public void onPacketRecieve(PacketInboundEvent event) {
if (event.getPacket() instanceof SChatPacket) {
SChatPacket packet = (SChatPacket) event.getPacket();
if (packet.getType() == ChatType.SYSTEM) {
ITextComponent comp = packet.getChatComponent();
if (comp.getSiblings().size() >= 2) {
String text = comp.getSiblings().get(0).getUnformattedComponentText();
if ("Position in queue: ".equals(text)) {
try {
int pos = Integer.parseInt(comp.getSiblings().get(1).getUnformattedComponentText());
if (pos != position) {
position = pos;
if (on_queue_move.getValue() && position <= queue_notify_pos.getValue()) {
if (position == 1) {
ping("Position 1 in queue");
} else {
notify("Position %d in queue", position);
}
}
}
} catch (Throwable t) {
// ignore
}
}
}
}
}
}
private static clreplaced AllowAllHostsVerifier implements X509HostnameVerifier {
@Override
public void verify(String host, SSLSocket ssl) throws IOException {
}
@Override
public void verify(String host, X509Certificate cert) throws SSLException {
}
@Override
public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
}
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}
}
17
Source : AbstractRequest.java
with Apache License 2.0
from finleytianhe
with Apache License 2.0
from finleytianhe
/**
* 执行http请求
*
* @param getMethod
* @return
* @throws IOException
*/
@SuppressWarnings("deprecation")
private final HttpResponse executeHttpRequest(HttpGet getMethod, String host) throws Exception {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry registry = RegistryBuilder.create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", sslsf).build();
HttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
CloseableHttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(50).setMaxConnTotal(100).setConnectionManager(httpClientConnectionManager).build();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
getMethod.setConfig(requestConfig);
HttpResponse response = httpClient.execute(getMethod);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpResponseStatus.OK.code() && statusCode != HttpResponseStatus.PARTIAL_CONTENT.code()) {
String result = EnreplacedyUtils.toString(response.getEnreplacedy());
throw new RuntimeException("return error !" + response.getStatusLine().getReasonPhrase() + ", " + result);
}
return response;
}
17
Source : HttpclientConnectionManager.java
with MIT License
from dromara
with MIT License
from dromara
@Override
public void init(ForestConfiguration configuration) {
try {
httpParams = new BasicHttpParams();
Integer maxConnections = configuration.getMaxConnections() != null ? configuration.getMaxConnections() : HttpConnectionConstants.DEFAULT_MAX_TOTAL_CONNECTIONS;
Integer maxRouteConnections = configuration.getMaxRouteConnections() != null ? configuration.getMaxRouteConnections() : HttpConnectionConstants.DEFAULT_MAX_TOTAL_CONNECTIONS;
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectFactory).register("http", new PlainConnectionSocketFactory()).build();
tsConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
tsConnectionManager.setMaxTotal(maxConnections);
tsConnectionManager.setDefaultMaxPerRoute(maxRouteConnections);
// / init async connection manager
boolean supportAsync = true;
try {
Clreplaced.forName("org.apache.http.nio.client.HttpAsyncClient");
} catch (ClreplacedNotFoundException e) {
supportAsync = false;
}
if (supportAsync) {
ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
if (asyncConnectionManager == null) {
try {
ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE).setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build();
asyncConnectionManager = new PoolingNHttpClientConnectionManager(ioReactor);
asyncConnectionManager.setMaxTotal(maxConnections);
asyncConnectionManager.setDefaultMaxPerRoute(maxRouteConnections);
asyncConnectionManager.setDefaultConnectionConfig(connectionConfig);
} catch (Throwable t) {
}
}
}
} catch (Throwable th) {
throw new ForestRuntimeException(th);
}
}
17
Source : PageFetcher.java
with Apache License 2.0
from doubleview
with Apache License 2.0
from doubleview
/**
* init the httpclient
*
* @param crawlerConfig
*/
private void initHttpClient(CrawlerConfig crawlerConfig) {
RequestConfig requestConfig = RequestConfig.custom().setExpectContinueEnabled(true).setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).setSocketTimeout(crawlerConfig.getSocketTimeout()).setConnectTimeout(crawlerConfig.getConnectionTimeout()).build();
RegistryBuilder<ConnectionSocketFactory> connRegistryBuilder = RegistryBuilder.create();
connRegistryBuilder.register("http", PlainConnectionSocketFactory.INSTANCE);
SSLConnectionSocketFactory sslConnectionSocketFactory = null;
try {
sslConnectionSocketFactory = new SSLConnectionSocketFactory(createIgnoreVerifySSL());
} catch (Exception e) {
logger.error("Error occurred : {}", e);
}
connRegistryBuilder.register("https", sslConnectionSocketFactory);
Registry<ConnectionSocketFactory> connRegistry = connRegistryBuilder.build();
connectionManager = new SniPoolingHttpClientConnectionManager(connRegistry);
connectionManager.setMaxTotal(crawlerConfig.getMaxTotalConns());
connectionManager.setDefaultMaxPerRoute(crawlerConfig.getMaxConnsPerHost());
HttpClientBuilder clientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).setUserAgent(crawlerConfig.getUserAgent());
if (crawlerConfig.getProxyHost() != null) {
if (crawlerConfig.getProxyUsername() != null) {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(crawlerConfig.getProxyHost(), crawlerConfig.getProxyPort()), new UsernamePreplacedwordCredentials(crawlerConfig.getProxyUsername(), crawlerConfig.getProxyPreplacedword()));
clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
HttpHost proxy = new HttpHost(crawlerConfig.getProxyHost(), crawlerConfig.getProxyPort());
clientBuilder.setProxy(proxy);
logger.debug("Working through Proxy : {}", proxy.getHostName());
}
httpClient = clientBuilder.build();
if (connectionMonitorThread == null) {
connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
}
connectionMonitorThread.start();
}
17
Source : Utility.java
with Apache License 2.0
from CognitiveBuild
with Apache License 2.0
from CognitiveBuild
public static CloseableHttpClient getTrustedHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
CloseableHttpClient httpClient = null;
SSLContext sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} }, new SecureRandom());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
PlainConnectionSocketFactory plainsf = new PlainConnectionSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
return httpClient;
}
17
Source : HttpClientUtils.java
with MIT License
from chingov
with MIT License
from chingov
/**
* 初始化HTTPS请求服务
*
* @param httpClient HTTP客户端
* @param port 端口
*/
private static void initSSL(CloseableHttpClient httpClient, int port) {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
final X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
// 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
sslContext.init(null, new TrustManager[] { trustManager }, null);
ConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext);
Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
BasicHttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(r);
HttpClients.custom().setConnectionManager(ccm).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
17
Source : HttpUtils.java
with Apache License 2.0
from apache
with Apache License 2.0
from apache
/**
* http utils
*/
public clreplaced HttpUtils {
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.clreplaced);
private HttpUtils() {
throw new UnsupportedOperationException("Construct HttpUtils");
}
public static CloseableHttpClient getInstance() {
return HttpClientInstance.httpClient;
}
private static clreplaced HttpClientInstance {
private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build();
}
private static PoolingHttpClientConnectionManager cm;
private static SSLContext ctx = null;
private static SSLConnectionSocketFactory socketFactory;
private static RequestConfig requestConfig;
private static Registry<ConnectionSocketFactory> socketFactoryRegistry;
private static X509TrustManager xtm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
static {
try {
ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
ctx.init(null, new TrustManager[] { xtm }, null);
} catch (NoSuchAlgorithmException e) {
logger.error("SSLContext init with NoSuchAlgorithmException", e);
} catch (KeyManagementException e) {
logger.error("SSLContext init with KeyManagementException", e);
}
socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
/**
* set timeout、request time、socket timeout
*/
requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).setExpectContinueEnabled(Boolean.TRUE).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT).setSocketTimeout(Constants.SOCKET_TIMEOUT).setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT).setRedirectsEnabled(true).build();
socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setDefaultMaxPerRoute(60);
cm.setMaxTotal(100);
}
/**
* get http request content
*
* @param url url
* @return http get request response content
*/
public static String get(String url) {
CloseableHttpClient httpclient = HttpUtils.getInstance();
HttpGet httpget = new HttpGet(url);
return getResponseContentString(httpget, httpclient);
}
/**
* get http response content
*
* @param httpget httpget
* @param httpClient httpClient
* @return http get request response content
*/
public static String getResponseContentString(HttpGet httpget, CloseableHttpClient httpClient) {
String responseContent = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpget);
// check response status is 200
if (response.getStatusLine().getStatusCode() == 200) {
HttpEnreplacedy enreplacedy = response.getEnreplacedy();
if (enreplacedy != null) {
responseContent = EnreplacedyUtils.toString(enreplacedy, Constants.UTF_8);
} else {
logger.warn("http enreplacedy is null");
}
} else {
logger.error("http get:{} response status code is not 200!", response.getStatusLine().getStatusCode());
}
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
} finally {
try {
if (response != null) {
EnreplacedyUtils.consume(response.getEnreplacedy());
response.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
if (!httpget.isAborted()) {
httpget.releaseConnection();
httpget.abort();
}
}
return responseContent;
}
}
17
Source : HttpClientUtil.java
with Apache License 2.0
from airufei
with Apache License 2.0
from airufei
public static CloseableHttpClient SSLClient() throws Exception {
SSLContext sc = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[] { trustManager }, null);
// 设置协议http和https对应的处理socket链接工厂的对象
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", new SSLConnectionSocketFactory(sc)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClients.custom().setConnectionManager(connManager);
// 创建自定义的httpclient对象
CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
return client;
}
17
Source : RestTemplateUtil.java
with Apache License 2.0
from 201206030
with Apache License 2.0
from 201206030
@SneakyThrows
public static RestTemplate getInstance(Charset charset) {
RestTemplate restTemplate = restTemplateMap.get(charset.name());
if (restTemplate == null) {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
// 忽略证书
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", csf).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
// 连接池的最大连接数,0代表不限;如果取0,需要考虑连接泄露导致系统崩溃的后果
connectionManager.setMaxTotal(1000);
// 每个路由的最大连接数,如果只调用一个地址,可以将其设置为最大连接数
connectionManager.setDefaultMaxPerRoute(300);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
// 从连接池获取连接的timeout,不宜过大,ms
requestFactory.setConnectionRequestTimeout(3000);
// 指客户端和服务器建立连接的超时时间,ms , 最大约21秒,因为内部tcp在进行三次握手建立连接时,默认tcp超时时间是20秒
requestFactory.setConnectTimeout(3000);
// 指客户端从服务器读取数据包的间隔超时时间,不是总读取时间,也就是socket timeout,ms
requestFactory.setReadTimeout(10000);
restTemplate = new RestTemplate(requestFactory);
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
for (HttpMessageConverter<?> httpMessageConverter : list) {
if (httpMessageConverter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(charset);
break;
}
}
restTemplateMap.put(charset.name(), restTemplate);
}
return restTemplate;
}
See More Examples