0
votes

i´m implementing a Restful service using Jax-RS 2.0 (Resteasy 3.0.7.Final) and share the interface between client and service.

The return value is void because ClientResponse is deprecated since RestEasy introduced JAX-RS 2.0 in version 3+.

To return the location of the new created object i inject the response, using the @Context annotation, and add the Content-Location header.

For example:

Shared Interface:

   @Path("/")
   @Consumes("application/xml")
   @Produces("application/xml")
   interface Resource {

        @Path("createSomething")
        void createSomething(AnyObject object);

        ...
   }

Implementation class (The Service):

    class ResourceImpl {

         ...
         @Context org.jboss.resteasy.spi.HttpResponse response;
         ...

         @Override
         void createSomething(AnyObject object) throws AnyException {

             String id = service.create(object);

             response.getOutputHeaders().putSingle("Content-Location",
                  "/createSomething/" + id);

             response.setStatus(Response.Status.CREATED.getStatusCode());
         }

    }

The client (build with the Resteasy Proxy Framework):

     ...
     ResteasyClient client = new ResteasyClientBuilder().build();
     ResteasyWebTarget target = client.target(baseUrl);

     Resource resource = (Resource) target.proxy(Resource.class);   

     resource.createSomething(anyObject);
     ...

How can i retrieve Header information (and others, like Atom Links) which has been injected by the service?

Is it reasonable to use client side Filters and Interceptors?

Thank You

1

1 Answers

0
votes

The best solution i found was to use a Filter to process the incoming response header.

public class HeaderFilter implements ClientResponseFilter {


  private Map<String, String> headers = new HashMap<>();

  private List<String> headerFilter = new ArrayList<>();


  public final void addHeaderFilter(final String header) {

      headerFilter.add(header);
  }

  public final void removeHeaderFilter(final String header) {

      headerFilter.remove(header);
  }

  public final String getHeader(final String header) {

      return headers.get(header);
  }

  @Override
  public final void filter(final ClientRequestContext requestContext,
          final ClientResponseContext responseContext) 
                                      throws IOException {

      headers = new HashMap<>();

      for (String headerToLookFor : headerFilter) {

          String header = responseContext.getHeaderString(headerToLookFor);

          if (header != null) {

              headers.put(headerToLookFor, header);
          } else {

              ...
          }
      }
  }
}