Search in sources :

Example 21 with ClientConfig

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

the class SslConnectorConfigurationTest method testSSLAuth1.

/**
     * Test to see that SSLHandshakeException is thrown when client don't have
     * trusted key.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLAuth1() throws Exception {
    final SSLContext sslContext = getSslContext();
    final ClientConfig cc = new ClientConfig().connectorProvider(new ApacheConnectorProvider());
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).build();
    WebTarget target = client.target(Server.BASE_URI).register(LoggingFeature.class);
    boolean caught = false;
    try {
        target.path("/").request().get(String.class);
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
}
Also used : ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) SSLContext(javax.net.ssl.SSLContext) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 22 with ClientConfig

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

the class SslConnectorConfigurationTest method testSSLWithAuth.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithAuth() throws Exception {
    final SSLContext sslContext = getSslContext();
    final ClientConfig cc = new ClientConfig().connectorProvider(connectorProvider);
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).build();
    // client basic auth demonstration
    client.register(HttpAuthenticationFeature.basic("user", "password"));
    final WebTarget target = client.target(Server.BASE_URI).register(LoggingFeature.class);
    final Response response = target.path("/").request().get(Response.class);
    assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) SSLContext(javax.net.ssl.SSLContext) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 23 with ClientConfig

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

the class SslConnectorHostnameVerifierTest method testHostnameVerifierApplied.

/**
     * Test to apply {@link HostnameVerifier} along with SSL in the predefined connectors
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testHostnameVerifierApplied() throws Exception {
    // Grizzly and Jetty connectors don't support Hostname Verification
    if (isExcluded(Arrays.asList(GrizzlyConnectorProvider.class, JettyConnectorProvider.class))) {
        return;
    }
    final Client client = ClientBuilder.newBuilder().withConfig(new ClientConfig().connectorProvider(connectorProvider)).register(HttpAuthenticationFeature.basic("user", "password")).hostnameVerifier(new CustomHostnameVerifier()).sslContext(getSslContext()).build();
    try {
        client.target(Server.BASE_URI).request().get(Response.class);
        fail("HostnameVerifier was not applied.");
    } catch (ProcessingException pex) {
        CustomHostnameVerifier.HostnameVerifierException hve = getHVE(pex);
        if (hve != null) {
            assertEquals(CustomHostnameVerifier.EX_VERIFIER_MESSAGE, hve.getMessage());
        } else {
            fail("Invalid wrapped exception.");
        }
    }
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) JettyConnectorProvider(org.glassfish.jersey.jetty.connector.JettyConnectorProvider) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 24 with ClientConfig

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

the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
    final SSLContext sslContext = getSslContext();
    final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
    final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {

        @Override
        public HttpURLConnection getConnection(final URL url) throws IOException {
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(socketFactory);
            return connection;
        }
    }));
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
    final Response response = client.target(Server.BASE_URI).path("/").request().get();
    assertEquals(200, response.getStatus());
    assertTrue(socketFactory.isVisited());
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 25 with ClientConfig

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

the class AsyncTest method testClientThreadPool.

@Test
@Ignore("Unstable test.")
public void testClientThreadPool() throws Exception {
    final AsyncInvoker invoker = ClientBuilder.newClient(new ClientConfig().property(ClientProperties.ASYNC_THREADPOOL_SIZE, 9)).target(getBaseUri()).path("threadpool").request().async();
    final CountDownLatch latch = new CountDownLatch(100);
    final int threadCount = Thread.activeCount();
    final List<Thread> threads = new ArrayList<Thread>(20);
    for (int i = 0; i < 20; i++) {
        threads.add(new Thread(new Runnable() {

            @Override
            public void run() throws RuntimeException {
                for (int i = 0; i < 5; i++) {
                    try {
                        assertThat(invoker.get().get().readEntity(String.class), equalTo("GET"));
                        assertThat(Thread.activeCount() - threadCount - 20, lessThanOrEqualTo(10));
                        latch.countDown();
                    } catch (final InterruptedException e) {
                        fail();
                    } catch (final ExecutionException e) {
                        fail();
                    }
                }
            }
        }));
    }
    for (final Thread thread : threads) {
        thread.start();
    }
    assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
}
Also used : AsyncInvoker(javax.ws.rs.client.AsyncInvoker) ArrayList(java.util.ArrayList) ClientConfig(org.glassfish.jersey.client.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Ignore(org.junit.Ignore) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

ClientConfig (org.glassfish.jersey.client.ClientConfig)94 Test (org.junit.Test)77 Client (javax.ws.rs.client.Client)74 JerseyTest (org.glassfish.jersey.test.JerseyTest)51 WebTarget (javax.ws.rs.client.WebTarget)46 Response (javax.ws.rs.core.Response)33 ClientResponse (org.glassfish.jersey.client.ClientResponse)12 Invocation (javax.ws.rs.client.Invocation)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)7 JerseyClient (org.glassfish.jersey.client.JerseyClient)6 SSLContext (javax.net.ssl.SSLContext)5 ProcessingException (javax.ws.rs.ProcessingException)5 ClientBuilder (javax.ws.rs.client.ClientBuilder)5 ApacheConnectorProvider (org.glassfish.jersey.apache.connector.ApacheConnectorProvider)5 IOException (java.io.IOException)4 HttpUrlConnectorProvider (org.glassfish.jersey.client.HttpUrlConnectorProvider)4 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)4 Ignore (org.junit.Ignore)4