Get the corresponding HttpClient in a HttpClientFilter (micronaut)

68 Views Asked by At

I am attempting to write a reusable HttpClientFilter that can be assigned to various HttpClients using an Annotation. The HttpClientFilter requires certain properties, such as an audience, that differ for each HttpClient.

My plan is to add these properties to the annotation that gets assigned to the HttpClient.

@FilterMatcher
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.PARAMETER})
public @interface ServiceAccountAuthentication {
    String audience() default "";
}
@Client(value = "http://example.com/api/v1")
@ServiceAccountAuthentication(audience = "test")
public interface MyHttpClient extends MyApi {
    
}
@ServiceAccountAuthentication
@Singleton
public class ServiceAccountAuthenticationClientFilter implements HttpClientFilter {

    @Override
    public Publisher<? extends HttpResponse<?>> doFilter(MutableHttpRequest<?> request, ClientFilterChain chain) {
        String audience = "???"; // How do I retrieve that from the annotation?
        return chain.proceed(request.bearerAuth(buildToken(audience)));
    }

    private String buildToken(String audience) {
        // TODO: Build token
        return "";
    }
}

However, to acquire these properties within the HttpClientFilter, I need to retrieve the class of the HttpClient. Is there a method to do this? Or is there a better way to implement something like that?

0

There are 0 best solutions below