Question

I have incoming HTTP requests with protobuf message in the HTTP content. So far I managed to use the Snoop (HTTP server) example and modify the handler part to parse the HTTP content into protobuf message and back to bytes for output. But I guess this is not the optimal way to do this.

Is it possible to use the built-in Protobuf encoder/decoder in Netty in the pipeline? So first HTTPResponseDecoder (or something more convenient) "chops" off the header and passes only the content part to the FrameDecoder + ProtobufDecoder then passes the message to myAppHandler where business logic is applied?

I am a bit confused how message (or part of it) is passed to the next handler. Or am I on totally on the wrong track here?

Maybe a link to an example using protobuf with HTTP content would also serve as an explanation if there's any.

Thnx in advance.

Was it helpful?

Solution

Thank you for the silent support :]

The solution is to use HttpObjectAggregator() and simply write a handler taking care of the byte-to-message process using the FullHttpRequest from the aggregator handler. The output of this decoder is the protobuf message:

public class ProtoRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> {
    @Override
    protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {


        byte[] payloadBytes = new byte[msg.content().readableBytes()];
        msg.content().readBytes(payloadBytes);

        MyMessage protoMessage = MyMessage.parseFrom(payloadBytes);

        out.add(protoMessage);

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top