Search in sources :

Example 11 with Invocation

use of javax.ws.rs.client.Invocation in project jersey by jersey.

the class ClientInvocationTest method testMultipleCallbackInvocationSubmits.

@Test
public void testMultipleCallbackInvocationSubmits() throws Exception {
    final Invocation invocation = target().request().buildGet();
    for (int i = 0; i < INVOCATIONS; i++) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<String> response = new AtomicReference<>();
        invocation.submit(new InvocationCallback<String>() {

            @Override
            public void completed(final String s) {
                response.set(s);
                latch.countDown();
            }

            @Override
            public void failed(final Throwable throwable) {
                response.set(throwable.getMessage());
                latch.countDown();
            }
        });
        latch.await(5, TimeUnit.SECONDS);
        assertThat(response.get(), is("OK"));
    }
}
Also used : Invocation(javax.ws.rs.client.Invocation) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 12 with Invocation

use of javax.ws.rs.client.Invocation in project jersey by jersey.

the class AbortResponseClientTest method testRequestAbort.

@Test
public void testRequestAbort() {
    final Date date = getDate();
    ClientRequestFilter outFilter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            NewCookie cookie1 = new NewCookie("cookie1", "cookie1");
            NewCookie cookie2 = new NewCookie("cookie2", "cookie2");
            final Response response = Response.ok().cookie(cookie1).cookie(cookie2).header("head1", "head1").header(HttpHeaders.DATE, date).header(HttpHeaders.ETAG, "\"123465\"").header(HttpHeaders.CONTENT_LANGUAGE, "language").header(HttpHeaders.LAST_MODIFIED, date).header(HttpHeaders.CONTENT_LENGTH, 99).type(MediaType.TEXT_HTML_TYPE).location(URI.create("www.oracle.com")).build();
            // abort the request
            context.abortWith(response);
        }
    };
    ClientResponseFilter inFilter = new ClientResponseFilter() {

        @Override
        public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
            Map<String, NewCookie> map = responseContext.getCookies();
            assertEquals("cookie1", map.get("cookie1").getValue());
            assertEquals("cookie2", map.get("cookie2").getValue());
            final MultivaluedMap<String, String> headers = responseContext.getHeaders();
            assertEquals("head1", headers.get("head1").get(0));
            assertEquals(date.getTime(), responseContext.getDate().getTime());
        }
    };
    WebTarget target = target().path("test");
    target.register(outFilter).register(inFilter);
    Invocation i = target.request().buildGet();
    Response r = i.invoke();
    assertEquals("head1", r.getHeaderString("head1"));
    assertEquals("cookie1", r.getCookies().get("cookie1").getValue());
    assertEquals("cookie2", r.getCookies().get("cookie2").getValue());
    assertEquals(date.getTime(), r.getDate().getTime());
    assertEquals("123465", r.getEntityTag().getValue());
    assertEquals("language", r.getLanguage().toString());
    assertEquals(date.getTime(), r.getLastModified().getTime());
    // Assert.assertEquals("uri", r.getLink("link")); TODO: not supported yet
    assertEquals("www.oracle.com", r.getLocation().toString());
    assertEquals(MediaType.TEXT_HTML_TYPE, r.getMediaType());
    assertEquals(99, r.getLength());
    assertEquals(200, r.getStatus());
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) WebTarget(javax.ws.rs.client.WebTarget) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Date(java.util.Date) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 13 with Invocation

use of javax.ws.rs.client.Invocation in project jersey by jersey.

the class BasicClientTest method testAbortSyncRequest.

@Test
public // JERSEY-1412
void testAbortSyncRequest() throws Exception {
    Invocation invocation = abortingTarget().request().buildPost(text("entity"));
    String response = invocation.invoke(String.class);
    assertEquals("aborted", response);
}
Also used : Invocation(javax.ws.rs.client.Invocation) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

Invocation (javax.ws.rs.client.Invocation)13 Test (org.junit.Test)10 WebTarget (javax.ws.rs.client.WebTarget)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 Client (javax.ws.rs.client.Client)5 Response (javax.ws.rs.core.Response)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 ExecutionException (java.util.concurrent.ExecutionException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ProcessingException (javax.ws.rs.ProcessingException)2 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)2 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)2 InvocationCallback (javax.ws.rs.client.InvocationCallback)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1