Skip to content
Advertisement

Is there an elegant way to exit ClientRequestFilter?

I implemented a ClientRequestFilter. But one call of a client should not be filtered, which means if the request comes from this class (in my case the class is called TokenClient) the method should just return. Right now as you can see I check the path and if it contains /token it will return. But I would rather check if the class is ofInstance TokenClient. How can I do that ?

@Provider
public class MyClientRequestFilter implements ClientRequestFilter {

    @Inject
    MyClient myClient;

    @Override
    public void filter(ClientRequestContext clientRequestContext) throws IOException {

        if(clientRequestContext.getUri().getPath().contains("/token"))
        {
            return;
        }

        String token= myClient.getToken();

        clientRequestContext.getHeaders().addFirst("Authorization", "Bearer "+token);
    }
}

Answer

There is a way to know which method and it described here.

Essentially you do something like:

@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {

    Method targetMethod = (Method)clientRequestContext.getProperty("org.eclipse.microprofile.rest.client.invokedMethod");

    // check target method
    
}
Advertisement