Search in sources :

Example 11 with ClientConfig

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

the class MultiPartTest method testMultiPartResource.

@Test
public void testMultiPartResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class).register(new MultiPartFeature());
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig);
    Client c = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
    final Response response = c.target(baseUri).path("/multipart-simple").request().buildGet().invoke();
    MultiPart result = response.readEntity(MultiPart.class);
    System.out.println("RESULT = " + result);
    checkEntity("This is the only segment", (BodyPartEntity) result.getBodyParts().get(0).getEntity());
    server.shutdownNow();
}
Also used : Response(javax.ws.rs.core.Response) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) MultiPartFeature(org.glassfish.jersey.media.multipart.MultiPartFeature) HttpServer(org.glassfish.grizzly.http.server.HttpServer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 12 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 13 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 14 with ClientConfig

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

the class HttpMethodTest method createClient.

protected Client createClient() {
    ClientConfig cc = new ClientConfig();
    cc.connectorProvider(new ApacheConnectorProvider());
    return ClientBuilder.newClient(cc);
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig)

Example 15 with ClientConfig

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

the class MainTest method testSSLWithBasicAndSSLAuthHttpUrlConnector.

@Test
public void testSSLWithBasicAndSSLAuthHttpUrlConnector() {
    final ClientConfig clientConfig = getHttpUrlConnectorConfig();
    _testSSLWithBasicAndSSLAuth(clientConfig);
}
Also used : ClientConfig(org.glassfish.jersey.client.ClientConfig) 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