I have created an access log filter in my Quarkus application, that works as a Handler at the Vert.x web router layer. Now I need to pass data from my business logic at the JAX-RS layer to this access log. This post shows a possible solution.

Connected pipes by Jean-Paul Jandrain on Pixabay

In my last two articles (1, 2) I talked about how to modify HTTP-requests at the layer of the Vert.x web router, which is then transparent to the upper layers.

Request flow (red) and data flow for my code (green)

This time I need to transfer data down (green arrow in above diagram). I could add it to the headers of the HTTP response, but as this data should not be sent to the client, it is not an option.

My business code sits in a JAX-RS ContainerRequestFilter and pulls out the accountId from incoming requests after some processing of a header field:

@PreMatching

@Provider

public class MyFilter implements ContainerRequestFilter { @Override

public void filter(ContainerRequestContext requestContext)

throws IOException {



String xrhid_header = requestContext

.getHeaderString("x-rh-identity"); String accountId = getAccountId(xrhid_header); // see below

But how can I now forward this to my logging handler in the Vert.x layer?

CDI has it all

Quarkus provides a lot of resources within CDI, ready to inject into your code. One of the resources is the Vert.x RoutingContext of the current HTTP request. This context has a method put(key,value) to forward arbitrary content along the chaing of handlers and RouteFilters. We can obtain the context with this helper:

CurrentVertxRequest request() {

if (currentVertxRequest == null) {

currentVertxRequest = CDI.current()

.select(CurrentVertxRequest.class).get();

}

return currentVertxRequest;

}

and then make use of it in our ContainerRequestFilter:

RoutingContext routingContext = request().getCurrent();

routingContext.put("x-rh-account", accountId);

We can then later in our logging handler retrieve the data from the routing context again:

void log(RoutingContext context, ... {

String acctId = context.get("x-rh-account");

Conclusion

The above code lives in a few different places in the code base. Above is an abbreviated version and you can see the full code in the linked classes on GitHub. You can still see how it is possible to access the Vert.x web routing layer from the higher more business oriented layers in your Quarkus code and make business data available to the Vert.x web layer.