Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1991

ResponseBuilder creates BuiltResponse even if called from the client filters

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 4.0.0.CR1, 3.7.0.Final
    • 3.6.1.Final, 4.0.0.Beta5
    • jaxrs
    • None

      If ResponseBuilder is used in the client filters and the built Response was initialized with the entity (stream) then Response.readEntity(...) fails because ResponseBuilder only produces BuiltResponse which does not implement readEntity and few other methods.

      In our particular case I needed to create a Response at the ClientResponseFilter level.

      Steps to reproduce:

      public class App {
          public static void main(String[] args) throws Exception {
              ResteasyClient client = new ResteasyClientBuilder().register(CustomResponseFilter.class).build();
              Response response = client.target("http://google.com").request().get();
              System.out.println(response.readEntity(String.class));
              System.out.println(response.getStatus());
          }
      }
      public class CustomResponseFilter implements ClientResponseFilter {
          public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
              Response response = Response.ok(responseContext.getEntityStream()).build();
              if (responseContext.getStatus() == 301) {
                  ResponseChecker301.check(response);
              }
      //      if (responseContext.getStatus() == 500) {
      //          ResponseChecker500.check(response);
      //      }
      //      ...
          }
      }
      public class ResponseChecker301 {
          public static void check(Response response) {
              String responseContent = response.readEntity(String.class);
              if (responseContent.contains("Error")) {
                  throw new RuntimeException(responseContent);
              }
          }
      }
      
      Exception in thread "main" javax.ws.rs.client.ResponseProcessingException: java.lang.IllegalStateException: RESTEASY003290: Entity is not backed by an input stream
      	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterResponse(ClientInvocation.java:627)
      	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:440)
      	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:61)
      	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:183)
      	at com.resteasy.test.App.main(App.java:14)
      Caused by: java.lang.IllegalStateException: RESTEASY003290: Entity is not backed by an input stream
      	at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:250)
      	at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:237)
      	at com.resteasy.test.ResponseChecker301.check(ResponseChecker301.java:10)
      	at com.resteasy.test.CustomResponseFilter.filter(CustomResponseFilter.java:17)
      	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterResponse(ClientInvocation.java:619)
      	... 4 more
      

      Proposals:

      • collapse ClientResponse and BuiltResponse into a single class.
      • Given that both Aborted and Client Response implement readEntity, would it be reasonable to push this code to the BuiltResponse ?
      • ...

              rsearls r searls
              sbiarozk Sergey Beryozkin
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: