5 Replies Latest reply on Jun 13, 2014 4:21 PM by marrrck

    Need some Advice about RestClientInterceptors

    marrrck

      Hello Everyone,

       

      I'm trying to implement a fallback to-local cache behavior on the client using Errai JPA when a JAX-RS call fails. I'm trying to do this in a way that is transparent to the code invoking the rest service, but I'm running into some trouble getting it working.

       

      I've tried implementing a RestClientInterceptor, but once I make the RestCallbackContext.proceed() and the rest call fails, I'm having trouble getting out of my RestErrorCallback back into a non-error state. I've tried calling RestCallbackContext.setResult(..) from inside the error callback and returning false to stop handling the error, but that still never invokes the downstream RemoteCallbacks so the results are never read. Is there any other way to accomplish this with a RestClientInterceptor, or some other way that I haven't thought Of? I also tried extending the CallerProvider class using @Alternative and added the fallback to cache logic there, but it doesn't seem like wiring for IOCProviders respects the @Alternative directive (or at least, I couldn't get it to work).

       

      Any ideas? I'm happy to provide some code snippets if that would help explain what I'm asking...

       

      Thanks for your help!

       

      Marc

        • 1. Re: Need some Advice about RestClientInterceptors
          csa

          Hi Marc,

           

          Can you elaborate what you mean by "rest call fails". Do you mean the server returning an error (status code other than 200/201)? In that case you should be able to use a ResponseCallback (org.jboss.errai.enterprise.client.jaxrs.api.ResponseCallback) in the interceptor, check for the status code and call context.setResult().

           

          Or do you mean the client having no connection and throwing an exception?

           

          Cheers,

          Christian

          • 2. Re: Need some Advice about RestClientInterceptors
            marrrck

            Hi Christian,

             

            Thanks for the reply! I was hoping to handle both scenarios, but the primary use case would be the client device having no connection. I'm using cordova so I guess for the connection use-case I can check to see if the device has any connection, then also use a ResponseCallback for http errors like 404. I'll give it a shot.

            • 3. Re: Need some Advice about RestClientInterceptors
              csa

              Hi Marc,

               

              I am starting to think now that we should give interceptors the power to call context.setResult in an ErrorCallback and subsequently cause the RemoteCallback(s) to be invoked. It seems this would make your code a lot easier.

               

              Cheers,

              Christian

              • 4. Re: Need some Advice about RestClientInterceptors
                csa

                OK I've published new 3.1.0- and 3.0.1-SNAPSHOTs that contain a fix for this. Your original approach should work now.

                • 5. Re: Need some Advice about RestClientInterceptors
                  marrrck

                  Nice! Javadocs and unit tests and everything Thank you!