Nick Ginanto
Nick Ginanto

Reputation: 32170

add headers to a response fired by ChannelHandlerContext (Netty)

I have a jar, that I can't edit, that adds several headers to an http response. It then takes the response andctx.writeAndFlush(resp)

Is there a way to catch this response elsewhere (like a middleware) and edit it (add/remove headers, etc)?

The code uses Netty http tranport

Upvotes: 1

Views: 558

Answers (2)

Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7983

If you have access to the ChannelHandlerContext (ctx), the Channel or the Pipeline through which the response propagates. Then you can access the response by adding a ChannelOutboundHandler to the Pipeline and overriding the write or the flush method of this handler. In these methods you can modify the response.

Upvotes: 1

Norman Maurer
Norman Maurer

Reputation: 23567

Sure you can add your own ChannelOutboundHandlerAdapter and override write(...). Here you can adjust the response on the fly before you call ctx.write(...) again and pass it on.

Just ensure you put your handler before the other handler in the ChannelPipeline.

Upvotes: 0

Related Questions