Search in sources :

Example 6 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter in project cxf by apache.

the class JAXRS20ClientServerBookTest method testGetSetEntityStream.

@Test
public void testGetSetEntityStream() {
    String address = "http://localhost:" + PORT + "/bookstore/entityecho";
    String entity = "BOOKSTORE";
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            context.setEntityStream(new ReplacingOutputStream(context.getEntityStream(), 'X', 'O'));
        }
    });
    WebTarget target = client.target(address);
    Response response = target.request().post(Entity.entity(entity.replace('O', 'X'), "text/plain"));
    assertEquals(entity, response.readEntity(String.class));
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) IOException(java.io.IOException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 7 with ClientRequestFilter

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

the class JerseyClientTest method testTargetConfigUpdate.

@Test
public void testTargetConfigUpdate() {
    final JerseyWebTarget target = client.target("http://jersey.java.net/examples");
    target.getConfiguration().register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext clientRequestContext) throws IOException {
            throw new UnsupportedOperationException("Not supported yet");
        }
    });
    assertEquals(1, target.getConfiguration().getInstances().size());
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter 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 9 with ClientRequestFilter

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

the class BasicClientTest method abortingTarget.

protected WebTarget abortingTarget() {
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            Response r = Response.ok("aborted").build();
            ctx.abortWith(r);
        }
    });
    return client.target("http://any.web:888");
}
Also used : ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientRequestContext(javax.ws.rs.client.ClientRequestContext) Response(javax.ws.rs.core.Response) IOException(java.io.IOException) Client(javax.ws.rs.client.Client)

Example 10 with ClientRequestFilter

use of javax.ws.rs.client.ClientRequestFilter 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)

Aggregations

ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)16 Client (javax.ws.rs.client.Client)9 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)9 Response (javax.ws.rs.core.Response)9 Test (org.junit.Test)8 IOException (java.io.IOException)7 WebTarget (javax.ws.rs.client.WebTarget)7 ClientBuilder (javax.ws.rs.client.ClientBuilder)4 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)4 Invocation (javax.ws.rs.client.Invocation)3 WebClient (org.apache.cxf.jaxrs.client.WebClient)3 Collections (java.util.Collections)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 SSLContext (javax.net.ssl.SSLContext)2 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)2 Entity (javax.ws.rs.client.Entity)2 HttpHeaders (javax.ws.rs.core.HttpHeaders)2 ITHttpAsyncClient (brave.test.http.ITHttpAsyncClient)1