Search in sources :

Example 51 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project lithium by wireapp.

the class TrustedTlsClientBuilder method build.

public static Client build() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    ClientConfig cfg = new ClientConfig(JacksonJsonProvider.class);
    try {
        SSLContext ctx = SSLContext.getInstance("TLSv1.2");
        ctx.init(null, certs, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
        ClientBuilder clientBuilder = ClientBuilder.newBuilder();
        clientBuilder.sslContext(ctx);
        clientBuilder.hostnameVerifier((hostname, session) -> true);
        return clientBuilder.withConfig(cfg).build();
    } catch (Exception e) {
        Logger.error(e.toString());
        return JerseyClientBuilder.createClient(cfg);
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) X509Certificate(java.security.cert.X509Certificate) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) ClientBuilder(javax.ws.rs.client.ClientBuilder) JerseyClientBuilder(org.glassfish.jersey.client.JerseyClientBuilder)

Example 52 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project batfish by batfish.

the class CommonUtil method createHttpClientBuilder.

public static ClientBuilder createHttpClientBuilder(boolean noSsl, boolean trustAllSslCerts, Path keystoreFile, String keystorePassword, Path truststoreFile, String truststorePassword) {
    ClientBuilder clientBuilder = ClientBuilder.newBuilder();
    try {
        if (!noSsl) {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            TrustManager[] trustManagers;
            if (trustAllSslCerts) {
                trustManagers = new TrustManager[] { new X509TrustManager() {

                    @Override
                    public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                } };
                clientBuilder.hostnameVerifier(new TrustAllHostNameVerifier());
            } else if (truststoreFile != null) {
                TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
                KeyStore ts = KeyStore.getInstance("JKS");
                if (truststorePassword == null) {
                    throw new BatfishException("Truststore file supplied but truststore password missing");
                }
                char[] tsPass = truststorePassword.toCharArray();
                try (FileInputStream trustInputStream = new FileInputStream(truststoreFile.toFile())) {
                    ts.load(trustInputStream, tsPass);
                }
                tmf.init(ts);
                trustManagers = tmf.getTrustManagers();
            } else {
                trustManagers = null;
            }
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            KeyStore ks = KeyStore.getInstance("JKS");
            KeyManager[] keyManagers;
            if (keystoreFile != null) {
                if (keystorePassword == null) {
                    throw new BatfishException("Keystore file supplied but keystore password");
                }
                char[] ksPass = keystorePassword.toCharArray();
                try (FileInputStream keystoreStream = new FileInputStream(keystoreFile.toFile())) {
                    ks.load(keystoreStream, ksPass);
                }
                kmf.init(ks, ksPass);
                keyManagers = kmf.getKeyManagers();
            } else {
                keyManagers = null;
            }
            sslcontext.init(keyManagers, trustManagers, new java.security.SecureRandom());
            clientBuilder.sslContext(sslcontext);
        }
        if (GlobalTracer.isRegistered()) {
            clientBuilder.register(ClientTracingFeature.class);
        }
    } catch (Exception e) {
        throw new BatfishException("Error creating HTTP client builder", e);
    }
    return clientBuilder;
}
Also used : BatfishException(org.batfish.common.BatfishException) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) BatfishException(org.batfish.common.BatfishException) MalformedURLException(java.net.MalformedURLException) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 53 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project cloudbreak by hortonworks.

the class RestClientUtil method createClient.

public static Client createClient(SSLContext sslContext, boolean debug, Class<?> debugClass) {
    ClientConfig config = new ClientConfig();
    config.property(ClientProperties.FOLLOW_REDIRECTS, "false");
    config.property(ClientProperties.CONNECT_TIMEOUT, CONNECT_TIMEOUT_MS);
    config.register(MultiPartFeature.class);
    ClientBuilder builder = ClientBuilder.newBuilder().withConfig(config);
    builder.sslContext(sslContext);
    builder.hostnameVerifier(CertificateTrustManager.hostnameVerifier());
    if (debug) {
        builder = builder.register(new LoggingFilter(java.util.logging.Logger.getLogger(debugClass.getName()), true));
    }
    Client client = builder.build();
    LOGGER.debug("Jax rs client has been constructed: {}, sslContext: {}", client, sslContext);
    return client;
}
Also used : LoggingFilter(org.glassfish.jersey.filter.LoggingFilter) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 54 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project oxAuth by GluuFederation.

the class JwtUtil method getJSONWebKeys.

public static JSONObject getJSONWebKeys(String jwksUri, ClientHttpEngine engine) {
    log.debug("Retrieving jwks " + jwksUri + "...");
    JSONObject jwks = null;
    try {
        if (!StringHelper.isEmpty(jwksUri)) {
            ClientBuilder clientBuilder = ResteasyClientBuilder.newBuilder();
            if (engine != null) {
                ((ResteasyClientBuilder) clientBuilder).httpEngine(engine);
            }
            javax.ws.rs.client.Client clientRequest = clientBuilder.build();
            try {
                Response clientResponse = clientRequest.target(jwksUri).request().buildGet().invoke();
                int status = clientResponse.getStatus();
                log.debug(String.format("Status: %n%d", status));
                if (status == 200) {
                    jwks = fromJson(clientResponse.readEntity(String.class));
                    log.debug(String.format("JWK: %s", jwks));
                }
            } finally {
                clientRequest.close();
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
    return jwks;
}
Also used : Response(javax.ws.rs.core.Response) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) JSONObject(org.json.JSONObject) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchProviderException(java.security.NoSuchProviderException) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 55 with ClientBuilder

use of javax.ws.rs.client.ClientBuilder in project muikku by otavanopisto.

the class ClientPool method buildClient.

private Client buildClient() {
    // TODO: trust all only on development environment
    ClientBuilder clientBuilder = ClientBuilder.newBuilder();
    if ("development".equals(System.getProperties().getProperty("system.environment"))) {
        clientBuilder = trustSelfSignedCerts(clientBuilder);
    }
    ClientBuilder builder = clientBuilder.register(new JacksonConfigurator()).register(new BrowserCacheFeature());
    return builder.build();
}
Also used : BrowserCacheFeature(org.jboss.resteasy.client.jaxrs.cache.BrowserCacheFeature) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Aggregations

ClientBuilder (javax.ws.rs.client.ClientBuilder)57 Client (javax.ws.rs.client.Client)41 Response (javax.ws.rs.core.Response)26 Test (org.junit.Test)26 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)24 AuthenticationContext (org.wildfly.security.auth.client.AuthenticationContext)24 URL (java.net.URL)20 SSLContext (javax.net.ssl.SSLContext)16 ClientConfig (org.glassfish.jersey.client.ClientConfig)15 AuthenticationConfiguration (org.wildfly.security.auth.client.AuthenticationConfiguration)13 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)11 InvalidAuthenticationConfigurationException (org.wildfly.security.auth.client.InvalidAuthenticationConfigurationException)11 WebTarget (javax.ws.rs.client.WebTarget)10 IOException (java.io.IOException)9 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)9 KeyStore (java.security.KeyStore)7 HttpClient (org.apache.http.client.HttpClient)7 AuthenticationContextConfigurationClient (org.wildfly.security.auth.client.AuthenticationContextConfigurationClient)6 MalformedURLException (java.net.MalformedURLException)5 HostnameVerifier (javax.net.ssl.HostnameVerifier)5