23

Adding data to the Vert.X web layer from your Quarkus application

 4 years ago
source link: https://itnext.io/adding-data-to-the-vert-x-web-layer-from-your-quarkus-application-da817e328a5f?source=friends_link&gi=774436705456
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adding data to the Vert.X web layer from your Quarkus application

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.

Image for post
Image for post

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.

Image for post
Image for post

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK