Cookie management in Quarkus

1.4k Views Asked by At

How do I configure cookie management in the Quarkus rest client?

I am building a client to an API that sets a session cookie on login and then uses that session cookie for auth - a pretty standard stateful API.

It seems that with default behaviour with @RegisterRestClient does not preserve cookies between requests.

Having a dig, it seems that ResteasyClientBuilderImpl has a field cookieManagementEnabled which is false by default. This then disables cookies in ClientHttpEngineBuilder43 by default too.

I also found that if you use the async engine (useAsyncHttpEngine) this sync engine builder is not used and cookie management is also enabled.

The only way I found to configure the ResteasyClientBuilder underlying the RestClientBuilder was to use a RestClientBuilderListener. However, the plot thickens here.

There is a method in RestClientBuilder - property(String name, Object value) - that, in the case of Quarkus at least will delegate to the underlying ResteasyClientBuilder

if (name.startsWith(RESTEASY_PROPERTY_PREFIX)) {
    // Makes it possible to configure some of the ResteasyClientBuilder delegate properties

However, a few lines down

Method builderMethod = Arrays.stream(ResteasyClientBuilder.class.getMethods())
  .filter(m -> builderMethodName.equals(m.getName()) && m.getParameterTypes().length >= 1)
  .findFirst()
  .orElse(null);

i.e. it will only allows for non-nullary methods to be called. And to enable cookie management I need to call enableCookieManagement - which has no arguments.

So, the only way I have found to enable cookie management is:

class CookieConfigBuilderListener : RestClientBuilderListener {
    override fun onNewBuilder(builder: RestClientBuilder) {
        val builderImpl = builder as QuarkusRestClientBuilder
        val delegateField = QuarkusRestClientBuilder::class.java.getDeclaredField("builderDelegate")
        delegateField.isAccessible = true
        val resteasyBuilder = delegateField.get(builderImpl) as ResteasyClientBuilder
        resteasyBuilder.enableCookieManagement()
    }
}

This is obviously far from ideal, not least because it uses reflection in a GraalVM native image setting and therefore will likely also require me to enable reflection on QuarkusRestClientBuilder.

So, back to the original question, how do I enable cookie management in the Quarkus rest client?

0

There are 0 best solutions below