@Override public boolean execute(final EnvironmentContext ctx, final InfrastructureComponent component) { val defaultRequestConfig = RequestConfig.custom() .setConnectTimeout(2000) .setSocketTimeout(2000) .setConnectionRequestTimeout(2000) .build(); val httpClient = HttpClients.custom() .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(defaultRequestConfig) .build(); await().atMost(atMost.toMillis(), TimeUnit.MILLISECONDS).until(() -> { try { val response = httpClient.execute(new HttpGet(url)); val result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); return containsText != null && result.contains(containsText); } catch(final Exception ignored) { return false; } }); return true; }
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom() .setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000) .setSocketTimeout(20 * 1000) .setMaxRedirects(20) .build(); URL mmsc = new URL(apn.getMmsc()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (apn.hasAuthentication()) { credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword())); } return HttpClients.custom() .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()) .setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT)) .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(config) .setDefaultCredentialsProvider(credsProvider) .build(); }
protected CloseableHttpClient createHttpClient(Map<String, Object> parameters) { HttpClientBuilder clientBuilder = HttpClients.custom(); // single connection BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(); clientBuilder.setConnectionManager(connManager); // ignore cookies for now RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); clientBuilder.setDefaultRequestConfig(requestConfig); setAuthentication(parameters, clientBuilder); CloseableHttpClient client = clientBuilder.build(); return client; }
public ProcessConnection(ProcessDirector director, PhantomJSProcess process) { this.process = process; HttpClientBuilder clientBuilder = HttpClients.custom(); // single connection BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(); clientBuilder.setConnectionManager(connManager); RequestConfig requestConfig = RequestConfig.custom() // ignore cookies for now .setCookieSpec(CookieSpecs.IGNORE_COOKIES) .setSocketTimeout(director.getRequestTimeout()).build(); clientBuilder.setDefaultRequestConfig(requestConfig); this.httpClient = clientBuilder.build(); }
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom() .setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000) .setSocketTimeout(20 * 1000) .setMaxRedirects(20) .build(); URL mmsc = new URL(apn.getMmsc()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (apn.hasAuthentication()) { credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword())); } return HttpClients.custom() .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()) .setUserAgent("Android-Mms/2.0") .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(config) .setDefaultCredentialsProvider(credsProvider) .build(); }
public SimpleHttpClient(final SSLContext sslContext, final int listenPort, final boolean useCompression) { HttpClientBuilder builder = HttpClientBuilder.create(); if (!useCompression) { builder.disableContentCompression(); } if (sslContext != null) { SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory( sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); builder.setSSLSocketFactory(sslConnectionFactory); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionFactory) .build(); builder.setConnectionManager(new BasicHttpClientConnectionManager(registry)); scheme = "https"; } else { scheme = "http"; } this.delegate = builder.build(); this.listenPort = listenPort; }
public void performLogoutOnServiceProviders(String idpSessionId) { List<String> serviceProviderLogoutUrls = idpConfig.getServiceProviderLogoutUrls(); asyncServiceLogoutExecutor.submit(() -> { HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) .build(); try { for (String serviceProviderLogoutUrl : serviceProviderLogoutUrls) { callLoggedOutServiceUrl(client, serviceProviderLogoutUrl, idpSessionId); } } catch (Throwable ex) { log.error("Unable to perform logout on IDP services for session {}", idpSessionId, ex); } finally { connectionManager.shutdown(); } }); }
public HttpCon createClient() throws Exception { ArrayList<Header> defheaders = new ArrayList<Header>(); defheaders.add(new BasicHeader("X-Csrf-Token", "1")); BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager( cfgMgr.getHttpClientSocketRegistry()); 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; }
public HttpCon createClient() throws Exception { ArrayList<Header> defheaders = new ArrayList<Header>(); BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager( cfgMgr.getHttpClientSocketRegistry()); 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; }
/** * This code sets up the httpclient to accept any SSL certificate. The * SSL certificate generated by the instructions above is not correctly * signed, so we need ignore the problem. * This code should not, under any circumstances, be allowed anywhere * the production code. * @return */ private CloseableHttpClient createClient () { try { HttpClientBuilder builder = HttpClientBuilder.create(); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, new TrustManager[]{getTrustManager()}, null); SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); builder.setSSLSocketFactory(scsf); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", scsf) .build(); HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry); builder.setConnectionManager(ccm); return builder.build(); } catch (Exception ex) { ex.printStackTrace(); return null; } }
protected HttpClientConnectionManager createConnectionManager() { if (httpClientConfig.isMultiThreaded()) { log.debug("Multi-threaded http connection manager created"); final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); final Integer maxTotal = httpClientConfig.getMaxTotalConnection(); if (maxTotal != null) { cm.setMaxTotal(maxTotal); } final Integer defaultMaxPerRoute = httpClientConfig.getDefaultMaxTotalConnectionPerRoute(); if (defaultMaxPerRoute != null) { cm.setDefaultMaxPerRoute(defaultMaxPerRoute); } final Map<HttpRoute, Integer> maxPerRoute = httpClientConfig.getMaxTotalConnectionPerRoute(); for (final HttpRoute route : maxPerRoute.keySet()) { cm.setMaxPerRoute(route, maxPerRoute.get(route)); } return cm; } log.debug("Default http connection is created without multi threaded option"); return new BasicHttpClientConnectionManager(); }
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom() .setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000) .setSocketTimeout(20 * 1000) .setMaxRedirects(20) .build(); URL mmsc = new URL(apn.getMmsc()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (apn.hasAuthentication()) { credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword())); } return HttpClients.custom() .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()) .setUserAgent(SilencePreferences.getMmsUserAgent(context, USER_AGENT)) .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(config) .setDefaultCredentialsProvider(credsProvider) .build(); }
@Bean(destroyMethod = "close") public CloseableHttpClient httpClient() { // cache.itb.ac.id is not very reliable with pooling connection, better dedicated connection per recognition // final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); // cm.setMaxTotal(200); // cm.setDefaultMaxPerRoute(20); // cm.setValidateAfterInactivity(100); final BasicHttpClientConnectionManager basicCm = new BasicHttpClientConnectionManager(); return HttpClients.custom().useSystemProperties().setConnectionManager(basicCm) .setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(10000) .setSocketTimeout(10000) .setConnectionRequestTimeout(10000) .build()) .build(); }
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom() .setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000) .setSocketTimeout(20 * 1000) .setMaxRedirects(20) .build(); return HttpClients.custom() .setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()) .setUserAgent("Android-Mms/2.0") .setConnectionManager(new BasicHttpClientConnectionManager()) .setDefaultRequestConfig(config) .build(); }
/** Creates a new fetching thread. * * @param frontier a reference to the {@link Frontier}. * @param index the index of this thread (only for logging purposes). */ public FetchingThread(final Frontier frontier, final int index) throws NoSuchAlgorithmException, IllegalArgumentException, IOException { setName(this.getClass().getSimpleName() + '-' + index); setPriority(Thread.MIN_PRIORITY); // Low priority; there will be thousands of this guys around. this.frontier = frontier; final BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManagerWithAlternateDNS(frontier.rc.dnsResolver); connManager.closeIdleConnections(0, TimeUnit.MILLISECONDS); connManager.setConnectionConfig(ConnectionConfig.custom().setBufferSize(8 * 1024).build()); // TODO: make this configurable cookieStore = new BasicCookieStore(); BasicHeader[] headers = { new BasicHeader("From", frontier.rc.userAgentFrom), new BasicHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.95,text/*;q=0.9,*/*;q=0.8") }; httpClient = HttpClients.custom() .setSSLContext(frontier.rc.acceptAllCertificates ? TRUST_ALL_CERTIFICATES_SSL_CONTEXT : TRUST_SELF_SIGNED_SSL_CONTEXT) .setConnectionManager(connManager) .setConnectionReuseStrategy(frontier.rc.keepAliveTime == 0 ? NoConnectionReuseStrategy.INSTANCE : DefaultConnectionReuseStrategy.INSTANCE) .setUserAgent(frontier.rc.userAgent) .setDefaultCookieStore(cookieStore) .setDefaultHeaders(ObjectArrayList.wrap(headers)) .build(); fetchData = new FetchData(frontier.rc); }
@BeforeClass public static void setupProxying() { DnsResolver dnsResolver = prepareProxiedDnsResolver(); DefaultSchemePortResolver schemePortResolver = prepareSchemePortResolver(); BasicHttpClientConnectionManager connManager = prepareConnectionManager(dnsResolver, schemePortResolver); HttpClient httpClient = prepareHttpClient(connManager); originalHttpClient = (HttpClient) Options.getOption(Option.HTTPCLIENT); Unirest.setHttpClient(httpClient); }
private static BasicHttpClientConnectionManager prepareConnectionManager(DnsResolver dnsResolver, DefaultSchemePortResolver schemePortResolver) { return new BasicHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(), null, schemePortResolver, dnsResolver ); }
@Before public void requestConnection() throws Exception { HttpHost host = new HttpHost(InetAddress.getLocalHost(), inbound.getPort()); HttpRoute route = new HttpRoute(host, InetAddress.getLocalHost(), false); connManager = new BasicHttpClientConnectionManager(); ConnectionRequest connRequest = connManager.requestConnection(route, null); conn = connRequest.get(10, TimeUnit.SECONDS); // Make two requests on the same connection connManager.connect(conn, route, 10000, new BasicHttpContext()); }
/** * You should call {@link LogDataProvider#obtainUrl()} before * */ @Override public InputStream provide() { HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); String uri = url + "?s=" + userSessionSource.getUserSession().getId(); if (downloadFullLog) { uri += "&full=true"; } HttpGet httpGet = new HttpGet(uri); try { HttpResponse httpResponse = httpClient.execute(httpGet); int httpStatus = httpResponse.getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { inputStream = httpEntity.getContent(); } else { log.debug("Unable to download log from " + url + "\nHttpEntity is null"); throw new RuntimeException("Unable to download log from " + url + "\nHttpEntity is null"); } } else { log.debug("Unable to download log from " + url + "\n" + httpResponse.getStatusLine()); throw new RuntimeException("Unable to download log from " + url + "\n" + httpResponse.getStatusLine()); } } catch (IOException e) { log.debug("Unable to download log from " + url + "\n" + e); throw new RuntimeException(e); } return inputStream; }
@Override public InputStream provide() { String url = restApiUrl + query; HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { inputStream = httpEntity.getContent(); } else { throw new RuntimeException("Unable to retrieve data using REST API from " + url + "\nHttpEntity is null"); } } else { throw new RuntimeException("Unable to retrieve data using REST API from " + url + "\n" + response.getStatusLine()); } } catch (IOException e) { throw new RuntimeException(e); } finally { connectionManager.shutdown(); } return inputStream; }
@Test public void testBasics() throws Exception { this.clientBuilder.setConnectionManager(new BasicHttpClientConnectionManager()); final HttpHost target = start(); final HttpGet get = new HttpGet("/random/1024"); final CloseableHttpResponse response = this.httpclient.execute(target, get); try { Assert.assertEquals(200, response.getStatusLine().getStatusCode()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } }
@Test(expected=IllegalStateException.class) public void testConnectionStillInUse() throws Exception { this.clientBuilder.setConnectionManager(new BasicHttpClientConnectionManager()); final HttpHost target = start(); final HttpGet get1 = new HttpGet("/random/1024"); this.httpclient.execute(target, get1); final HttpGet get2 = new HttpGet("/random/1024"); this.httpclient.execute(target, get2); }
@Test public void testServerErrors() throws Exception { // tests the behaviour of the client when the server sends an error // must use BasicClientConnectionManager to test whether the client is closed correctly BasicHttpClientConnectionManager conMgr = new BasicHttpClientConnectionManager(); Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr); ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null), new PerSessionDirectoryFactory(clientWorkDir)); try { publishRevision(5); try { replicationServlet.setRespondWithError(true); client.updateNow(); fail("expected exception"); } catch (Throwable t) { // expected } replicationServlet.setRespondWithError(false); client.updateNow(); // now it should work reopenReader(); assertEquals(5, Integer.parseInt(reader.getIndexCommit().getUserData().get("ID"), 16)); client.close(); } finally { replicationServlet.setRespondWithError(false); } }
/** * Get a default HttpClient based on the HttpConfiguration object. If required the defaults can * be altered to meet the requirements of the SDK user. The default client does not use connection * pooling and does not reuse connections. Timeouts for connection and socket are taken from the * {@link HttpConfiguration} object. * * @param httpConfiguration * @return CloseableHttpClient */ public static CloseableHttpClient getDefaultClient(HttpConfiguration httpConfiguration) { RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(httpConfiguration.getTimeout()) .setSocketTimeout(httpConfiguration.getTimeout()).build(); HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); ConnectionReuseStrategy connectionResuseStrategy = new NoConnectionReuseStrategy(); logger.debug("Creating HttpClient with simple no pooling/no connection reuse default settings."); CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager) .setConnectionReuseStrategy(connectionResuseStrategy).build(); return httpClient; }
public B fromConfig(Config config) { config = config.withFallback(FALLBACK); RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY)) .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .build(); getHttpClientBuilder().setDefaultRequestConfig(requestConfig); if (config.hasPath(STATIC_SVC_ENDPOINT)) { try { svcEndpoint = Optional.of(new URI(config.getString(AbstractHttpWriterBuilder.STATIC_SVC_ENDPOINT))); } catch (URISyntaxException e) { throw new RuntimeException(e); } } String connMgrStr = config.getString(HTTP_CONN_MANAGER); switch (ConnManager.valueOf(connMgrStr.toUpperCase())) { case BASIC: httpConnManager = new BasicHttpClientConnectionManager(); break; case POOLING: PoolingHttpClientConnectionManager poolingConnMgr = new PoolingHttpClientConnectionManager(); poolingConnMgr.setMaxTotal(config.getInt(POOLING_CONN_MANAGER_MAX_TOTAL_CONN)); poolingConnMgr.setDefaultMaxPerRoute(config.getInt(POOLING_CONN_MANAGER_MAX_PER_CONN)); httpConnManager = poolingConnMgr; break; default: throw new IllegalArgumentException(connMgrStr + " is not supported"); } LOG.info("Using " + httpConnManager.getClass().getSimpleName()); return typedSelf(); }
private synchronized HttpClient getHttpClient () throws KeyManagementException, NoSuchAlgorithmException { if (httpClient == null) { HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60).setConnectionRequestTimeout(60).build(); httpClient = HttpClientBuilder.create().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).build(); } return httpClient; }
private void initializeHttpClient() { Registry<ConnectionSocketFactory> registry = createConnectionSocketFactoryRegistry(); HttpClientConnectionManager httpConnectionManager = new BasicHttpClientConnectionManager(registry); AuthenticationStrategy authStrategy = new CookieProcessingTargetAuthenticationStrategy(); httpClient = HttpClients.custom() .setConnectionManager(httpConnectionManager) .setTargetAuthenticationStrategy(authStrategy) .build(); }
public TransportConnection(SignalFxEndpoint endpoint, int timeoutMs) { super(endpoint, timeoutMs, new BasicHttpClientConnectionManager()); this.transportRequestConfig = RequestConfig.custom().setSocketTimeout(0) .setConnectionRequestTimeout(this.requestConfig.getConnectionRequestTimeout()) .setConnectTimeout(this.requestConfig.getConnectTimeout()) .setProxy(this.requestConfig.getProxy()).build(); log.debug("constructed request config: {}", this.transportRequestConfig.toString()); }
@Nonnull private static HttpClientConnectionManagerBuilder getBasicConnectionManagerBuilder() { return socketConfig -> { BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); connectionManager.setConnectionConfig(HttpClientImmutableFieldHolder.CONNECTION_CONFIG); connectionManager.setSocketConfig(socketConfig); return connectionManager; }; }
@Override public CloseableHttpClient createHttpClient(HttpHost httpHost, boolean acceptAnyCertificate, HttpRequestInterceptor requestInterceptor, HttpResponseInterceptor responseInterceptor) { HttpClientBuilder builder = HttpClientBuilder.create(); //configure proxy from system environment builder.setRoutePlanner(new SystemDefaultRoutePlanner(null)); //accept any certificate if necessary if ("https".equals(httpHost.getSchemeName()) && acceptAnyCertificate) { SSLConnectionSocketFactory icsf = getInsecureSSLSocketFactory(); builder.setSSLSocketFactory(icsf); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", icsf) .build(); HttpClientConnectionManager cm = new BasicHttpClientConnectionManager(registry); builder.setConnectionManager(cm); } //add an interceptor that replaces the invalid Content-Type //'none' by 'identity' builder.addInterceptorFirst(new ContentEncodingNoneInterceptor()); //add interceptors if (requestInterceptor != null) { builder.addInterceptorLast(requestInterceptor); } if (responseInterceptor != null) { builder.addInterceptorLast(responseInterceptor); } CloseableHttpClient client = builder.build(); return client; }
@Override @SuppressWarnings("resource") public IConvertingHttpRequestExecutorBuilder useAlwaysTheSameConnection() { this.connectionManagerProvider = new HttpClientConnectionManagerProvider(new BasicHttpClientConnectionManager()); return this; }
private static CloseableHttpClient prepareHttpClient(BasicHttpClientConnectionManager connManager) { return HttpClientBuilder.create() .setConnectionManager(connManager) .build(); }
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(); } }
protected InputStream openStreamWithServlet(FileDescriptor fd) throws FileStorageException { ClientConfig clientConfig = configuration.getConfig(ClientConfig.class); String fileDownloadContext = clientConfig.getFileDownloadContext(); Object context = serverSelector.initContext(); String selectedUrl = serverSelector.getUrl(context); if (selectedUrl == null) { throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName()); } while (true) { String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&f=" + fd.getId().toString(); HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); int httpStatus = httpResponse.getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { return httpEntity.getContent(); } else { log.debug("Unable to download file from {}\nHttpEntity is null", url); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) { throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName()); } } } else { log.debug("Unable to download file from {}\n{}", url, httpResponse.getStatusLine()); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) { throw new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus), fd.getName()); } } } catch (InterruptedIOException e) { log.trace("Downloading has been interrupted"); throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e); } catch (IOException ex) { log.debug("Unable to download file from {}\n{}", url, ex); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) { throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), ex); } } } }
protected IdpSessionStatus pingIdpSessionServer(String idpSessionId) { log.debug("Ping IDP session {}", idpSessionId); String idpBaseURL = idpConfig.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", idpConfig.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 == HttpStatus.GONE.value()) { log.debug("IDP session expired {}", idpSessionId); return IdpSessionStatus.EXPIRED; } if (statusCode != HttpStatus.OK.value()) { 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(); } return IdpSessionStatus.ALIVE; }
@Override public InputStream provide() { if (filePath == null) throw new IllegalStateException("File path is null"); Object context = serverSelector.initContext(); String selectedUrl = serverSelector.getUrl(context); if (selectedUrl == null) { throw new RuntimeException(String.format("Unable to download file '%s': no available server URLs", filePath)); } while (true) { String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&p=" + URLEncodeUtils.encodeUtf8(filePath); HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); int httpStatus = httpResponse.getStatusLine().getStatusCode(); if (httpStatus == HttpStatus.SC_OK) { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { inputStream = httpEntity.getContent(); break; } else { log.debug("Unable to download file from " + url + "\nHttpEntity is null"); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) throw new RuntimeException(String.format("Unable to download file '%s': HttpEntity is null", filePath)); } } else { log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine()); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) throw new RuntimeException(String.format("Unable to download file '%s': HTTP status is %d", filePath, httpStatus)); } } catch (IOException ex) { log.debug("Unable to download file from " + url + "\n" + ex); selectedUrl = failAndGetNextUrl(context); if (selectedUrl == null) throw new RuntimeException(String.format("Unable to download file '%s'", filePath), ex); } } return inputStream; }
/** * Retrieves certificate chain of specified host:port using https protocol. * * @param host to get certificate chain from (cannot be null) * @param port of host to connect to * @return certificate chain * @throws Exception Re-thrown from accessing the remote host */ public Certificate[] retrieveCertificatesFromHttpsServer(final String host, final int port) throws Exception { checkNotNull(host); log.info("Retrieving certificate from https://{}:{}", host, port); // setup custom connection manager so we can configure SSL to trust-all SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[]{ACCEPT_ALL_TRUST_MANAGER}, null); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register(HttpSchemes.HTTP, PlainConnectionSocketFactory.getSocketFactory()) .register(HttpSchemes.HTTPS, sslSocketFactory).build(); final HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry); try { final AtomicReference<Certificate[]> certificates = new AtomicReference<>(); HttpClient httpClient = httpClientManager.create(new Customizer() { @Override public void customize(final HttpClientPlan plan) { // replace connection-manager with customized version needed to fetch SSL certificates plan.getClient().setConnectionManager(connectionManager); // add interceptor to grab peer-certificates plan.getClient().addInterceptorFirst(new HttpResponseInterceptor() { @Override public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { ManagedHttpClientConnection connection = HttpCoreContext.adapt(context).getConnection(ManagedHttpClientConnection.class); // grab the peer-certificates from the session if (connection != null) { SSLSession session = connection.getSSLSession(); if (session != null) { certificates.set(session.getPeerCertificates()); } } } }); } }); httpClient.execute(new HttpGet("https://" + host + ":" + port)); return certificates.get(); } finally { // shutdown single-use connection manager connectionManager.shutdown(); } }
@Override public Module module(final Key<PluginSink> key, final String id) { return new OutputPluginModule(id) { @Provides Supplier<AggregateMetricSender> sender() { final Collection<OnSendErrorHandler> handlers = ImmutableList.of(metricError -> { log.error(metricError.toString()); }); return () -> { final SignalFxEndpoint endpoint = new SignalFxEndpoint(); final HttpDataPointProtobufReceiverFactory dataPoints = new HttpDataPointProtobufReceiverFactory(endpoint).setVersion(2); BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); SocketConfig socketConfigWithSoTimeout = SocketConfig .copy(connectionManager.getSocketConfig()) .setSoTimeout(soTimeout) .build(); connectionManager.setSocketConfig(socketConfigWithSoTimeout); dataPoints.setHttpClientConnectionManager(connectionManager); final EventReceiverFactory events = new HttpEventProtobufReceiverFactory(endpoint); final AuthToken auth = new StaticAuthToken(authToken); return new AggregateMetricSender(sourceName, dataPoints, events, auth, handlers); }; } @Override protected void configure() { bind(BatchedPluginSink.class).to(SignalFxPluginSink.class); Key<PluginSink> sinkKey = Key.get(PluginSink.class, Names.named("signalfxSink")); bind(sinkKey).to(SignalFxPluginSink.class); install(wrapPluginSink(sinkKey, key)); expose(key); } }; }