I'm currently migrating an application from jersey 1 to 2. In the old app, we used a ClientFilter
for all jersey clients that automatically refreshed expired OAuth tokens like this:
@Override
public ClientResponse handle(ClientRequest cr) {
ClientResponse resp = getNext().handle(cr);
if (resp.getStatus() == Status.UNAUTHORIZED.getStatusCode()) {
// Try to refresh the token
boolean refreshed = refreshToken(oAuthInfo);
if (refreshed) {
resp = getNext().handle(cr);
}
}
return resp;
}
It might not haven been the most elegant way, but the benefit was that rest client users did not have to care about expired tokens themselves.
With the ContainerResponseFilter
for jersey 2, this does not seem to be that simple anymore. The only option I currently see is to use the ClientRequestContext
and try to re-create the original request using getClient
, getHeaders
etc... and then update the result in ContainerResponseContext
. This however seems a bit clunky so I was wondering if there is any more convenient way to refresh an OAuth token without having to deal with this wherever a jersey client is used?