aboutsummaryrefslogtreecommitdiffstats
path: root/vid-automation/src/main/java/vid/automation/test/utils/InsecureHttpsClient.java
blob: 68ebeb1220aad0f6cef95ffc453bb72cb37a73c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package vid.automation.test.utils;

import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class InsecureHttpsClient {

    public static RestTemplate newRestTemplate() {

        CloseableHttpClient insecureTLSHttpClient = HttpClients.custom()
            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .setSSLContext(trustAllCertificates())
            .build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(insecureTLSHttpClient);
        return new RestTemplate(factory);
    }

    private static SSLContext trustAllCertificates() {
        try {
            return new SSLContextBuilder()
                .loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
                .build();
        } catch (Exception e) {
            return ExceptionUtils.rethrow(e);
        }
    }

    public static Client newJaxrsClient() {
        return ClientBuilder.newBuilder()
            .hostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .sslContext(trustAllCertificates())
            .build();
    }

}