Search in sources :

Example 41 with ClientConfig

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

the class EventOutputTest method testGrizzlyConnectorWithEventSource.

@Test
public void testGrizzlyConnectorWithEventSource() throws InterruptedException {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
    clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
    clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
    clientConfig.connectorProvider(new GrizzlyConnectorProvider());
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> eventData = new AtomicReference<String>();
    final AtomicInteger counter = new AtomicInteger(0);
    WebTarget single = client.target(getBaseUri()).path("test/single");
    EventSource es = EventSource.target(single).build();
    es.register(new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            final int i = counter.incrementAndGet();
            if (i == 1) {
                eventData.set(inboundEvent.readData());
            }
            latch.countDown();
        }
    });
    boolean latchTimedOut;
    boolean closeTimedOut;
    try {
        es.open();
        latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    } finally {
        closeTimedOut = es.close(5, TimeUnit.SECONDS);
    }
    assertEquals("Unexpected event count", 1, counter.get());
    assertEquals("Unexpected event data", "single", eventData.get());
    assertTrue("Event latch has timed out", latchTimedOut);
    assertTrue("EventSource.close() has timed out", closeTimedOut);
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 42 with ClientConfig

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

the class EventOutputTest method testReadCommentsOnlySseEvents.

/**
     * Reproducer for JERSEY-2912: Sending and receiving comments-only events.
     *
     * @throws Exception
     */
@Test
public void testReadCommentsOnlySseEvents() throws Exception {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
    clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
    clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
    clientConfig.connectorProvider(new GrizzlyConnectorProvider());
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
    final CountDownLatch latch = new CountDownLatch(2);
    final Queue<String> eventComments = new ArrayBlockingQueue<>(2);
    WebTarget single = client.target(getBaseUri()).path("test/comments-only");
    EventSource es = EventSource.target(single).build();
    es.register(new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            eventComments.add(inboundEvent.getComment());
            latch.countDown();
        }
    });
    boolean latchTimedOut;
    boolean closeTimedOut;
    try {
        es.open();
        latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    } finally {
        closeTimedOut = es.close(5, TimeUnit.SECONDS);
    }
    assertEquals("Unexpected event count", 2, eventComments.size());
    for (int i = 1; i <= 2; i++) {
        assertEquals("Unexpected comment data on event #" + i, "No comment #" + i, eventComments.poll());
    }
    assertTrue("Event latch has timed out", latchTimedOut);
    assertTrue("EventSource.close() has timed out", closeTimedOut);
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) EventSource(org.glassfish.jersey.media.sse.EventSource) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 43 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 44 with ClientConfig

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

the class OpenTsdbClient method init.

private void init() {
    final ApacheConnectorProvider apacheConnectorProvider = new ApacheConnectorProvider();
    final ClientConfig clientConfig = new ClientConfig().connectorProvider(apacheConnectorProvider);
    // transfer encoding should be set as jersey sets it on by default.
    clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, enableChunkedEncoding ? RequestEntityProcessing.CHUNKED : RequestEntityProcessing.BUFFERED);
    client = ClientBuilder.newClient(clientConfig);
    target = client.target(urlString).path(PUT_PATH);
    if (sync) {
        // need to add an empty string else it is nto added as query param.
        target = target.queryParam("sync", "").queryParam("sync_timeout", syncTimeout);
    }
    if (responseType != ResponseType.None) {
        // need to add an empty string else it is nto added as query param.
        target = target.queryParam(responseType.value, "");
    }
    LOG.info("target uri [{}]", target.getUri());
}
Also used : ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) ClientConfig(org.glassfish.jersey.client.ClientConfig)

Example 45 with ClientConfig

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

the class UnderlyingHttpClientAccessTest method testHttpClientInstanceAccess.

/**
     * Verifier of JERSEY-2424 fix.
     */
@Test
public void testHttpClientInstanceAccess() {
    final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new ApacheConnectorProvider()));
    final HttpClient hcOnClient = ApacheConnectorProvider.getHttpClient(client);
    // important: the web target instance in this test must be only created AFTER the client has been pre-initialized
    // (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
    // connector provider's static getHttpClient method above.
    final WebTarget target = client.target("http://localhost/");
    final HttpClient hcOnTarget = ApacheConnectorProvider.getHttpClient(target);
    assertNotNull("HTTP client instance set on JerseyClient should not be null.", hcOnClient);
    assertNotNull("HTTP client instance set on JerseyWebTarget should not be null.", hcOnTarget);
    assertSame("HTTP client instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", hcOnClient, hcOnTarget);
}
Also used : HttpClient(org.apache.http.client.HttpClient) WebTarget(javax.ws.rs.client.WebTarget) HttpClient(org.apache.http.client.HttpClient) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Aggregations

ClientConfig (org.glassfish.jersey.client.ClientConfig)91 Test (org.junit.Test)77 Client (javax.ws.rs.client.Client)71 JerseyTest (org.glassfish.jersey.test.JerseyTest)51 WebTarget (javax.ws.rs.client.WebTarget)43 Response (javax.ws.rs.core.Response)31 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