Java Examples for org.apache.http.conn.ssl.DefaultHostnameVerifier
The following java examples will help you to understand the usage of org.apache.http.conn.ssl.DefaultHostnameVerifier. These source code samples are taken from different open source projects.
Example 1
| Project: btpka3.github.com-master File: TestSocksProxy.java View source code |
public static void main(String[] args) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {
// 在 192.168.0.12 上执行
// ssh root@192.168.0.12 -C -f -N -g -D 9999
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("192.168.0.12", 9999));
HttpHost proxy1 = HttpHost.create("socks://192.168.0.12:9999");
PlainConnectionSocketFactoryEx httpConnFac = new PlainConnectionSocketFactoryEx();
httpConnFac.setProxy(proxy);
//
// KeyStore wxKeyStore = KeyStore.getInstance("PKCS12");
// wxKeyStore.load(new FileInputStream("xxx.p12"), "ks-pass".toCharArray());
SSLContext sslcontext = SSLContexts.custom().build();
SSLConnectionSocketFactoryEx httpsConnFac = new SSLConnectionSocketFactoryEx(sslcontext, new String[] { "TLSv1" }, null, new DefaultHostnameVerifier());
httpsConnFac.setProxy(proxy);
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpConnFac).register("https", httpsConnFac).build();
BasicHttpClientConnectionManager connMgr = new BasicHttpClientConnectionManager(reg);
CloseableHttpClient httpCient = HttpClients.custom().setConnectionManager(connMgr).build();
HttpComponentsClientHttpRequestFactory reqFac = new HttpComponentsClientHttpRequestFactory(httpCient);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(reqFac);
String url = "http://kingsilk.net/qh/mall/";
String respStr = restTemplate.getForObject(url, String.class);
System.out.println(respStr);
url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/activity/js/activity_start_52498d2c.js";
respStr = restTemplate.getForObject(url, String.class);
System.out.println(respStr);
}Example 2
| Project: uw-android-master File: ClientCustomPublicSuffixList.java View source code |
public static final void main(String[] args) throws Exception {
// Use PublicSuffixMatcherLoader to load public suffix list from a file,
// resource or from an arbitrary URL
PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL("https://publicsuffix.org/list/effective_tld_names.dat"));
// Please use the publicsuffix.org URL to download the list no more than once per day !!!
// Please consider making a local copy !!!
DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
RFC6265CookieSpecProvider cookieSpecProvider = new RFC6265CookieSpecProvider(publicSuffixMatcher);
Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, cookieSpecProvider).register(CookieSpecs.STANDARD, cookieSpecProvider).register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider).build();
CloseableHttpClient httpclient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).setDefaultCookieSpecRegistry(cookieSpecRegistry).build();
try {
HttpGet httpget = new HttpGet("https://remotehost/");
System.out.println("executing request " + httpget.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}Example 3
| Project: apiman-master File: DefaultESClientFactory.java View source code |
/**
* @param httpConfig
* @param config
*/
@SuppressWarnings("nls")
private void updateSslConfig(Builder httpConfig, Map<String, String> config) {
try {
String clientKeystorePath = config.get("client-keystore");
String clientKeystorePassword = config.get("client-keystore.password");
String trustStorePath = config.get("trust-store");
String trustStorePassword = config.get("trust-store.password");
SSLContext sslContext = SSLContext.getInstance("TLS");
Info kPathInfo = new Info(clientKeystorePath, clientKeystorePassword);
Info tPathInfo = new Info(trustStorePath, trustStorePassword);
sslContext.init(KeyStoreUtil.getKeyManagers(kPathInfo), KeyStoreUtil.getTrustManagers(tPathInfo), null);
HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);
httpConfig.defaultSchemeForDiscoveredNodes("https");
// for sync calls
httpConfig.sslSocketFactory(sslSocketFactory);
// for async calls
httpConfig.httpsIOSessionStrategy(httpsIOSessionStrategy);
} catch (Exception e) {
throw new RuntimeException(e);
}
}Example 4
| Project: ovirt-engine-sdk-java-master File: ConnectionBuilder45.java View source code |
private Registry createConnectionSocketFactoryRegistry() {
String protocol = getProtocol();
Registry registry = null;
// Create SSL/TLS or plain connection:
if (HTTP_PROTOCOL.equals(protocol)) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
registry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP_PROTOCOL, plainsf).build();
} else if (HTTPS_PROTOCOL.equals(protocol)) {
try {
LayeredConnectionSocketFactory sslsf = null;
if (this.insecure) {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { noCaTrustManager }, null);
sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
} else {
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
if (trustStoreFile != null) {
sslContextBuilder.loadTrustMaterial(new File(trustStoreFile), this.trustStorePassword != null ? this.trustStorePassword.toCharArray() : null);
}
SSLContext sslContext = sslContextBuilder.build();
sslsf = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
}
registry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTPS_PROTOCOL, sslsf).build();
} catch (NoSuchAlgorithmException e) {
throw new Error(NO_TLS_ERROR, e);
} catch (KeyManagementException e) {
throw new Error(BAD_KEY_ERROR, e);
} catch (KeyStoreException e) {
throw new Error(KEY_STORE_ERROR, e);
} catch (FileNotFoundException e) {
throw new Error(KEY_STORE_FILE_NOT_FOUND_ERROR, e);
} catch (CertificateException e) {
throw new Error(CERTIFICATE_ERROR, e);
} catch (IOException e) {
throw new Error(IO_ERROR, e);
}
} else {
throw new Error(BAD_PROTOCOL_ERROR + protocol);
}
return registry;
}Example 5
| Project: Resteasy-master File: HttpClientBuilder43.java View source code |
/**
* Create ClientHttpEngine using Apache 4.3.x+ apis.
* @return
*/
protected static ClientHttpEngine initDefaultEngine43(ResteasyClientBuilder that) {
HttpClient httpClient = null;
HostnameVerifier verifier = null;
if (that.verifier != null) {
verifier = new ResteasyClientBuilder.VerifierWrapper(that.verifier);
} else {
switch(that.policy) {
case ANY:
verifier = new NoopHostnameVerifier();
break;
case WILDCARD:
verifier = new DefaultHostnameVerifier();
break;
case STRICT:
verifier = new DefaultHostnameVerifier();
break;
}
}
try {
SSLConnectionSocketFactory sslsf = null;
SSLContext theContext = that.sslContext;
if (that.disableTrustManager) {
theContext = SSLContext.getInstance("SSL");
theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
verifier = new NoopHostnameVerifier();
sslsf = new SSLConnectionSocketFactory(theContext, verifier);
} else if (theContext != null) {
sslsf = new SSLConnectionSocketFactory(theContext, verifier) {
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
that.prepareSocketForSni(socket);
}
};
} else if (that.clientKeyStore != null || that.truststore != null) {
SSLContext ctx = SSLContexts.custom().useProtocol(SSLConnectionSocketFactory.TLS).setSecureRandom(null).loadKeyMaterial(that.clientKeyStore, that.clientPrivateKeyPassword != null ? that.clientPrivateKeyPassword.toCharArray() : null).loadTrustMaterial(that.truststore, TrustSelfSignedStrategy.INSTANCE).build();
sslsf = new SSLConnectionSocketFactory(ctx, verifier) {
@Override
protected void prepareSocket(SSLSocket socket) throws IOException {
that.prepareSocketForSni(socket);
}
};
} else {
final SSLContext tlsContext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
tlsContext.init(null, null, null);
sslsf = new SSLConnectionSocketFactory(tlsContext, verifier);
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
HttpClientConnectionManager cm = null;
if (that.connectionPoolSize > 0) {
PoolingHttpClientConnectionManager tcm = new PoolingHttpClientConnectionManager(registry, null, null, null, that.connectionTTL, that.connectionTTLUnit);
tcm.setMaxTotal(that.connectionPoolSize);
if (that.maxPooledPerRoute == 0) {
that.maxPooledPerRoute = that.connectionPoolSize;
}
tcm.setDefaultMaxPerRoute(that.maxPooledPerRoute);
cm = tcm;
} else {
cm = new BasicHttpClientConnectionManager(registry);
}
RequestConfig.Builder rcBuilder = RequestConfig.custom();
if (that.socketTimeout > -1) {
rcBuilder.setSocketTimeout((int) that.socketTimeoutUnits.toMillis(that.socketTimeout));
}
if (that.establishConnectionTimeout > -1) {
rcBuilder.setConnectTimeout((int) that.establishConnectionTimeoutUnits.toMillis(that.establishConnectionTimeout));
}
if (that.connectionCheckoutTimeoutMs > -1) {
rcBuilder.setConnectionRequestTimeout(that.connectionCheckoutTimeoutMs);
}
httpClient = HttpClientBuilder.create().setConnectionManager(cm).setDefaultRequestConfig(rcBuilder.build()).setProxy(that.defaultProxy).disableContentCompression().build();
ApacheHttpClient43Engine engine = (ApacheHttpClient43Engine) ApacheHttpClient4EngineFactory.create(httpClient, true);
engine.setResponseBufferSize(that.responseBufferSize);
engine.setHostnameVerifier(verifier);
// this may be null. We can't really support this with Apache Client.
engine.setSslContext(theContext);
return engine;
} catch (Exception e) {
throw new RuntimeException(e);
}
}Example 6
| Project: Signal-Server-master File: FederatedClient.java View source code |
private Client createClient(Environment environment, JerseyClientConfiguration configuration, String federationName, FederatedPeer peer) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, CertificateException {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init(initializeTrustStore(peer.getName(), peer.getCertificate()));
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory).build();
Client client = new JerseyClientBuilder(environment).using(configuration).using(registry).build("FederatedClient");
client.property(ClientProperties.CONNECT_TIMEOUT, 5000);
client.property(ClientProperties.READ_TIMEOUT, 10000);
client.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);
client.register(HttpAuthenticationFeature.basic(federationName, peer.getAuthenticationToken()));
return client;
}Example 7
| Project: tool-master File: HttpClientUtil.java View source code |
/**
* 发送Get请求Https
*
* @param httpGet httpget 对象
* @param charset 字符集
*/
private String sendHttpsGet(HttpGet httpGet, String charset) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String responseContent = null;
try {
PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
responseContent = EntityUtils.toString(response.getEntity(), StringUtil.isEmpty(charset) ? "UTF-8" : charset);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeResource(response, httpClient);
}
return responseContent;
}Example 8
| Project: nifi-master File: AbstractAWSProcessor.java View source code |
protected ClientConfiguration createConfiguration(final ProcessContext context) {
final ClientConfiguration config = new ClientConfiguration();
config.setMaxConnections(context.getMaxConcurrentTasks());
config.setMaxErrorRetry(0);
config.setUserAgent(DEFAULT_USER_AGENT);
// If this is changed to be a property, ensure other uses are also changed
config.setProtocol(DEFAULT_PROTOCOL);
final int commsTimeout = context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
config.setConnectionTimeout(commsTimeout);
config.setSocketTimeout(commsTimeout);
final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
if (sslContextService != null) {
final SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.NONE);
// NIFI-3788: Changed hostnameVerifier from null to DHV (BrowserCompatibleHostnameVerifier is deprecated)
SdkTLSSocketFactory sdkTLSSocketFactory = new SdkTLSSocketFactory(sslContext, new DefaultHostnameVerifier());
config.getApacheHttpClientConfig().setSslSocketFactory(sdkTLSSocketFactory);
}
if (context.getProperty(PROXY_HOST).isSet()) {
String proxyHost = context.getProperty(PROXY_HOST).getValue();
config.setProxyHost(proxyHost);
Integer proxyPort = context.getProperty(PROXY_HOST_PORT).asInteger();
config.setProxyPort(proxyPort);
}
return config;
}Example 9
| Project: nutch-master File: ElasticRestIndexWriter.java View source code |
@Override
public void open(JobConf job, String name) throws IOException {
host = job.get(ElasticRestConstants.HOST);
port = job.getInt(ElasticRestConstants.PORT, 9200);
user = job.get(ElasticRestConstants.USER);
password = job.get(ElasticRestConstants.PASSWORD);
https = job.getBoolean(ElasticRestConstants.HTTPS, false);
trustAllHostnames = job.getBoolean(ElasticRestConstants.HOSTNAME_TRUST, false);
// trust ALL certificates
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
} catch (NoSuchAlgorithmExceptionKeyManagementException | KeyStoreException | e) {
LOG.error("Failed to instantiate sslcontext object: \n{}", ExceptionUtils.getStackTrace(e));
throw new SecurityException();
}
// skip hostname checks
HostnameVerifier hostnameVerifier = null;
if (trustAllHostnames) {
hostnameVerifier = NoopHostnameVerifier.INSTANCE;
} else {
hostnameVerifier = new DefaultHostnameVerifier();
}
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
SchemeIOSessionStrategy httpsIOSessionStrategy = new SSLIOSessionStrategy(sslContext, hostnameVerifier);
JestClientFactory jestClientFactory = new JestClientFactory();
URL urlOfElasticsearchNode = new URL(https ? "https" : "http", host, port, "");
if (host != null && port > 1) {
HttpClientConfig.Builder builder = new HttpClientConfig.Builder(urlOfElasticsearchNode.toString()).multiThreaded(true).connTimeout(300000).readTimeout(300000);
if (https) {
if (user != null && password != null) {
builder.defaultCredentials(user, password);
}
builder.defaultSchemeForDiscoveredNodes("https").sslSocketFactory(// this only affects sync calls
sslSocketFactory).httpsIOSessionStrategy(// this only affects async calls
httpsIOSessionStrategy);
}
jestClientFactory.setHttpClientConfig(builder.build());
} else {
throw new IllegalStateException("No host or port specified. Please set the host and port in nutch-site.xml");
}
client = jestClientFactory.getObject();
defaultIndex = job.get(ElasticRestConstants.INDEX, "nutch");
defaultType = job.get(ElasticRestConstants.TYPE, "doc");
maxBulkDocs = job.getInt(ElasticRestConstants.MAX_BULK_DOCS, DEFAULT_MAX_BULK_DOCS);
maxBulkLength = job.getInt(ElasticRestConstants.MAX_BULK_LENGTH, DEFAULT_MAX_BULK_LENGTH);
bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
}Example 10
| Project: rest-client-master File: HTTPClientRequestExecuter.java View source code |
@Override
public void execute(Request request, View... views) {
// Verify if this is the first call to this object:
if (isRequestStarted) {
throw new MultipleRequestInSameRequestExecuterException("A RequestExecuter object can be used only once!");
}
isRequestStarted = true;
// Proceed with execution:
for (View view : views) {
view.doStart(request);
}
// Needed for specifying HTTP pre-emptive authentication:
HttpContext httpContext = null;
// Create all the builder objects:
final HttpClientBuilder hcBuilder = HttpClientBuilder.create();
final RequestConfig.Builder rcBuilder = RequestConfig.custom();
final RequestBuilder reqBuilder = RequestBuilder.create(request.getMethod().name());
// Retry handler (no-retries):
hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
// Url:
final URL url = IDNUtil.getIDNizedURL(request.getUrl());
final String urlHost = url.getHost();
final int urlPort = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
final String urlProtocol = url.getProtocol();
final String urlStr = url.toString();
reqBuilder.setUri(urlStr);
// Set HTTP version:
HTTPVersion httpVersion = request.getHttpVersion();
ProtocolVersion protocolVersion = httpVersion == HTTPVersion.HTTP_1_1 ? new ProtocolVersion("HTTP", 1, 1) : new ProtocolVersion("HTTP", 1, 0);
reqBuilder.setVersion(protocolVersion);
// Set request timeout (default 1 minute--60000 milliseconds)
IGlobalOptions options = ServiceLocator.getInstance(IGlobalOptions.class);
rcBuilder.setConnectionRequestTimeout(Integer.parseInt(options.getProperty("request-timeout-in-millis")));
// Set proxy
ProxyConfig proxy = ProxyConfig.getInstance();
proxy.acquire();
if (proxy.isEnabled()) {
final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http");
if (proxy.isAuthEnabled()) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword())));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
hcBuilder.setProxy(proxyHost);
}
proxy.release();
// HTTP Authentication
if (request.getAuth() != null) {
// Add auth preference:
Auth auth = request.getAuth();
List<String> authPrefs = new ArrayList<>();
if (auth instanceof BasicAuth) {
authPrefs.add(AuthSchemes.BASIC);
} else if (auth instanceof DigestAuth) {
authPrefs.add(AuthSchemes.DIGEST);
} else if (auth instanceof NtlmAuth) {
authPrefs.add(AuthSchemes.NTLM);
}
rcBuilder.setTargetPreferredAuthSchemes(authPrefs);
// BASIC & DIGEST:
if (auth instanceof BasicAuth || auth instanceof DigestAuth) {
BasicDigestAuth a = (BasicDigestAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
String host = StringUtil.isEmpty(a.getHost()) ? urlHost : a.getHost();
String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, urlPort, realm), new UsernamePasswordCredentials(uid, pwd));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
// preemptive mode:
if (a.isPreemptive()) {
AuthCache authCache = new BasicAuthCache();
AuthSchemeBase authScheme = a instanceof BasicAuth ? new BasicScheme() : new DigestScheme();
authCache.put(new HttpHost(urlHost, urlPort, urlProtocol), authScheme);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
httpContext = localContext;
}
}
// NTLM:
if (auth instanceof NtlmAuth) {
NtlmAuth a = (NtlmAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(uid, pwd, a.getWorkstation(), a.getDomain()));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
// Authorization header
// Logic written in same place where Header is processed--a little down!
}
try {
{
// Authorization Header Authentication:
Auth auth = request.getAuth();
if (auth != null && auth instanceof AuthorizationHeaderAuth) {
AuthorizationHeaderAuth a = (AuthorizationHeaderAuth) auth;
final String authHeader = a.getAuthorizationHeaderValue();
if (StringUtil.isNotEmpty(authHeader)) {
Header header = new BasicHeader("Authorization", authHeader);
reqBuilder.addHeader(header);
}
}
}
// Get request headers
MultiValueMap<String, String> header_data = request.getHeaders();
for (String key : header_data.keySet()) {
for (String value : header_data.get(key)) {
Header header = new BasicHeader(key, value);
reqBuilder.addHeader(header);
}
}
// Cookies
{
// Set cookie policy:
rcBuilder.setCookieSpec(CookieSpecs.DEFAULT);
// Add to CookieStore:
CookieStore store = new RESTClientCookieStore();
List<HttpCookie> cookies = request.getCookies();
for (HttpCookie cookie : cookies) {
BasicClientCookie c = new BasicClientCookie(cookie.getName(), cookie.getValue());
c.setVersion(cookie.getVersion());
c.setDomain(urlHost);
c.setPath("/");
store.addCookie(c);
}
// Attach store to client:
hcBuilder.setDefaultCookieStore(store);
}
// POST/PUT/PATCH/DELETE method specific logic
if (HttpUtil.isEntityEnclosingMethod(reqBuilder.getMethod())) {
// Create and set RequestEntity
ReqEntity bean = request.getBody();
if (bean != null) {
try {
if (bean instanceof ReqEntitySimple) {
AbstractHttpEntity e = HTTPClientUtil.getEntity((ReqEntitySimple) bean);
reqBuilder.setEntity(e);
} else if (bean instanceof ReqEntityMultipart) {
ReqEntityMultipart multipart = (ReqEntityMultipart) bean;
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
// multipart/mixed / multipart/form-data:
meb.setMimeSubtype(multipart.getSubtype().toString());
// Format:
MultipartMode mpMode = multipart.getMode();
switch(mpMode) {
case BROWSER_COMPATIBLE:
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
break;
case RFC_6532:
meb.setMode(HttpMultipartMode.RFC6532);
break;
case STRICT:
meb.setMode(HttpMultipartMode.STRICT);
break;
}
// Parts:
for (ReqEntityPart part : multipart.getBody()) {
ContentBody cb = null;
if (part instanceof ReqEntityStringPart) {
ReqEntityStringPart p = (ReqEntityStringPart) part;
String body = p.getPart();
ContentType ct = p.getContentType();
if (ct != null) {
cb = new StringBody(body, HTTPClientUtil.getContentType(ct));
} else {
cb = new StringBody(body, org.apache.http.entity.ContentType.DEFAULT_TEXT);
}
} else if (part instanceof ReqEntityFilePart) {
ReqEntityFilePart p = (ReqEntityFilePart) part;
File body = p.getPart();
ContentType ct = p.getContentType();
if (ct != null) {
cb = new FileBody(body, HTTPClientUtil.getContentType(ct), p.getFilename());
} else {
cb = new FileBody(body, org.apache.http.entity.ContentType.DEFAULT_BINARY, p.getFilename());
}
}
FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create().setName(part.getName()).setBody(cb);
MultiValueMap<String, String> fields = part.getFields();
for (String key : fields.keySet()) {
for (String value : fields.get(key)) {
bodyPart.addField(key, value);
}
}
meb.addPart(bodyPart.build());
}
reqBuilder.setEntity(meb.build());
}
} catch (UnsupportedEncodingException ex) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
view.doEnd();
}
return;
}
}
}
// SSL
// Set the hostname verifier:
final SSLReq sslReq = request.getSslReq();
if (sslReq != null) {
SSLHostnameVerifier verifier = sslReq.getHostNameVerifier();
final HostnameVerifier hcVerifier;
switch(verifier) {
case ALLOW_ALL:
hcVerifier = new NoopHostnameVerifier();
break;
case STRICT:
default:
hcVerifier = new DefaultHostnameVerifier();
break;
}
// Register the SSL Scheme:
final KeyStore trustStore = sslReq.getTrustStore() == null ? null : sslReq.getTrustStore().getKeyStore();
final KeyStore keyStore = sslReq.getKeyStore() == null ? null : sslReq.getKeyStore().getKeyStore();
final TrustStrategy trustStrategy = sslReq.isTrustAllCerts() ? new TrustAllTrustStrategy() : null;
SSLContext ctx = new SSLContextBuilder().loadKeyMaterial(keyStore, sslReq.getKeyStore() != null ? sslReq.getKeyStore().getPassword() : null).loadTrustMaterial(trustStore, trustStrategy).setSecureRandom(null).useProtocol("TLS").build();
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(ctx, hcVerifier);
hcBuilder.setSSLSocketFactory(sf);
}
// How to handle redirects:
rcBuilder.setRedirectsEnabled(request.isFollowRedirect());
// Now Execute:
long startTime = System.currentTimeMillis();
RequestConfig rc = rcBuilder.build();
reqBuilder.setConfig(rc);
HttpUriRequest req = reqBuilder.build();
httpClient = hcBuilder.build();
HttpResponse http_res = httpClient.execute(req, httpContext);
long endTime = System.currentTimeMillis();
// Create response:
ResponseBean response = new ResponseBean();
response.setExecutionTime(endTime - startTime);
response.setStatusCode(http_res.getStatusLine().getStatusCode());
response.setStatusLine(http_res.getStatusLine().toString());
final Header[] responseHeaders = http_res.getAllHeaders();
for (Header header : responseHeaders) {
response.addHeader(header.getName(), header.getValue());
}
// Response body:
final HttpEntity entity = http_res.getEntity();
if (entity != null) {
if (request.isIgnoreResponseBody()) {
EntityUtils.consumeQuietly(entity);
} else {
InputStream is = entity.getContent();
try {
byte[] responseBody = StreamUtil.inputStream2Bytes(is);
if (responseBody != null) {
response.setResponseBody(responseBody);
}
} catch (IOException ex) {
for (View view : views) {
view.doError("Byte array conversion from response body stream failed.");
}
LOG.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
// Now execute tests:
try {
junit.framework.TestSuite suite = TestUtil.getTestSuite(request, response);
if (suite != null) {
// suite will be null if there is no associated script
TestResult testResult = TestUtil.execute(suite);
response.setTestResult(testResult);
}
} catch (TestException ex) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
}
}
for (View view : views) {
view.doResponse(response);
}
} catch (IOExceptionKeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyManagementException | IllegalStateException | ex) {
if (!interruptedShutdown) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
}
} else {
for (View view : views) {
view.doCancelled();
}
}
} finally {
if (!interruptedShutdown) {
// close it only when otherwise:
try {
if (httpClient != null)
httpClient.close();
} catch (IOException ex) {
LOG.log(Level.WARNING, "Exception when closing httpClient", ex);
}
} else {
// reset value to default:
interruptedShutdown = false;
}
for (View view : views) {
view.doEnd();
}
isRequestCompleted = true;
}
}Example 11
| Project: light-4j-master File: Client.java View source code |
private HostnameVerifier hostnameVerifier() {
Map<String, Object> tlsMap = (Map) config.get(TLS);
HostnameVerifier verifier = null;
if (tlsMap != null) {
Boolean verifyHostname = (Boolean) tlsMap.get(VERIFY_HOSTNAME);
if (verifyHostname != null && !verifyHostname) {
verifier = new NoopHostnameVerifier();
} else {
verifier = new DefaultHostnameVerifier();
}
}
return verifier;
}Example 12
| Project: i2p.i2p-master File: I2PSSLSocketFactory.java View source code |
/**
* Validate the hostname
*
* ref: https://developer.android.com/training/articles/security-ssl.html
* ref: http://op-co.de/blog/posts/java_sslsocket_mitm/
* ref: http://kevinlocke.name/bits/2012/10/03/ssl-certificate-verification-in-dispatch-and-asynchttpclient/
*
* @throws SSLException on hostname verification failure
* @since 0.9.20
*/
public static void verifyHostname(I2PAppContext ctx, SSLSocket socket, String host) throws SSLException {
Log log = ctx.logManager().getLog(I2PSSLSocketFactory.class);
if (ctx.getBooleanProperty(PROP_DISABLE) || host.equals("localhost") || host.equals("127.0.0.1") || host.equals("::1") || host.equals("0:0:0:0:0:0:0:1")) {
if (log.shouldWarn())
log.warn("Skipping hostname validation for " + host);
return;
}
HostnameVerifier hv;
if (SystemVersion.isAndroid()) {
// https://developer.android.com/training/articles/security-ssl.html
hv = HttpsURLConnection.getDefaultHostnameVerifier();
} else {
// haha the above may work for Android but it doesn't in Oracle
//
// quote http://kevinlocke.name/bits/2012/10/03/ssl-certificate-verification-in-dispatch-and-asynchttpclient/ :
// Unlike SSLContext, using the Java default (HttpsURLConnection.getDefaultHostnameVerifier)
// is not a viable option because the default HostnameVerifier expects to only be called
// in the case that there is a mismatch (and therefore always returns false) while some
// of the AsyncHttpClient providers (e.g. Netty, the default) call it on all connections.
// To make matters worse, the check is not trivial (consider SAN and wildcard matching)
// and is implemented in sun.security.util.HostnameChecker (a Sun internal proprietary API).
// This leaves the developer in the position of either depending on an internal API or
// finding/copying/creating another implementation of this functionality.
//
hv = new DefaultHostnameVerifier(getDefaultMatcher(ctx));
}
SSLSession sess = socket.getSession();
// This is due to lack of SNI support in the current SSLSocket.
if (!hv.verify(host, sess)) {
throw new SSLHandshakeException("SSL hostname verify failed, Expected " + host + // enable logging for DefaultHostnameVerifier to find out the CN and SANs
" - set " + PROP_DISABLE + "=true to disable verification (dangerous!)");
}
// At this point SSLSocket performed certificate verificaiton and
// we have performed hostname verification, so it is safe to proceed.
}Example 13
| Project: bugvm-master File: HttpClientBuilder.java View source code |
public CloseableHttpClient build() {
// Create main request executor
// We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
PublicSuffixMatcher publicSuffixMatcherCopy = this.publicSuffixMatcher;
if (publicSuffixMatcherCopy == null) {
publicSuffixMatcherCopy = PublicSuffixMatcherLoader.getDefault();
}
HttpRequestExecutor requestExecCopy = this.requestExec;
if (requestExecCopy == null) {
requestExecCopy = new HttpRequestExecutor();
}
HttpClientConnectionManager connManagerCopy = this.connManager;
if (connManagerCopy == null) {
LayeredConnectionSocketFactory sslSocketFactoryCopy = this.sslSocketFactory;
if (sslSocketFactoryCopy == null) {
final String[] supportedProtocols = systemProperties ? split(System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = systemProperties ? split(System.getProperty("https.cipherSuites")) : null;
HostnameVerifier hostnameVerifierCopy = this.hostnameVerifier;
if (hostnameVerifierCopy == null) {
hostnameVerifierCopy = new DefaultHostnameVerifier(publicSuffixMatcherCopy);
}
if (sslContext != null) {
sslSocketFactoryCopy = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
} else {
if (systemProperties) {
sslSocketFactoryCopy = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
} else {
sslSocketFactoryCopy = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifierCopy);
}
}
}
@SuppressWarnings("resource") final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactoryCopy).build(), null, null, dnsResolver, connTimeToLive, connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
if (defaultSocketConfig != null) {
poolingmgr.setDefaultSocketConfig(defaultSocketConfig);
}
if (defaultConnectionConfig != null) {
poolingmgr.setDefaultConnectionConfig(defaultConnectionConfig);
}
if (systemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);
}
}
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
connManagerCopy = poolingmgr;
}
ConnectionReuseStrategy reuseStrategyCopy = this.reuseStrategy;
if (reuseStrategyCopy == null) {
if (systemProperties) {
final String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
reuseStrategyCopy = DefaultClientConnectionReuseStrategy.INSTANCE;
} else {
reuseStrategyCopy = NoConnectionReuseStrategy.INSTANCE;
}
} else {
reuseStrategyCopy = DefaultClientConnectionReuseStrategy.INSTANCE;
}
}
ConnectionKeepAliveStrategy keepAliveStrategyCopy = this.keepAliveStrategy;
if (keepAliveStrategyCopy == null) {
keepAliveStrategyCopy = DefaultConnectionKeepAliveStrategy.INSTANCE;
}
AuthenticationStrategy targetAuthStrategyCopy = this.targetAuthStrategy;
if (targetAuthStrategyCopy == null) {
targetAuthStrategyCopy = TargetAuthenticationStrategy.INSTANCE;
}
AuthenticationStrategy proxyAuthStrategyCopy = this.proxyAuthStrategy;
if (proxyAuthStrategyCopy == null) {
proxyAuthStrategyCopy = ProxyAuthenticationStrategy.INSTANCE;
}
UserTokenHandler userTokenHandlerCopy = this.userTokenHandler;
if (userTokenHandlerCopy == null) {
if (!connectionStateDisabled) {
userTokenHandlerCopy = DefaultUserTokenHandler.INSTANCE;
} else {
userTokenHandlerCopy = NoopUserTokenHandler.INSTANCE;
}
}
String userAgentCopy = this.userAgent;
if (userAgentCopy == null) {
if (systemProperties) {
userAgentCopy = System.getProperty("http.agent");
}
if (userAgentCopy == null) {
userAgentCopy = VersionInfo.getUserAgent("Apache-HttpClient", "org.apache.http.client", getClass());
}
}
ClientExecChain execChain = createMainExec(requestExecCopy, connManagerCopy, reuseStrategyCopy, keepAliveStrategyCopy, new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)), targetAuthStrategyCopy, proxyAuthStrategyCopy, userTokenHandlerCopy);
execChain = decorateMainExec(execChain);
HttpProcessor httpprocessorCopy = this.httpprocessor;
if (httpprocessorCopy == null) {
final HttpProcessorBuilder b = HttpProcessorBuilder.create();
if (requestFirst != null) {
for (final HttpRequestInterceptor i : requestFirst) {
b.addFirst(i);
}
}
if (responseFirst != null) {
for (final HttpResponseInterceptor i : responseFirst) {
b.addFirst(i);
}
}
b.addAll(new RequestDefaultHeaders(defaultHeaders), new RequestContent(), new RequestTargetHost(), new RequestClientConnControl(), new RequestUserAgent(userAgentCopy), new RequestExpectContinue());
if (!cookieManagementDisabled) {
b.add(new RequestAddCookies());
}
if (!contentCompressionDisabled) {
if (contentDecoderMap != null) {
final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
Collections.sort(encodings);
b.add(new RequestAcceptEncoding(encodings));
} else {
b.add(new RequestAcceptEncoding());
}
}
if (!authCachingDisabled) {
b.add(new RequestAuthCache());
}
if (!cookieManagementDisabled) {
b.add(new ResponseProcessCookies());
}
if (!contentCompressionDisabled) {
if (contentDecoderMap != null) {
final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
for (final Map.Entry<String, InputStreamFactory> entry : contentDecoderMap.entrySet()) {
b2.register(entry.getKey(), entry.getValue());
}
b.add(new ResponseContentEncoding(b2.build()));
} else {
b.add(new ResponseContentEncoding());
}
}
if (requestLast != null) {
for (final HttpRequestInterceptor i : requestLast) {
b.addLast(i);
}
}
if (responseLast != null) {
for (final HttpResponseInterceptor i : responseLast) {
b.addLast(i);
}
}
httpprocessorCopy = b.build();
}
execChain = new ProtocolExec(execChain, httpprocessorCopy);
execChain = decorateProtocolExec(execChain);
// Add request retry executor, if not disabled
if (!automaticRetriesDisabled) {
HttpRequestRetryHandler retryHandlerCopy = this.retryHandler;
if (retryHandlerCopy == null) {
retryHandlerCopy = DefaultHttpRequestRetryHandler.INSTANCE;
}
execChain = new RetryExec(execChain, retryHandlerCopy);
}
HttpRoutePlanner routePlannerCopy = this.routePlanner;
if (routePlannerCopy == null) {
SchemePortResolver schemePortResolverCopy = this.schemePortResolver;
if (schemePortResolverCopy == null) {
schemePortResolverCopy = DefaultSchemePortResolver.INSTANCE;
}
if (proxy != null) {
routePlannerCopy = new DefaultProxyRoutePlanner(proxy, schemePortResolverCopy);
} else if (systemProperties) {
routePlannerCopy = new SystemDefaultRoutePlanner(schemePortResolverCopy, ProxySelector.getDefault());
} else {
routePlannerCopy = new DefaultRoutePlanner(schemePortResolverCopy);
}
}
// Add redirect executor, if not disabled
if (!redirectHandlingDisabled) {
RedirectStrategy redirectStrategyCopy = this.redirectStrategy;
if (redirectStrategyCopy == null) {
redirectStrategyCopy = DefaultRedirectStrategy.INSTANCE;
}
execChain = new RedirectExec(execChain, routePlannerCopy, redirectStrategyCopy);
}
// Optionally, add service unavailable retry executor
final ServiceUnavailableRetryStrategy serviceUnavailStrategyCopy = this.serviceUnavailStrategy;
if (serviceUnavailStrategyCopy != null) {
execChain = new ServiceUnavailableRetryExec(execChain, serviceUnavailStrategyCopy);
}
// Optionally, add connection back-off executor
if (this.backoffManager != null && this.connectionBackoffStrategy != null) {
execChain = new BackoffStrategyExec(execChain, this.connectionBackoffStrategy, this.backoffManager);
}
Lookup<AuthSchemeProvider> authSchemeRegistryCopy = this.authSchemeRegistry;
if (authSchemeRegistryCopy == null) {
authSchemeRegistryCopy = 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();
}
Lookup<CookieSpecProvider> cookieSpecRegistryCopy = this.cookieSpecRegistry;
if (cookieSpecRegistryCopy == null) {
cookieSpecRegistryCopy = CookieSpecRegistries.createDefault(publicSuffixMatcherCopy);
}
CookieStore defaultCookieStore = this.cookieStore;
if (defaultCookieStore == null) {
defaultCookieStore = new BasicCookieStore();
}
CredentialsProvider defaultCredentialsProvider = this.credentialsProvider;
if (defaultCredentialsProvider == null) {
if (systemProperties) {
defaultCredentialsProvider = new SystemDefaultCredentialsProvider();
} else {
defaultCredentialsProvider = new BasicCredentialsProvider();
}
}
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
if (!this.connManagerShared) {
if (closeablesCopy == null) {
closeablesCopy = new ArrayList<Closeable>(1);
}
final HttpClientConnectionManager cm = connManagerCopy;
if (evictExpiredConnections || evictIdleConnections) {
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm, maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS);
closeablesCopy.add(new Closeable() {
@Override
public void close() throws IOException {
connectionEvictor.shutdown();
}
});
connectionEvictor.start();
}
closeablesCopy.add(new Closeable() {
@Override
public void close() throws IOException {
cm.shutdown();
}
});
}
return new InternalHttpClient(execChain, connManagerCopy, routePlannerCopy, cookieSpecRegistryCopy, authSchemeRegistryCopy, defaultCookieStore, defaultCredentialsProvider, defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT, closeablesCopy);
}Example 14
| Project: manifold-master File: SharePointRepository.java View source code |
/** Set up a session */
protected void getSession() throws ManifoldCFException {
if (proxy == null) {
String serverVersion = params.getParameter(SharePointConfig.PARAM_SERVERVERSION);
if (serverVersion == null)
serverVersion = "4.0";
supportsItemSecurity = !serverVersion.equals("2.0");
dspStsWorks = serverVersion.equals("2.0") || serverVersion.equals("3.0");
attachmentsSupported = !serverVersion.equals("2.0");
String authorityType = params.getParameter(SharePointConfig.PARAM_AUTHORITYTYPE);
if (authorityType == null)
authorityType = "ActiveDirectory";
activeDirectoryAuthority = authorityType.equals("ActiveDirectory");
serverProtocol = params.getParameter(SharePointConfig.PARAM_SERVERPROTOCOL);
if (serverProtocol == null)
serverProtocol = "http";
try {
String serverPort = params.getParameter(SharePointConfig.PARAM_SERVERPORT);
if (serverPort == null || serverPort.length() == 0) {
if (serverProtocol.equals("https"))
this.serverPort = 443;
else
this.serverPort = 80;
} else
this.serverPort = Integer.parseInt(serverPort);
} catch (NumberFormatException e) {
throw new ManifoldCFException(e.getMessage(), e);
}
serverLocation = params.getParameter(SharePointConfig.PARAM_SERVERLOCATION);
if (serverLocation == null)
serverLocation = "";
if (serverLocation.endsWith("/"))
serverLocation = serverLocation.substring(0, serverLocation.length() - 1);
if (serverLocation.length() > 0 && !serverLocation.startsWith("/"))
serverLocation = "/" + serverLocation;
encodedServerLocation = serverLocation;
serverLocation = decodePath(serverLocation);
userName = params.getParameter(SharePointConfig.PARAM_SERVERUSERNAME);
password = params.getObfuscatedParameter(SharePointConfig.PARAM_SERVERPASSWORD);
int index = userName.indexOf("\\");
if (index != -1) {
strippedUserName = userName.substring(index + 1);
ntlmDomain = userName.substring(0, index);
} else {
strippedUserName = null;
ntlmDomain = null;
}
String proxyHost = params.getParameter(SharePointConfig.PARAM_PROXYHOST);
String proxyPortString = params.getParameter(SharePointConfig.PARAM_PROXYPORT);
int proxyPort = 8080;
if (proxyPortString != null && proxyPortString.length() > 0) {
try {
proxyPort = Integer.parseInt(proxyPortString);
} catch (NumberFormatException e) {
throw new ManifoldCFException(e.getMessage(), e);
}
}
String proxyUsername = params.getParameter(SharePointConfig.PARAM_PROXYUSER);
String proxyPassword = params.getObfuscatedParameter(SharePointConfig.PARAM_PROXYPASSWORD);
String proxyDomain = params.getParameter(SharePointConfig.PARAM_PROXYDOMAIN);
serverUrl = serverProtocol + "://" + serverName;
if (serverProtocol.equals("https")) {
if (serverPort != 443)
serverUrl += ":" + Integer.toString(serverPort);
} else {
if (serverPort != 80)
serverUrl += ":" + Integer.toString(serverPort);
}
fileBaseUrl = serverUrl + encodedServerLocation;
// Set up ssl if indicated
keystoreData = params.getParameter(SharePointConfig.PARAM_SERVERKEYSTORE);
int connectionTimeout = 60000;
int socketTimeout = 900000;
SSLConnectionSocketFactory myFactory = null;
if (keystoreData != null) {
keystoreManager = KeystoreManagerFactory.make("", keystoreData);
myFactory = new SSLConnectionSocketFactory(keystoreManager.getSecureSocketFactory(), new DefaultHostnameVerifier());
} else {
myFactory = SSLConnectionSocketFactory.getSocketFactory();
}
PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", myFactory).build());
poolingConnectionManager.setDefaultMaxPerRoute(1);
poolingConnectionManager.setValidateAfterInactivity(2000);
poolingConnectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(socketTimeout).build());
connectionManager = poolingConnectionManager;
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (strippedUserName != null) {
credentialsProvider.setCredentials(new AuthScope(serverName, serverPort), new NTCredentials(strippedUserName, password, currentHost, ntlmDomain));
}
RequestConfig.Builder requestBuilder = RequestConfig.custom().setCircularRedirectsAllowed(true).setSocketTimeout(socketTimeout).setExpectContinueEnabled(false).setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(socketTimeout);
// If there's a proxy, set that too.
if (proxyHost != null && proxyHost.length() > 0) {
// Configure proxy authentication
if (proxyUsername != null && proxyUsername.length() > 0) {
if (proxyPassword == null)
proxyPassword = "";
if (proxyDomain == null)
proxyDomain = "";
credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, currentHost, proxyDomain));
}
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
requestBuilder.setProxy(proxy);
}
HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager).disableAutomaticRetries().setDefaultRequestConfig(requestBuilder.build()).setDefaultCredentialsProvider(credentialsProvider);
builder.setRequestExecutor(new HttpRequestExecutor(socketTimeout)).setRedirectStrategy(new DefaultRedirectStrategy());
httpClient = builder.build();
proxy = new SPSProxyHelper(serverUrl, encodedServerLocation, serverLocation, userName, password, org.apache.manifoldcf.connectorcommon.common.CommonsHTTPSender.class, "client-config.wsdd", httpClient);
}
sessionTimeout = System.currentTimeMillis() + sessionExpirationInterval;
}Example 15
| Project: dungproxy-master File: CrawlerHttpClientBuilder.java View source code |
public CrawlerHttpClient build() {
// Create main request executor
// We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version
PublicSuffixMatcher publicSuffixMatcherCopy = this.publicSuffixMatcher;
if (publicSuffixMatcherCopy == null) {
publicSuffixMatcherCopy = PublicSuffixMatcherLoader.getDefault();
}
HttpRequestExecutor requestExecCopy = this.requestExec;
if (requestExecCopy == null) {
requestExecCopy = new HttpRequestExecutor();
}
HttpClientConnectionManager connManagerCopy = this.connManager;
if (connManagerCopy == null) {
// 连接池
LayeredConnectionSocketFactory sslSocketFactoryCopy = this.sslSocketFactory;
if (sslSocketFactoryCopy == null) {
final String[] supportedProtocols = systemProperties ? split(System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = systemProperties ? split(System.getProperty("https.cipherSuites")) : null;
HostnameVerifier hostnameVerifierCopy = this.hostnameVerifier;
if (hostnameVerifierCopy == null) {
hostnameVerifierCopy = new DefaultHostnameVerifier(publicSuffixMatcherCopy);
}
if (sslContext != null) {
sslSocketFactoryCopy = new SSLConnectionSocketFactory(sslContext, supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
} else {
if (systemProperties) {
sslSocketFactoryCopy = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifierCopy);
} else {
SSLContext httpCertificateIgnoreSSLContext = createHttpCertificateIgnoreSSLContext();
if (httpCertificateIgnoreSSLContext == null) {
httpCertificateIgnoreSSLContext = SSLContexts.createDefault();
}
sslSocketFactoryCopy = new SSLConnectionSocketFactory(httpCertificateIgnoreSSLContext, hostnameVerifierCopy);
}
}
}
@SuppressWarnings("resource") final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactoryCopy).build(), null, null, dnsResolver, connTimeToLive, connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
if (defaultSocketConfig != null) {
poolingmgr.setDefaultSocketConfig(defaultSocketConfig);
}
if (defaultConnectionConfig != null) {
poolingmgr.setDefaultConnectionConfig(defaultConnectionConfig);
}
if (systemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);
}
}
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
connManagerCopy = poolingmgr;
}
ConnectionReuseStrategy reuseStrategyCopy = this.reuseStrategy;
if (reuseStrategyCopy == null) {
if (systemProperties) {
final String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
reuseStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE;
} else {
reuseStrategyCopy = NoConnectionReuseStrategy.INSTANCE;
}
} else {
reuseStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE;
}
}
ConnectionKeepAliveStrategy keepAliveStrategyCopy = this.keepAliveStrategy;
if (keepAliveStrategyCopy == null) {
keepAliveStrategyCopy = DefaultConnectionKeepAliveStrategy.INSTANCE;
}
AuthenticationStrategy targetAuthStrategyCopy = this.targetAuthStrategy;
if (targetAuthStrategyCopy == null) {
targetAuthStrategyCopy = TargetAuthenticationStrategy.INSTANCE;
}
AuthenticationStrategy proxyAuthStrategyCopy = this.proxyAuthStrategy;
if (proxyAuthStrategyCopy == null) {
proxyAuthStrategyCopy = ProxyAuthenticationStrategy.INSTANCE;
}
UserTokenHandler userTokenHandlerCopy = this.userTokenHandler;
if (userTokenHandlerCopy == null) {
if (!connectionStateDisabled) {
userTokenHandlerCopy = DefaultUserTokenHandler.INSTANCE;
} else {
userTokenHandlerCopy = NoopUserTokenHandler.INSTANCE;
}
}
String userAgentCopy = this.userAgent;
if (userAgentCopy == null) {
if (systemProperties) {
userAgentCopy = System.getProperty("http.agent");
}
if (userAgentCopy == null) {
userAgentCopy = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36";
}
}
ClientExecChain execChain = createMainExec(requestExecCopy, connManagerCopy, reuseStrategyCopy, keepAliveStrategyCopy, new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)), targetAuthStrategyCopy, proxyAuthStrategyCopy, userTokenHandlerCopy);
execChain = decorateMainExec(execChain);
HttpProcessor httpprocessorCopy = this.httpprocessor;
if (httpprocessorCopy == null) {
// 拦截器构建,如果没有设置拦截器,则构建默认拦截器
final HttpProcessorBuilder b = HttpProcessorBuilder.create();
if (requestFirst != null) {
for (final HttpRequestInterceptor i : requestFirst) {
b.addFirst(i);
}
}
if (responseFirst != null) {
for (final HttpResponseInterceptor i : responseFirst) {
b.addFirst(i);
}
}
Collection<? extends Header> defaultHeaders = this.defaultHeaders;
if (defaultHeaders == null) {
// CommonUtil.defaultHeader();
defaultHeaders = HeaderBuilder.create().defaultCommonHeader().buildList();
}
b.addAll(new RequestDefaultHeaders(defaultHeaders), new RequestContent(), new RequestTargetHost(), new RequestClientConnControl(), new RequestUserAgent(userAgentCopy), new org.apache.http.client.protocol.RequestExpectContinue());
if (multiUserCookieSpaceEnable) {
b.add(new DungProxyRequestAddCookies());
} else {
if (!cookieManagementDisabled) {
b.add(new RequestAddCookies());
}
}
if (!contentCompressionDisabled) {
if (contentDecoderMap != null) {
final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
Collections.sort(encodings);
b.add(new RequestAcceptEncoding(encodings));
} else {
b.add(new RequestAcceptEncoding());
}
}
if (!authCachingDisabled) {
b.add(new RequestAuthCache());
}
if (multiUserCookieSpaceEnable) {
b.add(new DungProxyResponseProcessCookies());
} else {
if (!cookieManagementDisabled) {
b.add(new ResponseProcessCookies());
}
}
if (!contentCompressionDisabled) {
if (contentDecoderMap != null) {
final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
for (final Map.Entry<String, InputStreamFactory> entry : contentDecoderMap.entrySet()) {
b2.register(entry.getKey(), entry.getValue());
}
b.add(new ResponseContentEncoding(b2.build()));
} else {
b.add(new ResponseContentEncoding());
}
}
if (requestLast != null) {
for (final HttpRequestInterceptor i : requestLast) {
b.addLast(i);
}
}
if (responseLast != null) {
for (final HttpResponseInterceptor i : responseLast) {
b.addLast(i);
}
}
httpprocessorCopy = b.build();
}
execChain = new ProtocolExec(execChain, httpprocessorCopy);
execChain = decorateProtocolExec(execChain);
// Add request retry executor, if not disabled
if (!automaticRetriesDisabled) {
HttpRequestRetryHandler retryHandlerCopy = this.retryHandler;
if (retryHandlerCopy == null) {
retryHandlerCopy = DunProxyHttpRequestRetryHandler.INSTANCE;
}
execChain = new RetryExec(execChain, retryHandlerCopy);
}
HttpRoutePlanner routePlannerCopy = this.routePlanner;
if (routePlannerCopy == null) {
SchemePortResolver schemePortResolverCopy = this.schemePortResolver;
if (schemePortResolverCopy == null) {
schemePortResolverCopy = DefaultSchemePortResolver.INSTANCE;
}
if (proxy != null) {
routePlannerCopy = new DefaultProxyRoutePlanner(proxy, schemePortResolverCopy);
} else if (systemProperties) {
routePlannerCopy = new SystemDefaultRoutePlanner(schemePortResolverCopy, ProxySelector.getDefault());
} else {
routePlannerCopy = new ProxyBindRoutPlanner(schemePortResolverCopy);
}
}
// Add redirect executor, if not disabled
if (!redirectHandlingDisabled) {
RedirectStrategy redirectStrategyCopy = this.redirectStrategy;
if (redirectStrategyCopy == null) {
redirectStrategyCopy = DefaultRedirectStrategy.INSTANCE;
}
execChain = new RedirectExec(execChain, routePlannerCopy, redirectStrategyCopy);
}
// Optionally, add service unavailable retry executor
final ServiceUnavailableRetryStrategy serviceUnavailStrategyCopy = this.serviceUnavailStrategy;
if (serviceUnavailStrategyCopy != null) {
execChain = new ServiceUnavailableRetryExec(execChain, serviceUnavailStrategyCopy);
}
// Optionally, add connection back-off executor
if (this.backoffManager != null && this.connectionBackoffStrategy != null) {
execChain = new BackoffStrategyExec(execChain, this.connectionBackoffStrategy, this.backoffManager);
}
Lookup<AuthSchemeProvider> authSchemeRegistryCopy = this.authSchemeRegistry;
if (authSchemeRegistryCopy == null) {
authSchemeRegistryCopy = 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();
}
Lookup<CookieSpecProvider> cookieSpecRegistryCopy = this.cookieSpecRegistry;
if (cookieSpecRegistryCopy == null) {
final CookieSpecProvider defaultProvider = new DefaultCookieSpecProvider(publicSuffixMatcherCopy);
final CookieSpecProvider laxStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.RELAXED, publicSuffixMatcherCopy);
final CookieSpecProvider strictStandardProvider = new RFC6265CookieSpecProvider(RFC6265CookieSpecProvider.CompatibilityLevel.STRICT, publicSuffixMatcherCopy);
cookieSpecRegistryCopy = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.DEFAULT, defaultProvider).register("best-match", defaultProvider).register("compatibility", defaultProvider).register(CookieSpecs.STANDARD, laxStandardProvider).register(CookieSpecs.STANDARD_STRICT, strictStandardProvider).register(CookieSpecs.NETSCAPE, new NetscapeDraftSpecProvider()).register(CookieSpecs.IGNORE_COOKIES, new IgnoreSpecProvider()).build();
}
CookieStore defaultCookieStore = this.cookieStore;
if (defaultCookieStore == null) {
if (multiUserCookieSpaceEnable) {
defaultCookieStore = new MultiUserCookieStore();
} else {
defaultCookieStore = new BasicCookieStore();
}
}
CredentialsProvider defaultCredentialsProvider = this.credentialsProvider;
if (defaultCredentialsProvider == null) {
if (systemProperties) {
defaultCredentialsProvider = new SystemDefaultCredentialsProvider();
} else {
defaultCredentialsProvider = new BasicCredentialsProvider();
}
}
List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
if (!this.connManagerShared) {
if (closeablesCopy == null) {
closeablesCopy = new ArrayList<Closeable>(1);
}
final HttpClientConnectionManager cm = connManagerCopy;
if (evictExpiredConnections || evictIdleConnections) {
final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm, maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS);
closeablesCopy.add(new Closeable() {
@Override
public void close() throws IOException {
connectionEvictor.shutdown();
}
});
connectionEvictor.start();
}
closeablesCopy.add(new Closeable() {
@Override
public void close() throws IOException {
cm.shutdown();
}
});
}
return new CrawlerHttpClient(execChain, connManagerCopy, routePlannerCopy, cookieSpecRegistryCopy, authSchemeRegistryCopy, defaultCookieStore, defaultCredentialsProvider, defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT, closeablesCopy, charsetCacheEnable);
}Example 16
| Project: hive-master File: HiveConnection.java View source code |
private CloseableHttpClient getHttpClient(Boolean useSsl) throws SQLException {
boolean isCookieEnabled = sessConfMap.get(JdbcConnectionParams.COOKIE_AUTH) == null || (!JdbcConnectionParams.COOKIE_AUTH_FALSE.equalsIgnoreCase(sessConfMap.get(JdbcConnectionParams.COOKIE_AUTH)));
String cookieName = sessConfMap.get(JdbcConnectionParams.COOKIE_NAME) == null ? JdbcConnectionParams.DEFAULT_COOKIE_NAMES_HS2 : sessConfMap.get(JdbcConnectionParams.COOKIE_NAME);
CookieStore cookieStore = isCookieEnabled ? new BasicCookieStore() : null;
HttpClientBuilder httpClientBuilder;
// Request interceptor for any request pre-processing logic
HttpRequestInterceptor requestInterceptor;
Map<String, String> additionalHttpHeaders = new HashMap<String, String>();
// Retrieve the additional HttpHeaders
for (Map.Entry<String, String> entry : sessConfMap.entrySet()) {
String key = entry.getKey();
if (key.startsWith(JdbcConnectionParams.HTTP_HEADER_PREFIX)) {
additionalHttpHeaders.put(key.substring(JdbcConnectionParams.HTTP_HEADER_PREFIX.length()), entry.getValue());
}
}
// Configure http client for kerberos/password based authentication
if (isKerberosAuthMode()) {
/**
* Add an interceptor which sets the appropriate header in the request.
* It does the kerberos authentication and get the final service ticket,
* for sending to the server before every request.
* In https mode, the entire information is encrypted
*/
requestInterceptor = new HttpKerberosRequestInterceptor(sessConfMap.get(JdbcConnectionParams.AUTH_PRINCIPAL), host, getServerHttpUrl(useSsl), assumeSubject, cookieStore, cookieName, useSsl, additionalHttpHeaders);
} else {
// Check for delegation token, if present add it in the header
String tokenStr = getClientDelegationToken(sessConfMap);
if (tokenStr != null) {
requestInterceptor = new HttpTokenAuthInterceptor(tokenStr, cookieStore, cookieName, useSsl, additionalHttpHeaders);
} else {
/**
* Add an interceptor to pass username/password in the header.
* In https mode, the entire information is encrypted
*/
requestInterceptor = new HttpBasicAuthInterceptor(getUserName(), getPassword(), cookieStore, cookieName, useSsl, additionalHttpHeaders);
}
}
// Configure http client for cookie based authentication
if (isCookieEnabled) {
// Create a http client with a retry mechanism when the server returns a status code of 401.
httpClientBuilder = HttpClients.custom().setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
@Override
public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) {
int statusCode = response.getStatusLine().getStatusCode();
boolean ret = statusCode == 401 && executionCount <= 1;
// interceptor
if (ret) {
context.setAttribute(Utils.HIVE_SERVER2_RETRY_KEY, Utils.HIVE_SERVER2_RETRY_TRUE);
}
return ret;
}
@Override
public long getRetryInterval() {
// Immediate retry
return 0;
}
});
} else {
httpClientBuilder = HttpClientBuilder.create();
}
// In case the server's idletimeout is set to a lower value, it might close it's side of
// connection. However we retry one more time on NoHttpResponseException
httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount > 1) {
LOG.info("Retry attempts to connect to server exceeded.");
return false;
}
if (exception instanceof org.apache.http.NoHttpResponseException) {
LOG.info("Could not connect to the server. Retrying one more time.");
return true;
}
return false;
}
});
// Add the request interceptor to the client builder
httpClientBuilder.addInterceptorFirst(requestInterceptor);
// Add an interceptor to add in an XSRF header
httpClientBuilder.addInterceptorLast(new XsrfHttpRequestInterceptor());
// Configure http client for SSL
if (useSsl) {
String useTwoWaySSL = sessConfMap.get(JdbcConnectionParams.USE_TWO_WAY_SSL);
String sslTrustStorePath = sessConfMap.get(JdbcConnectionParams.SSL_TRUST_STORE);
String sslTrustStorePassword = sessConfMap.get(JdbcConnectionParams.SSL_TRUST_STORE_PASSWORD);
KeyStore sslTrustStore;
SSLConnectionSocketFactory socketFactory;
SSLContext sslContext;
/**
* The code within the try block throws: SSLInitializationException, KeyStoreException,
* IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException &
* UnrecoverableKeyException. We don't want the client to retry on any of these,
* hence we catch all and throw a SQLException.
*/
try {
if (useTwoWaySSL != null && useTwoWaySSL.equalsIgnoreCase(JdbcConnectionParams.TRUE)) {
socketFactory = getTwoWaySSLSocketFactory();
} else if (sslTrustStorePath == null || sslTrustStorePath.isEmpty()) {
// Create a default socket factory based on standard JSSE trust material
socketFactory = SSLConnectionSocketFactory.getSocketFactory();
} else {
// Pick trust store config from the given path
sslTrustStore = KeyStore.getInstance(JdbcConnectionParams.SSL_TRUST_STORE_TYPE);
try (FileInputStream fis = new FileInputStream(sslTrustStorePath)) {
sslTrustStore.load(fis, sslTrustStorePassword.toCharArray());
}
sslContext = SSLContexts.custom().loadTrustMaterial(sslTrustStore, null).build();
socketFactory = new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier(null));
}
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", socketFactory).build();
httpClientBuilder.setConnectionManager(new BasicHttpClientConnectionManager(registry));
} catch (Exception e) {
String msg = "Could not create an https connection to " + jdbcUriString + ". " + e.getMessage();
throw new SQLException(msg, " 08S01", e);
}
}
return httpClientBuilder.build();
}Example 17
| Project: WebServices-master File: HTTPClientRequestExecuter.java View source code |
@Override
public void execute(Request request, View... views) {
// Verify if this is the first call to this object:
if (isRequestStarted) {
throw new MultipleRequestInSameRequestExecuterException("A RequestExecuter object can be used only once!");
}
isRequestStarted = true;
// Proceed with execution:
for (View view : views) {
view.doStart(request);
}
// Needed for specifying HTTP pre-emptive authentication:
HttpContext httpContext = null;
// Create all the builder objects:
final HttpClientBuilder hcBuilder = HttpClientBuilder.create();
final RequestConfig.Builder rcBuilder = RequestConfig.custom();
final RequestBuilder reqBuilder = RequestBuilder.create(request.getMethod().name());
// Retry handler (no-retries):
hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
// Url:
final URL url = IDNUtil.getIDNizedURL(request.getUrl());
final String urlHost = url.getHost();
final int urlPort = url.getPort() == -1 ? url.getDefaultPort() : url.getPort();
final String urlProtocol = url.getProtocol();
final String urlStr = url.toString();
reqBuilder.setUri(urlStr);
// Set HTTP version:
HTTPVersion httpVersion = request.getHttpVersion();
ProtocolVersion protocolVersion = httpVersion == HTTPVersion.HTTP_1_1 ? new ProtocolVersion("HTTP", 1, 1) : new ProtocolVersion("HTTP", 1, 0);
reqBuilder.setVersion(protocolVersion);
// Set request timeout (default 1 minute--60000 milliseconds)
IGlobalOptions options = ServiceLocator.getInstance(IGlobalOptions.class);
rcBuilder.setConnectionRequestTimeout(Integer.parseInt(options.getProperty("request-timeout-in-millis")));
// Set proxy
ProxyConfig proxy = ProxyConfig.getInstance();
proxy.acquire();
if (proxy.isEnabled()) {
final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http");
if (proxy.isAuthEnabled()) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword())));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
hcBuilder.setProxy(proxyHost);
}
proxy.release();
// HTTP Authentication
if (request.getAuth() != null) {
// Add auth preference:
Auth auth = request.getAuth();
List<String> authPrefs = new ArrayList<>();
if (auth instanceof BasicAuth) {
authPrefs.add(AuthSchemes.BASIC);
} else if (auth instanceof DigestAuth) {
authPrefs.add(AuthSchemes.DIGEST);
} else if (auth instanceof NtlmAuth) {
authPrefs.add(AuthSchemes.NTLM);
}
rcBuilder.setTargetPreferredAuthSchemes(authPrefs);
// BASIC & DIGEST:
if (auth instanceof BasicAuth || auth instanceof DigestAuth) {
BasicDigestAuth a = (BasicDigestAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
String host = StringUtil.isEmpty(a.getHost()) ? urlHost : a.getHost();
String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, urlPort, realm), new UsernamePasswordCredentials(uid, pwd));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
// preemptive mode:
if (a.isPreemptive()) {
AuthCache authCache = new BasicAuthCache();
AuthSchemeBase authScheme = a instanceof BasicAuth ? new BasicScheme() : new DigestScheme();
authCache.put(new HttpHost(urlHost, urlPort, urlProtocol), authScheme);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
httpContext = localContext;
}
}
// NTLM:
if (auth instanceof NtlmAuth) {
NtlmAuth a = (NtlmAuth) auth;
String uid = a.getUsername();
String pwd = new String(a.getPassword());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new NTCredentials(uid, pwd, a.getWorkstation(), a.getDomain()));
hcBuilder.setDefaultCredentialsProvider(credsProvider);
}
// Authorization header
// Logic written in same place where Header is processed--a little down!
}
try {
{
// Authorization Header Authentication:
Auth auth = request.getAuth();
if (auth != null && auth instanceof AuthorizationHeaderAuth) {
AuthorizationHeaderAuth a = (AuthorizationHeaderAuth) auth;
final String authHeader = a.getAuthorizationHeaderValue();
if (StringUtil.isNotEmpty(authHeader)) {
Header header = new BasicHeader("Authorization", authHeader);
reqBuilder.addHeader(header);
}
}
}
// Get request headers
MultiValueMap<String, String> header_data = request.getHeaders();
for (String key : header_data.keySet()) {
for (String value : header_data.get(key)) {
Header header = new BasicHeader(key, value);
reqBuilder.addHeader(header);
}
}
// Cookies
{
// Set cookie policy:
rcBuilder.setCookieSpec(CookieSpecs.DEFAULT);
// Add to CookieStore:
CookieStore store = new RESTClientCookieStore();
List<HttpCookie> cookies = request.getCookies();
for (HttpCookie cookie : cookies) {
BasicClientCookie c = new BasicClientCookie(cookie.getName(), cookie.getValue());
c.setVersion(cookie.getVersion());
c.setDomain(urlHost);
c.setPath("/");
store.addCookie(c);
}
// Attach store to client:
hcBuilder.setDefaultCookieStore(store);
}
// POST/PUT/PATCH/DELETE method specific logic
if (HttpUtil.isEntityEnclosingMethod(reqBuilder.getMethod())) {
// Create and set RequestEntity
ReqEntity bean = request.getBody();
if (bean != null) {
try {
if (bean instanceof ReqEntitySimple) {
AbstractHttpEntity e = HTTPClientUtil.getEntity((ReqEntitySimple) bean);
reqBuilder.setEntity(e);
} else if (bean instanceof ReqEntityMultipart) {
ReqEntityMultipart multipart = (ReqEntityMultipart) bean;
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
// multipart/mixed / multipart/form-data:
meb.setMimeSubtype(multipart.getSubtype().toString());
// Format:
MultipartMode mpMode = multipart.getMode();
switch(mpMode) {
case BROWSER_COMPATIBLE:
meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
break;
case RFC_6532:
meb.setMode(HttpMultipartMode.RFC6532);
break;
case STRICT:
meb.setMode(HttpMultipartMode.STRICT);
break;
}
// Parts:
for (ReqEntityPart part : multipart.getBody()) {
ContentBody cb = null;
if (part instanceof ReqEntityStringPart) {
ReqEntityStringPart p = (ReqEntityStringPart) part;
String body = p.getPart();
ContentType ct = p.getContentType();
if (ct != null) {
cb = new StringBody(body, HTTPClientUtil.getContentType(ct));
} else {
cb = new StringBody(body, org.apache.http.entity.ContentType.DEFAULT_TEXT);
}
} else if (part instanceof ReqEntityFilePart) {
ReqEntityFilePart p = (ReqEntityFilePart) part;
File body = p.getPart();
ContentType ct = p.getContentType();
if (ct != null) {
cb = new FileBody(body, HTTPClientUtil.getContentType(ct), p.getFilename());
} else {
cb = new FileBody(body, org.apache.http.entity.ContentType.DEFAULT_BINARY, p.getFilename());
}
}
FormBodyPartBuilder bodyPart = FormBodyPartBuilder.create().setName(part.getName()).setBody(cb);
MultiValueMap<String, String> fields = part.getFields();
for (String key : fields.keySet()) {
for (String value : fields.get(key)) {
bodyPart.addField(key, value);
}
}
meb.addPart(bodyPart.build());
}
reqBuilder.setEntity(meb.build());
}
} catch (UnsupportedEncodingException ex) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
view.doEnd();
}
return;
}
}
}
// SSL
// Set the hostname verifier:
final SSLReq sslReq = request.getSslReq();
if (sslReq != null) {
SSLHostnameVerifier verifier = sslReq.getHostNameVerifier();
final HostnameVerifier hcVerifier;
switch(verifier) {
case ALLOW_ALL:
hcVerifier = new NoopHostnameVerifier();
break;
case STRICT:
default:
hcVerifier = new DefaultHostnameVerifier();
break;
}
// Register the SSL Scheme:
final KeyStore trustStore = sslReq.getTrustStore() == null ? null : sslReq.getTrustStore().getKeyStore();
final KeyStore keyStore = sslReq.getKeyStore() == null ? null : sslReq.getKeyStore().getKeyStore();
final TrustStrategy trustStrategy = sslReq.isTrustAllCerts() ? new TrustAllTrustStrategy() : null;
SSLContext ctx = new SSLContextBuilder().loadKeyMaterial(keyStore, sslReq.getKeyStore() != null ? sslReq.getKeyStore().getPassword() : null).loadTrustMaterial(trustStore, trustStrategy).setSecureRandom(null).useProtocol("TLS").build();
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(ctx, hcVerifier);
hcBuilder.setSSLSocketFactory(sf);
}
// How to handle redirects:
rcBuilder.setRedirectsEnabled(request.isFollowRedirect());
// Now Execute:
long startTime = System.currentTimeMillis();
RequestConfig rc = rcBuilder.build();
reqBuilder.setConfig(rc);
HttpUriRequest req = reqBuilder.build();
httpClient = hcBuilder.build();
HttpResponse http_res = httpClient.execute(req, httpContext);
long endTime = System.currentTimeMillis();
// Create response:
ResponseBean response = new ResponseBean();
response.setExecutionTime(endTime - startTime);
response.setStatusCode(http_res.getStatusLine().getStatusCode());
response.setStatusLine(http_res.getStatusLine().toString());
final Header[] responseHeaders = http_res.getAllHeaders();
for (Header header : responseHeaders) {
response.addHeader(header.getName(), header.getValue());
}
// Response body:
final HttpEntity entity = http_res.getEntity();
if (entity != null) {
if (request.isIgnoreResponseBody()) {
EntityUtils.consumeQuietly(entity);
} else {
InputStream is = entity.getContent();
try {
byte[] responseBody = StreamUtil.inputStream2Bytes(is);
if (responseBody != null) {
response.setResponseBody(responseBody);
}
} catch (IOException ex) {
for (View view : views) {
view.doError("Byte array conversion from response body stream failed.");
}
LOG.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
// Now execute tests:
try {
junit.framework.TestSuite suite = TestUtil.getTestSuite(request, response);
if (suite != null) {
// suite will be null if there is no associated script
TestResult testResult = TestUtil.execute(suite);
response.setTestResult(testResult);
}
} catch (TestException ex) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
}
}
for (View view : views) {
view.doResponse(response);
}
} catch (IOExceptionKeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException | KeyManagementException | IllegalStateException | ex) {
if (!interruptedShutdown) {
for (View view : views) {
view.doError(Util.getStackTrace(ex));
}
} else {
for (View view : views) {
view.doCancelled();
}
}
} finally {
if (!interruptedShutdown) {
// close it only when otherwise:
try {
if (httpClient != null)
httpClient.close();
} catch (IOException ex) {
LOG.log(Level.WARNING, "Exception when closing httpClient", ex);
}
} else {
// reset value to default:
interruptedShutdown = false;
}
for (View view : views) {
view.doEnd();
}
isRequestCompleted = true;
}
}Example 18
| Project: knowledgestore-master File: Client.java View source code |
private static PoolingHttpClientConnectionManager createConnectionManager(final int maxConnections, final boolean validateServer) {
// Setup SSLContext and HostnameVerifier based on validateServer parameter
final SSLContext sslContext;
HostnameVerifier hostVerifier;
try {
if (validateServer) {
sslContext = SSLContext.getDefault();
hostVerifier = new DefaultHostnameVerifier();
} else {
sslContext = SSLContext.getInstance(Protocol.HTTPS_PROTOCOLS[0]);
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(final X509Certificate[] chain, final String authType) {
}
@Override
public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} }, null);
hostVerifier = NoopHostnameVerifier.INSTANCE;
}
} catch (final Throwable ex) {
throw new RuntimeException("SSL configuration failed", ex);
}
// Create HTTP connection factory
final ConnectionSocketFactory httpConnectionFactory = PlainConnectionSocketFactory.getSocketFactory();
// Create HTTPS connection factory
final ConnectionSocketFactory httpsConnectionFactory = new SSLConnectionSocketFactory(sslContext, Protocol.HTTPS_PROTOCOLS, Protocol.HTTPS_CIPHER_SUITES, hostVerifier);
// Create pooled connection manager
final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpConnectionFactory).register("https", httpsConnectionFactory).build());
// Setup max concurrent connections
manager.setMaxTotal(maxConnections);
manager.setDefaultMaxPerRoute(maxConnections);
// validate connection after 1s idle
manager.setValidateAfterInactivity(1000);
return manager;
}Example 19
| Project: cas-master File: CasCoreHttpConfiguration.java View source code |
@ConditionalOnMissingBean(name = "hostnameVerifier")
@Bean
public HostnameVerifier hostnameVerifier() {
if (casProperties.getHttpClient().getHostNameVerifier().equalsIgnoreCase("none")) {
return NoopHostnameVerifier.INSTANCE;
}
return new DefaultHostnameVerifier();
}Example 20
| Project: saki-monkey-master File: AbstractMandrillClient.java View source code |
/**
*
* @return default HostnameVerifier object
*/
protected HostnameVerifier createHostnameVerifier() {
return new DefaultHostnameVerifier();
}Example 21
| Project: gradle-master File: HttpClientConfigurer.java View source code |
private void configureSslSocketConnectionFactory(HttpClientBuilder builder, SslContextFactory sslContextFactory) {
builder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextFactory.createSslContext(), new DefaultHostnameVerifier(null)));
}