Search in sources :

Example 76 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project dropwizard by dropwizard.

the class Resource method before.

public void before() throws Throwable {
    DropwizardTestResourceConfig.CONFIGURATION_REGISTRY.put(configuration.getId(), configuration);
    test = new JerseyTest(configuration.testContainerFactory) {

        @Override
        protected URI getBaseUri() {
            forceSet(TestProperties.CONTAINER_PORT, "0");
            return super.getBaseUri();
        }

        @Override
        protected DeploymentContext configureDeployment() {
            return ServletDeploymentContext.builder(new DropwizardTestResourceConfig(configuration)).initParam(ServletProperties.JAXRS_APPLICATION_CLASS, DropwizardTestResourceConfig.class.getName()).initParam(DropwizardTestResourceConfig.CONFIGURATION_ID, configuration.getId()).build();
        }

        @Override
        protected void configureClient(ClientConfig clientConfig) {
            final JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
            jsonProvider.setMapper(configuration.mapper);
            configuration.clientConfigurator.accept(clientConfig);
            clientConfig.register(jsonProvider);
        }
    };
    test.setUp();
}
Also used : DeploymentContext(org.glassfish.jersey.test.DeploymentContext) ServletDeploymentContext(org.glassfish.jersey.test.ServletDeploymentContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ClientConfig(org.glassfish.jersey.client.ClientConfig) URI(java.net.URI)

Example 77 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project openstack4j by ContainX.

the class ClientFactory method buildClientFromConfig.

private static Client buildClientFromConfig(Config config) {
    ClientConfig clientConfig = new ClientConfig();
    if (config.getProxy() != null) {
        addProxy(clientConfig, config);
    }
    ClientBuilder cb = ClientBuilder.newBuilder().withConfig(clientConfig).property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, "true").register(JacksonFeature.class).register(RESOLVER).register(new RequestFilter());
    if (config.getSslContext() != null)
        cb.sslContext(config.getSslContext());
    else if (config.isIgnoreSSLVerification())
        cb.sslContext(UntrustedSSL.getSSLContext());
    if (config.getHostNameVerifier() != null)
        cb.hostnameVerifier(config.getHostNameVerifier());
    else if (config.isIgnoreSSLVerification())
        cb.hostnameVerifier(UntrustedSSL.getHostnameVerifier());
    if (config.getReadTimeout() > 0)
        cb.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout());
    if (config.getConnectTimeout() > 0)
        cb.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout());
    return cb.build();
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 78 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project ats-framework by Axway.

the class RestClient method constructThirdPartyConnectorInvocationBuilder.

private void constructThirdPartyConnectorInvocationBuilder(String descriptionToken, boolean suppressHttpComplianceValidation) {
    // create the client config object
    ClientConfig clientConfig = createClientConfig(suppressHttpComplianceValidation);
    // create the client builder
    ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
    // handle HTTPS requests
    if (isHttps()) {
        // configure Trust-all SSL context
        checkSupportedProtocols();
        SSLContext sslContext = SslUtils.getSSLContext(clientConfigurator.getCertificateFileName(), clientConfigurator.getCertificateFilePassword(), supportedProtocols[0]);
        clientBuilder = clientBuilder.sslContext(sslContext).hostnameVerifier(new SslUtils.DefaultHostnameVerifier());
    }
    // now create the client
    createClient(clientBuilder);
    createInvocationBuilder(descriptionToken);
}
Also used : SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 79 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project pravega by pravega.

the class ControllerCommand method createContext.

/**
 * Creates a context for child classes consisting of a REST client to execute calls against the Controller.
 *
 * @return REST client.
 */
