Search in sources :

Example 1 with ClientRequestFilter

use of jakarta.ws.rs.client.ClientRequestFilter in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method createRequestFilter.

// ///////////////////////////////////////////////////////////////////////
protected static ClientRequestFilter createRequestFilter(final Response response) {
    ClientRequestFilter outFilter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            // logMsg(" -- OUT FILTER --");
            Response r;
            if (response == null)
                r = Response.ok().build();
            else
                r = response;
            context.abortWith(r);
        }
    };
    return outFilter;
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response)

Example 2 with ClientRequestFilter

use of jakarta.ws.rs.client.ClientRequestFilter in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method getStatusTest.

/*
   * @testName: getStatusTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:472; JAXRS:JAVADOC:479; JAXRS:JAVADOC:480;
   * 
   * @test_Strategy: Get the status code associated with the response.
   * ClientResponseFilter.filter
   */
@Test
public void getStatusTest() throws Fault {
    ContextProvider provider = new ContextProvider() {

        @Override
        protected void checkFilterContext(ClientRequestContext requestContext, ClientResponseContext responseContext) throws Fault {
            assertTrue(responseContext.getStatus() == 222, "unexpected status " + responseContext.getStatus());
            TestUtil.logMsg("Found expected response status 222");
        }
    };
    Response response = Response.status(222).build();
    ClientRequestFilter filter = createRequestFilter(response);
    Invocation i = buildInvocation(filter, provider);
    Response r = invoke(i);
    assertTrue(r.getStatus() == 222, "unexpected status " + r.getStatus());
}
Also used : ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) Invocation(jakarta.ws.rs.client.Invocation) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) Test(org.junit.jupiter.api.Test)

Example 3 with ClientRequestFilter

use of jakarta.ws.rs.client.ClientRequestFilter in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method createRequestFilter.

// ///////////////////////////////////////////////////////////////////////
/**
 * Simulates server side
 *
 * @return Response containing request method and entity
 */
protected ClientRequestFilter createRequestFilter() {
    ClientRequestFilter filter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append(ctx.getMethod()).append(";");
            if (ctx.hasEntity())
                sb.append(ctx.getEntity()).append(";");
            Response r = Response.ok(sb.toString()).build();
            ctx.abortWith(r);
        }
    };
    return filter;
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response)

Example 4 with ClientRequestFilter

use of jakarta.ws.rs.client.ClientRequestFilter in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method fromResourceMethodLinkUsedInInvocationTest.

/*
     * @testName: fromResourceMethodLinkUsedInInvocationTest
     * 
     * @assertion_ids: JAXRS:JAVADOC:787;
     * 
     * @test_Strategy: Generate a link by introspecting a resource method.
     */
@Test
public void fromResourceMethodLinkUsedInInvocationTest() throws Fault {
    final String linkName = "link";
    Client client = ClientBuilder.newClient();
    client.register(new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext ctx) throws IOException {
            String uri = ctx.getUri().toASCIIString();
            Link.Builder builder = Link.fromMethod(Resource.class, "consumesAppJson").rel(linkName);
            Link link = builder.build();
            Response response = Response.ok(uri).links(link).build();
            ctx.abortWith(response);
        }
    });
    // Phase 1, ask for a link;
    WebTarget target = client.target(url() + "resource/get");
    Response response = target.request().get();
    String entity = response.readEntity(String.class);
    assertTrue(response.hasLink(linkName), "No link received");
    assertContains(url() + "resource/get", entity);
    // Phase 2, use the link, check the correctness
    assertTrue(response.hasLink(linkName), "No link received");
    Link link = response.getLink(linkName);
    response = client.invocation(link).post(null);
    entity = response.readEntity(String.class);
    assertTrue(response.hasLink(linkName), "No link received");
    assertContains(url() + "resource/consumesappjson", entity);
    logMsg("Opaque Link has been used in Client.invocation() sucessfully");
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response) Builder(jakarta.ws.rs.core.Link.Builder) UriBuilder(jakarta.ws.rs.core.UriBuilder) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) IOException(java.io.IOException) WebTarget(jakarta.ws.rs.client.WebTarget) JAXRSCommonClient(ee.jakarta.tck.ws.rs.common.JAXRSCommonClient) Client(jakarta.ws.rs.client.Client) Link(jakarta.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Example 5 with ClientRequestFilter

use of jakarta.ws.rs.client.ClientRequestFilter in project jaxrs-api by eclipse-ee4j.

the class ReaderClient method createRequestFilter.

/**
 * Create a request filter to be aborted with given fake response simulating
 * the resource from a request
 *
 * @param response
 * @return
 */
protected static ClientRequestFilter createRequestFilter(final Response response) {
    ClientRequestFilter outFilter = new ClientRequestFilter() {

        @Override
        public void filter(ClientRequestContext context) throws IOException {
            Response r;
            if (response == null)
                r = Response.ok().build();
            else
                r = response;
            context.abortWith(r);
        }
    };
    return outFilter;
}
Also used : ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Response(jakarta.ws.rs.core.Response)

Aggregations

ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)13 Response (jakarta.ws.rs.core.Response)11 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)10 Test (org.junit.jupiter.api.Test)4 Invocation (jakarta.ws.rs.client.Invocation)3 JAXRSCommonClient (ee.jakarta.tck.ws.rs.common.JAXRSCommonClient)2 Client (jakarta.ws.rs.client.Client)2 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)2 WebTarget (jakarta.ws.rs.client.WebTarget)2 StringBean (ee.jakarta.tck.ws.rs.common.provider.StringBean)1 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)1 Link (jakarta.ws.rs.core.Link)1 Builder (jakarta.ws.rs.core.Link.Builder)1 MediaType (jakarta.ws.rs.core.MediaType)1 UriBuilder (jakarta.ws.rs.core.UriBuilder)1 IOException (java.io.IOException)1