Search in sources :

Example 1 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 API</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>JAX-RS 2 Client Invocation 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>");
    Collection<Invocation> is = Arrays.asList(i1, i2);
    for (Invocation i : is) {
        i.invoke();
        System.out.println("Request invoked");
    }
    out.println("... done.<br>");
    out.println("</body>");
    out.println("</html>");
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Invocation(javax.ws.rs.client.Invocation) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) PrintWriter(java.io.PrintWriter)

Example 2 with Invocation

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

the class HttpAuthenticationFilter method repeatRequest.

/**
     * Repeat the {@code request} with provided {@code newAuthorizationHeader}
     * and update the {@code response} with newest response data.
     *
     * @param request                Request context.
     * @param response               Response context (will be updated with the new response data).
     * @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
     * @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
     * {@code false} otherwise).
     */
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
    Client client = request.getClient();
    String method = request.getMethod();
    MediaType mediaType = request.getMediaType();
    URI lUri = request.getUri();
    WebTarget resourceTarget = client.target(lUri);
    Invocation.Builder builder = resourceTarget.request(mediaType);
    MultivaluedMap<String, Object> newHeaders = new MultivaluedHashMap<String, Object>();
    for (Map.Entry<String, List<Object>> entry : request.getHeaders().entrySet()) {
        if (HttpHeaders.AUTHORIZATION.equals(entry.getKey())) {
            continue;
        }
        newHeaders.put(entry.getKey(), entry.getValue());
    }
    newHeaders.add(HttpHeaders.AUTHORIZATION, newAuthorizationHeader);
    builder.headers(newHeaders);
    builder.property(REQUEST_PROPERTY_FILTER_REUSED, "true");
    Invocation invocation;
    if (request.getEntity() == null) {
        invocation = builder.build(method);
    } else {
        invocation = builder.build(method, Entity.entity(request.getEntity(), request.getMediaType()));
    }
    Response nextResponse = invocation.invoke();
    if (nextResponse.hasEntity()) {
        response.setEntityStream(nextResponse.readEntity(InputStream.class));
    }
    MultivaluedMap<String, String> headers = response.getHeaders();
    headers.clear();
    headers.putAll(nextResponse.getStringHeaders());
    response.setStatus(nextResponse.getStatus());
    return response.getStatus() != Response.Status.UNAUTHORIZED.getStatusCode();
}
Also used : Invocation(javax.ws.rs.client.Invocation) InputStream(java.io.InputStream) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 3 with Invocation

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

the class JerseyInvocationTest method testClearHeader.

/**
     * Regression test for JERSEY-2562.
     */
@Test
public void testClearHeader() {
    final Client client = ClientBuilder.newClient();
    final Invocation.Builder builder = client.target("http://localhost:8080/mypath").request();
    final JerseyInvocation invocation = (JerseyInvocation) builder.header("foo", "bar").header("foo", null).header("bar", "foo").buildGet();
    final MultivaluedMap<String, Object> headers = invocation.request().getHeaders();
    assertThat(headers.size(), is(1));
    assertThat(headers.keySet(), hasItem("bar"));
}
Also used : Invocation(javax.ws.rs.client.Invocation) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 4 with Invocation

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

the class JerseyInvocationTest method failedUnboundGenericCallback.

@Test
public void failedUnboundGenericCallback() throws InterruptedException {
    final Invocation invocation = ClientBuilder.newClient().target("http://localhost:888/").request().buildGet();
    final CountDownLatch latch = new CountDownLatch(1);
    final MyUnboundCallback<String> callback = new MyUnboundCallback<String>(latch);
    invocation.submit(callback);
    latch.await(1, TimeUnit.SECONDS);
    assertThat(callback.getThrowable(), CoreMatchers.instanceOf(ProcessingException.class));
    assertThat(callback.getThrowable().getCause(), CoreMatchers.instanceOf(IllegalArgumentException.class));
    assertThat(callback.getThrowable().getCause().getMessage(), CoreMatchers.allOf(CoreMatchers.containsString(MyUnboundCallback.class.getName()), CoreMatchers.containsString(InvocationCallback.class.getName())));
}
Also used : Invocation(javax.ws.rs.client.Invocation) InvocationCallback(javax.ws.rs.client.InvocationCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 5 with Invocation

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

the class ClientTest method testContextHeaders.

@Test
public // Inspired by JERSEY-1502
void testContextHeaders() {
    final WebTarget target = target().path("headers").path("content");
    Invocation.Builder ib;
    Invocation i;
    Response r;
    String reqHeaders;
    ib = target.request("*/*");
    ib.header("custom-header", "custom-value");
    ib.header("content-encoding", "deflate");
    i = ib.build("POST", Entity.entity("aaa", MediaType.WILDCARD_TYPE));
    r = i.invoke();
    reqHeaders = r.readEntity(String.class).toLowerCase();
    for (final String expected : new String[] { "custom-header:[custom-value]", "custom-header:custom-value" }) {
        assertTrue(String.format("Request headers do not contain expected '%s' entry:\n%s", expected, reqHeaders), reqHeaders.contains(expected));
    }
    final String unexpected = "content-encoding";
    assertFalse(String.format("Request headers contains unexpected '%s' entry:\n%s", unexpected, reqHeaders), reqHeaders.contains(unexpected));
    ib = target.request("*/*");
    i = ib.build("POST", Entity.entity("aaa", Variant.mediaTypes(MediaType.WILDCARD_TYPE).encodings("deflate").build().get(0)));
    r = i.invoke();
    final String expected = "content-encoding:[deflate]";
    reqHeaders = r.readEntity(String.class).toLowerCase();
    assertTrue(String.format("Request headers do not contain expected '%s' entry:\n%s", expected, reqHeaders), reqHeaders.contains(expected));
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

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