router.route().handler(new LoggingHandler());
router.route().handler(TimeoutHandler.create(5000));
router.route().handler(ResponseTimeHandler.create());
router.route().handler(corsHandler());
router.route().handler(BodyHandler.create());
router.route().handler(new ETagHandler()); // i want this to work
router.route().handler(requestHandler());
I am using Apex on VertX3.
1) What is the very last handler that I can set on response, that is fired when response is written to the channel? I want to measure the request time.
For now, I am using the: routingContext.addHeadersEndHandler() as in ResponseTimeHandler - but according to the docs, the body is not yet written to the output. Maybe 'response.bodyEndHandler()` ?
2) How can I modify the content of the response? let me explain what I have:
router.route().handler(new LoggingHandler());
router.route().handler(TimeoutHandler.create(5000));
router.route().handler(ResponseTimeHandler.create());
router.route().handler(corsHandler());
router.route().handler(BodyHandler.create());
router.route().handler(new ETagHandler()); // i want this to work
router.route().handler(requestHandler());
`requestHandler` is my handler that uses worker verticles (and worker threads) and prepares the response. This all works, except my ETagHandler(). His job is to
A) capture the response bodyB) add some headersC) optionally to reset the body, so nothing is returned when content is not changed.
Because I need to add headers, my idea is to use `routingContext.addHeadersEndHandler()` to do the logic - but I don't have access to written body.
Which is perfectly fine - but then I am asking, is there some HttpServerResponseWrapper that can be used in chaining apex handlers, that captures the output to the buffer, that I can later simply flush to response (yeah, like in servlets).
Or I should try different approach, like to have my event eg "response.ready" and fiddle with that? Or to use routing context data to pass the response until some final FlushHandler()?
(Ideally, my requestHandler() should not be aware of ETagHandler changes.)
Thank you very much!
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
So, I'm a bit confused what you're trying to achieve here.
If you just want to measure processing time and write the result in the headers then there's already a handler in Apex called ResponseTimeHandler that does exactly that.
If you want to measure the time take to write the entire response including the body and put that time in a header - that's impossible because the headers will have been written already before the body is written.
Thanx, sorry for confusion, there are two separate questions in my post :)
1) Measure request time
So, I'm a bit confused what you're trying to achieve here.
If you just want to measure processing time and write the result in the headers then there's already a handler in Apex called ResponseTimeHandler that does exactly that.
If you want to measure the time take to write the entire response including the body and put that time in a header - that's impossible because the headers will have been written already before the body is written.
Sure - when I said to measure the request time, I was not thinking about sending this info back in the headers (ResponseTimeHandler that does exactly that, like you said). Instead, I want to log this time internally, so the result will be written to some files. Therefore, I am looking for the last point I can add handler to.
And for some reason, this is not working:
routingContext.addBodyEndHandler(event -> System.out.println("BodyEndHandler"));
while addHeadersEndHandler works.
2) Modification of response
Do we have any means in vertx to wrap the response (like in servlets filter), so some other handler can change the response? Like in above ETag example?
Or I should code this by myself?
btw, i figured out why endhandler is not working for me: because TimeoutHandler registers bodyEndHandler directly on the response(), not using RoutingContext array of handlers.
+1 Tim. This is a good advice to keep in mind.
I'm facing this kind of issues but when chaining handlers I often found myself needing access to the payload object after it has been retrieved (from disk, database, from a foreign service, ...). Obviously I could just put everything into one single handler, but that would be a messy if, else if, else if else if, kind of handler.
That's really not easy to deal with... How to benfit from the nice chaining handler feature Apex provides and give them acces to the request payload,