Search in sources :

Example 1 with TrustAllStrategy

use of org.apache.http.conn.ssl.TrustAllStrategy in project docker-maven-plugin by fabric8io.

the class HttpPingChecker method ping.

private boolean ping() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_PING_TIMEOUT).setConnectTimeout(HTTP_PING_TIMEOUT).setConnectionRequestTimeout(HTTP_PING_TIMEOUT).setRedirectsEnabled(false).build();
    CloseableHttpClient httpClient;
    if (allowAllHosts) {
        SSLContextBuilder builder = new SSLContextBuilder();
        try {
            builder.loadTrustMaterial(new TrustAllStrategy());
            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRIES, false)).setSSLSocketFactory(socketFactory).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            throw new IOException("Unable to set self signed strategy on http wait: " + e, e);
        }
    } else {
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setRetryHandler(new DefaultHttpRequestRetryHandler(HTTP_CLIENT_RETRIES, false)).build();
    }
    try (CloseableHttpResponse response = httpClient.execute(RequestBuilder.create(method.toUpperCase()).setUri(url).build())) {
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode == HttpURLConnection.HTTP_NOT_IMPLEMENTED) {
            throw new IllegalArgumentException("Invalid or not supported HTTP method '" + method.toUpperCase() + "' for checking " + url);
        }
        return responseCode >= statusMin && responseCode <= statusMax;
    } finally {
        httpClient.close();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) KeyManagementException(java.security.KeyManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) TrustAllStrategy(org.apache.http.conn.ssl.TrustAllStrategy) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder)

Aggregations

IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 TrustAllStrategy (org.apache.http.conn.ssl.TrustAllStrategy)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)1 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)1