Search in sources :

Example 1 with WebTarget

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

the class BasicExamples method asyncCallbackUsingSubResourceClient.

public void asyncCallbackUsingSubResourceClient() throws Exception {
    final Client client = ClientBuilder.newClient();
    WebTarget anyCustomerUri = client.target("http://jaxrs.examples.org/jaxrsApplication/customers/{id}");
    // invoke a request in background
    Future<Customer> handle = // Target
    anyCustomerUri.resolveTemplate("id", 123).request().async().get(new InvocationCallback<Customer>() {

        @Override
        public void completed(Customer customer) {
        // do something
        }

        @Override
        public void failed(Throwable throwable) {
        // do something
        }
    });
    handle.cancel(true);
    // invoke another request in background
    // Target
    anyCustomerUri.resolveTemplate("id", 456).request().async().get(new InvocationCallback<Response>() {

        @Override
        public void completed(Response customer) {
        // do something
        }

        @Override
        public void failed(Throwable throwable) {
        // do something
        }
    });
    // invoke one more request using newClient
    Future<Response> response = anyCustomerUri.resolveTemplate("id", 789).request().cookie(new Cookie("fooName", "XYZ")).async().get();
    assert response.get() != null;
}
Also used : Response(jakarta.ws.rs.core.Response) Cookie(jakarta.ws.rs.core.Cookie) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) ThrottledClient(jaxrs.examples.client.custom.ThrottledClient)

Example 2 with WebTarget

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

the class BasicExamples method creatingResourceUriRequestsAndInvocations.

public void creatingResourceUriRequestsAndInvocations() {
    final Client client = ClientBuilder.newClient();
    final WebTarget customersUri = client.target("http://jaxrs.examples.org/jaxrsApplication/customers");
    // Create target request, customize it and invoke using newClient
    Response response = customersUri.request(MediaType.APPLICATION_XML).header("Foo", "Bar").get();
    assert response.getStatus() == 200;
}
Also used : Response(jakarta.ws.rs.core.Response) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) ThrottledClient(jaxrs.examples.client.custom.ThrottledClient)

Example 3 with WebTarget

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

the class BasicExamples method responseUsingSubResourceClient.

public void responseUsingSubResourceClient() {
    WebTarget customersUri = ClientBuilder.newClient().target("http://jaxrs.examples.org/jaxrsApplication/customers");
    WebTarget customer = customersUri.path("{id}");
    // Create a customer
    Response response = customersUri.request().post(xml(new Customer("Bill")));
    assert response.getStatus() == 201;
    Customer favorite;
    // view a customer
    favorite = customer.resolveTemplate("id", 123).request().get(Customer.class);
    assert favorite != null;
    // view a customer (alternative)
    favorite = customer.resolveTemplate("id", // Target ("http://jaxrs.examples.org/jaxrsApplication/customers/123/")
    123).request().get(Customer.class);
    assert favorite != null;
}
Also used : Response(jakarta.ws.rs.core.Response) WebTarget(jakarta.ws.rs.client.WebTarget)

Example 4 with WebTarget

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

the class BasicExamples method defaultResponse.

public void defaultResponse() {
    Customer customer;
    Response response;
    final WebTarget customersUri = ClientBuilder.newClient().target("http://jaxrs.examples.org/jaxrsApplication/customers");
    response = customersUri.path("{id}").resolveTemplate("id", 123).request().get();
    customer = response.readEntity(Customer.class);
    assert customer != null;
    response = customersUri.request().post(xml(new Customer("Marek")));
    assert response.getStatus() == 201;
}
Also used : Response(jakarta.ws.rs.core.Response) WebTarget(jakarta.ws.rs.client.WebTarget)

Example 5 with WebTarget

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

the class SpecExamples method benefitsOfResourceUri.

public void benefitsOfResourceUri() {
    Client client = ClientBuilder.newClient();
    WebTarget base = client.target("http://example.org/");
    WebTarget hello = base.path("hello").path("{whom}");
    final WebTarget whomToGreet = hello.resolveTemplate("whom", "world");
    Response res = whomToGreet.request().get();
}
Also used : Response(jakarta.ws.rs.core.Response) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) ThrottledClient(jaxrs.examples.client.custom.ThrottledClient)

Aggregations

WebTarget (jakarta.ws.rs.client.WebTarget)96 Test (org.junit.jupiter.api.Test)63 Client (jakarta.ws.rs.client.Client)52 JAXRSCommonClient (ee.jakarta.tck.ws.rs.common.JAXRSCommonClient)27 Response (jakarta.ws.rs.core.Response)24 Configuration (jakarta.ws.rs.core.Configuration)17 URI (java.net.URI)17 Invocation (jakarta.ws.rs.client.Invocation)16 Assertable (ee.jakarta.tck.ws.rs.api.rs.core.configurable.Assertable)14 SingleCheckAssertable (ee.jakarta.tck.ws.rs.api.rs.core.configurable.SingleCheckAssertable)14 JaxrsCommonClient (ee.jakarta.tck.ws.rs.common.client.JaxrsCommonClient)14 HashMap (java.util.HashMap)8 JdkLoggingFilter (ee.jakarta.tck.ws.rs.common.client.JdkLoggingFilter)6 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)5 InboundSseEvent (jakarta.ws.rs.sse.InboundSseEvent)5 SseEventSource (jakarta.ws.rs.sse.SseEventSource)5 ConfigurableObject (ee.jakarta.tck.ws.rs.api.rs.core.configurable.ConfigurableObject)4 LinkedHolder (ee.jakarta.tck.ws.rs.common.util.LinkedHolder)4 Configurable (jakarta.ws.rs.core.Configurable)4 ThrottledClient (jaxrs.examples.client.custom.ThrottledClient)4