users@jersey.java.net

[Jersey] ClientRequestFilter, replace with a new request

From: Maarten Boekhold <boekhold_at_gmx.com>
Date: Tue, 03 Mar 2015 14:12:21 +0400

Hi all,

I'm trying to implement a custom authentication scheme by writing
ClientRequestFilter/ClientResponseFilter implementations. As part of the
'flow' that I would like to implement, I need to do an additional
request inside the ClientRequestFilter.filter() method, and based on the
result of that request, I need to 'repeat' the original request. I'm
struggling a bit with how to ensure the original caller should get back
the proper response. "proper response" in this case is:

  * If the additional request failed, return the response of that
    additional request
  * Otherwise return the response of the "repeat" request

Since ClientRequestFilter doesn't allow me to manipulate any response
object yet, I think I need to somehow need to delegate the additional
request to the ClientResponseFilter.filter() methd. I'm struggling a bit
on how to do this. What I'm thinking of right now is something like:

  * Clone the original ClientRequestContext
  * Replace the original ClientRequestContext with the additional
    request (a special login call), and store the cloned
    ClientRequestContext on the modified ClientRequestContext
  * In ClientRequestFilter.filter(), handle the response of the modified
    ClientRequestContext, and based on that response either directly
    return the result of the modified ClientRequestContext (in case of
    login error), or 'play' the cloned ClientRequestContext and then
    update the ClientResponseContext with the response of the 'repeat
    request'.

I know how to do the first and third steps, but I'm having some trouble
figuring out how to modify the original ClientRequestContext to do a
completely different type of request (POST of an
APPLICATION_FORM_URLENCODED_TYPE form).

Questions:
(1) does this approach look OK?
(2) if so, how can I implement that second step?

Maarten