Search in sources :

Example 6 with Invocation

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

the class BasicClientTest method testAbortAsyncRequest.

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

Example 7 with Invocation

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

the class ClientInvocationTest method testMultipleCallbackInvocationSubmitsWithEntity.

@Test
public void testMultipleCallbackInvocationSubmitsWithEntity() throws Exception {
    final Invocation invocation = target().request().buildPost(Entity.text("OK"));
    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 8 with Invocation

use of javax.ws.rs.client.Invocation in project javaee7-samples by javaee-samples.

the class TestServlet method processRequest.

/**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>JAX-RS 2 Client Invocation Async API</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>JAX-RS 2 Client Invocation Async API at " + request.getContextPath() + "</h1>");
    out.println("Initializing client...<br>");
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/webresources/resource");
    // GET
    out.print("Building a GET request ...<br>");
    Invocation i1 = target.request().buildGet();
    out.print("GET request ready ...<br>");
    // POST
    out.print("Building a POST request...<br>");
    MultivaluedHashMap<String, String> map = new MultivaluedHashMap<>();
    map.add("name", "Name");
    map.add("age", "17");
    Invocation i2 = target.request().buildPost(Entity.form(map));
    out.print("POSTed request ready...<br>");
    Future<Response> f1 = i1.submit();
    Future<String> f2 = i2.submit(String.class);
    try {
        Response r1 = f1.get();
        out.println("Response from r1: " + r1.readEntity(String.class) + "<br>");
        String r2 = f2.get();
        out.println("Response from r2: " + r2 + "<br>");
    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.println("... done.<br>");
    out.println("</body>");
    out.println("</html>");
}
Also used : Invocation(javax.ws.rs.client.Invocation) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ExecutionException(java.util.concurrent.ExecutionException) PrintWriter(java.io.PrintWriter)

Example 9 with Invocation

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

the class JerseyInvocationTest method testNullResponseType.

@Test
public void testNullResponseType() throws Exception {
    final Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(final ClientRequestContext requestContext) throws IOException {
            requestContext.abortWith(Response.ok().build());
        }
    });
    final WebTarget target = client.target("http://localhost:8080/mypath");
    final Class<Response> responseType = null;
    final String[] methods = new String[] { "GET", "PUT", "POST", "DELETE", "OPTIONS" };
    for (final String method : methods) {
        final Invocation.Builder request = target.request();
        try {
            request.method(method, responseType);
            fail("IllegalArgumentException expected.");
        } catch (final IllegalArgumentException iae) {
        // OK.
        }
        final Invocation build = "PUT".equals(method) ? request.build(method, Entity.entity("", MediaType.TEXT_PLAIN_TYPE)) : request.build(method);
        try {
            build.submit(responseType);
            fail("IllegalArgumentException expected.");
        } catch (final IllegalArgumentException iae) {
        // OK.
        }
        try {
            build.invoke(responseType);
            fail("IllegalArgumentException expected.");
        } catch (final IllegalArgumentException iae) {
        // OK.
        }
        try {
            request.async().method(method, responseType);
            fail("IllegalArgumentException expected.");
        } catch (final IllegalArgumentException iae) {
        // OK.
        }
    }
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Invocation(javax.ws.rs.client.Invocation) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 10 with Invocation

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

the class JerseyInvocationTest method failedCallbackTest.

@Test
public void failedCallbackTest() throws InterruptedException {
    final Invocation.Builder builder = ClientBuilder.newClient().target("http://localhost:888/").request();
    for (int i = 0; i < 1; i++) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicInteger ai = new AtomicInteger(0);
        final InvocationCallback<String> callback = new InvocationCallback<String>() {

            @Override
            public void completed(final String arg0) {
                try {
                    ai.set(ai.get() + 1);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void failed(final Throwable throwable) {
                try {
                    int result = 10;
                    if (throwable instanceof ProcessingException) {
                        result += 100;
                    }
                    final Throwable ioe = throwable.getCause();
                    if (ioe instanceof IOException) {
                        result += 1000;
                    }
                    ai.set(ai.get() + result);
                } finally {
                    latch.countDown();
                }
            }
        };
        final Invocation invocation = builder.buildGet();
        final Future<String> future = invocation.submit(callback);
        try {
            future.get();
            fail("future.get() should have failed.");
        } catch (final ExecutionException e) {
            final Throwable pe = e.getCause();
            assertTrue("Execution exception cause is not a ProcessingException: " + pe.toString(), pe instanceof ProcessingException);
            final Throwable ioe = pe.getCause();
            assertTrue("Execution exception cause is not an IOException: " + ioe.toString(), ioe instanceof IOException);
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
        latch.await(1, TimeUnit.SECONDS);
        assertEquals(1110, ai.get());
    }
}
Also used : Invocation(javax.ws.rs.client.Invocation) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationCallback(javax.ws.rs.client.InvocationCallback) ExecutionException(java.util.concurrent.ExecutionException) ProcessingException(javax.ws.rs.ProcessingException) 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