use of org.apache.http.conn.HttpClientConnectionManager in project jersey by jersey.
the class HelloWorldTest method testAsyncClientRequests.
@Test
public void testAsyncClientRequests() throws InterruptedException {
HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
ClientConfig cc = new ClientConfig();
cc.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
cc.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(cc);
WebTarget target = client.target(getBaseUri());
final int REQUESTS = 20;
final CountDownLatch latch = new CountDownLatch(REQUESTS);
final long tic = System.currentTimeMillis();
final Map<Integer, String> results = new ConcurrentHashMap<Integer, String>();
for (int i = 0; i < REQUESTS; i++) {
final int id = i;
target.path(ROOT_PATH).request().async().get(new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
try {
final String result = response.readEntity(String.class);
results.put(id, result);
} finally {
latch.countDown();
}
}
@Override
public void failed(Throwable error) {
Logger.getLogger(HelloWorldTest.class.getName()).log(Level.SEVERE, "Failed on throwable", error);
results.put(id, "error: " + error.getMessage());
latch.countDown();
}
});
}
assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
final long toc = System.currentTimeMillis();
Logger.getLogger(HelloWorldTest.class.getName()).info("Executed in: " + (toc - tic));
StringBuilder resultInfo = new StringBuilder("Results:\n");
for (int i = 0; i < REQUESTS; i++) {
String result = results.get(i);
resultInfo.append(i).append(": ").append(result).append('\n');
}
Logger.getLogger(HelloWorldTest.class.getName()).info(resultInfo.toString());
for (int i = 0; i < REQUESTS; i++) {
String result = results.get(i);
assertEquals(HelloWorldResource.CLICHED_MESSAGE, result);
}
}
use of org.apache.http.conn.HttpClientConnectionManager in project wildfly by wildfly.
the class WebSecurityCERTTestCase method getHttpsClient.
private static CloseableHttpClient getHttpsClient(String alias) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
JBossJSSESecurityDomain jsseSecurityDomain = new JBossJSSESecurityDomain("client-cert");
jsseSecurityDomain.setKeyStorePassword("changeit");
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
URL keystore = tccl.getResource("security/client.keystore");
jsseSecurityDomain.setKeyStoreURL(keystore.getPath());
jsseSecurityDomain.setClientAlias(alias);
jsseSecurityDomain.reloadKeyAndTrustStore();
KeyManager[] keyManagers = jsseSecurityDomain.getKeyManagers();
TrustManager[] trustManagers = jsseSecurityDomain.getTrustManagers();
ctx.init(keyManagers, trustManagers, null);
HostnameVerifier verifier = (string, ssls) -> true;
//SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, verifier);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
return HttpClientBuilder.create().setSSLSocketFactory(ssf).setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm).build();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
use of org.apache.http.conn.HttpClientConnectionManager in project ats-framework by Axway.
the class HttpsClient method connect.
/**
* Connect to a remote host using basic authentication.
*
* @param hostname the host to connect to
* @param userName the user name
* @param password the password for the provided user name
* @throws FileTransferException
*/
@Override
public void connect(String hostname, String userName, String password) throws FileTransferException {
super.connect(hostname, userName, password);
// trust everybody
try {
SSLContext sslContext = SslUtils.getTrustAllSSLContext();
SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext, encryptionProtocols, cipherSuites, new NoopHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
HttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
this.httpBuilder.setConnectionManager(connectionManager).setSchemePortResolver(new DefaultSchemePortResolver());
this.httpClient = this.httpBuilder.build();
} catch (Exception e) {
throw new FileTransferException("Error setting trust manager", e);
}
}
use of org.apache.http.conn.HttpClientConnectionManager in project cuba by cuba-platform.
the class IdpSessionPingConnector method pingIdpSessionServer.
public void pingIdpSessionServer(String idpSessionId) {
log.debug("Ping IDP session {}", idpSessionId);
String idpBaseURL = webIdpConfig.getIdpBaseURL();
if (!idpBaseURL.endsWith("/")) {
idpBaseURL += "/";
}
String idpSessionPingUrl = idpBaseURL + "service/ping";
HttpPost httpPost = new HttpPost(idpSessionPingUrl);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("idpSessionId", idpSessionId), new BasicNameValuePair("trustedServicePassword", webIdpConfig.getIdpTrustedServicePassword())), StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
try {
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 410) {
// we have to logout user
log.debug("IDP session is expired {}", idpSessionId);
if (userSessionSource.checkCurrentUserSession()) {
authenticationService.logout();
UserSession userSession = userSessionSource.getUserSession();
throw new NoUserSessionException(userSession.getId());
}
}
if (statusCode != 200) {
log.warn("IDP respond status {} on session ping", statusCode);
}
} catch (IOException e) {
log.warn("Unable to ping IDP {} session {}", idpSessionPingUrl, idpSessionId, e);
} finally {
connectionManager.shutdown();
}
}
use of org.apache.http.conn.HttpClientConnectionManager in project cuba by cuba-platform.
the class IdpAuthController method getIdpSession.
@Nullable
protected IdpSession getIdpSession(String idpTicket) throws InvalidGrantException {
String idpBaseURL = this.idpBaseURL;
if (!idpBaseURL.endsWith("/")) {
idpBaseURL += "/";
}
String idpTicketActivateUrl = idpBaseURL + "service/activate";
HttpPost httpPost = new HttpPost(idpTicketActivateUrl);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("serviceProviderTicket", idpTicket), new BasicNameValuePair("trustedServicePassword", idpTrustedServicePassword)), StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
String idpResponse;
try {
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 410) {
// used old ticket
return null;
}
if (statusCode != 200) {
throw new RuntimeException("Idp respond with status " + statusCode);
}
idpResponse = new BasicResponseHandler().handleResponse(httpResponse);
} catch (IOException e) {
throw new RuntimeException("Unable to connect to IDP", e);
} finally {
connectionManager.shutdown();
}
IdpSession session;
try {
session = new Gson().fromJson(idpResponse, IdpSession.class);
} catch (JsonSyntaxException e) {
throw new RuntimeException("Unable to parse idp response", e);
}
return session;
}
Aggregations