protected Context createContext() {
    CLIConfig config = getCLIControllerConfig();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(JacksonJsonProvider.class);
    clientConfig.property("sun.net.http.allowRestrictedHeaders", "true");
    ClientBuilder builder = ClientBuilder.newBuilder().withConfig(clientConfig);
    // If TLS parameters are configured, set them in client.
    if (config.isTlsEnabled()) {
        SSLContext tlsContext;
        try {
            KeyStore ks = createTrustStore(config.getTruststore());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(ks);
            tlsContext = SSLContext.getInstance("TLS");
            tlsContext.init(null, tmf.getTrustManagers(), null);
        } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) {
            String message = String.format("Encountered exception while trying to use the given truststore: %s", config.getTruststore());
            log.error(message, e);
            return null;
        }
        builder.sslContext(tlsContext);
    }
    Client client = builder.build();
    // If authorization parameters are configured, set them in the client.
    if (config.isAuthEnabled()) {
        HttpAuthenticationFeature auth = HttpAuthenticationFeature.basic(config.getUserName(), config.getPassword());
        client = client.register(auth);
    }
    return new Context(client);
}
Also used : SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) CLIConfig(io.pravega.cli.admin.utils.CLIConfig) HttpAuthenticationFeature(org.glassfish.jersey.client.authentication.HttpAuthenticationFeature) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 80 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project athenz by yahoo.

the class ZMSClient method initClient.

/**
 * Initialize the client for class constructors
 *
 * @param url        ZMS Server url
 * @param sslContext SSLContext for service authentication
 */
private void initClient(String url, SSLContext sslContext) {
    if (url == null) {
        zmsUrl = lookupZMSUrl();
    } else {
        zmsUrl = url;
    }
    if (zmsUrl != null && !zmsUrl.isEmpty()) {
        if (!zmsUrl.endsWith("/zms/v1")) {
            if (zmsUrl.charAt(zmsUrl.length() - 1) != '/') {
                zmsUrl += '/';
            }
            zmsUrl += "zms/v1";
        }
    }
    /* determine our read and connect timeouts */
    int readTimeout = Integer.parseInt(System.getProperty(ZMS_CLIENT_PROP_READ_TIMEOUT, "30000"));
    int connectTimeout = Integer.parseInt(System.getProperty(ZMS_CLIENT_PROP_CONNECT_TIMEOUT, "30000"));
    if (sslContext == null) {
        sslContext = createSSLContext();
    }
    ClientBuilder builder = getClientBuilder();
    if (sslContext != null) {
        builder = builder.sslContext(sslContext);
    }
    final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ClientConfig clientConfig = new ClientConfig(jacksonJsonProvider);
    clientConfig.connectorProvider(new ApacheConnectorProvider());
    // JerseyClientBuilder::withConfig() replaces the existing config with the new client
    // config. Hence the client config should be added to the builder before the timeouts.
    // Otherwise the timeout settings would be overridden.
    Client rsClient = builder.withConfig(clientConfig).connectTimeout(connectTimeout, TimeUnit.MILLISECONDS).readTimeout(readTimeout, TimeUnit.MILLISECONDS).build();
    client = new ZMSRDLGeneratedClient(zmsUrl, rsClient);
}
Also used : JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) JacksonJaxbJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Aggregations

ClientConfig (org.glassfish.jersey.client.ClientConfig)169 Client (javax.ws.rs.client.Client)129 Test (org.junit.Test)85 Response (javax.ws.rs.core.Response)66 JerseyTest (org.glassfish.jersey.test.JerseyTest)52 WebTarget (javax.ws.rs.client.WebTarget)48 ClientBuilder (javax.ws.rs.client.ClientBuilder)17 SSLContext (javax.net.ssl.SSLContext)12 Invocation (javax.ws.rs.client.Invocation)12 ClientResponse (org.glassfish.jersey.client.ClientResponse)12 ApacheConnectorProvider (org.glassfish.jersey.apache.connector.ApacheConnectorProvider)10 IOException (java.io.IOException)9 IntegrationTest (com.hortonworks.streamline.common.test.IntegrationTest)8 URI (java.net.URI)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)7 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 JerseyClient (org.glassfish.jersey.client.JerseyClient)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5