示例#1
0
  public void creatingResourceUriRequestsAndInvocations() {
    final Client client = ClientFactory.newClient();
    final Link customersUri = client.link("http://jaxrs.examples.org/jaxrsApplication/customers");

    // Create link request, customize it and invoke using newClient
    HttpRequest<?> request = customersUri.get();
    request.accept(MediaType.APPLICATION_XML).header("Foo", "Bar");
    HttpResponse responseA = client.invoke(request);
    assert responseA.getStatusCode() == 200;

    // Direct invocation leveraging the Invocation interface
    HttpResponse responseB =
        customersUri
            .get() // Invocation (extends HttpRequest)
            .accept(MediaType.APPLICATION_XML)
            .header("Foo", "Bar")
            .invoke();
    assert responseB.getStatusCode() == 200;
  }
示例#2
0
  public void asyncCallbackUsingSubResourceClient() throws Exception {
    final Client client = ClientFactory.newClient();
    Link anyCustomerUri = client.link("http://jaxrs.examples.org/jaxrsApplication/customers/{id}");

    // invoke a request in background
    Future<Customer> handle =
        anyCustomerUri
            .pathParam("id", 123) // Link
            .get() // Invocation (extends HttpRequest)
            .queue(
                new InvocationCallback<Customer>() {

                  @Override
                  public void onComplete(Future<Customer> future) {
                    // Do something
                  }
                });
    handle.cancel(true);

    // invoke another request in background
    anyCustomerUri
        .pathParam("id", 456) // Link
        .get() // Invocation (extends HttpRequest)
        .queue(
            new InvocationCallback<HttpResponse>() {

              @Override
              public void onComplete(Future<HttpResponse> future) {
                // do something
              }
            });

    // invoke one more request using newClient
    HttpRequest<?> request = anyCustomerUri.pathParam("id", 789).get();
    request.cookie(new Cookie("fooName", "XYZ"));
    Future<HttpResponse> response = client.queue(request);
    assert response.get() != null;
  }