package io.netty.handler.codec.stomp;
import java.util.List;
import java.util.Map.Entry;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.AsciiHeadersEncoder;
import io.netty.handler.codec.AsciiHeadersEncoder.NewlineType;
import io.netty.handler.codec.AsciiHeadersEncoder.SeparatorType;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;
public class StompSubframeEncoder extends MessageToMessageEncoder<StompSubframe> {
@Override
protected void encode(ChannelHandlerContext ctx, StompSubframe msg, List<Object> out) throws Exception {
if (msg instanceof StompFrame) {
StompFrame frame = (StompFrame) msg;
ByteBuf frameBuf = encodeFrame(frame, ctx);
out.add(frameBuf);
ByteBuf contentBuf = encodeContent(frame, ctx);
out.add(contentBuf);
} else if (msg instanceof StompHeadersSubframe) {
StompHeadersSubframe frame = (StompHeadersSubframe) msg;
ByteBuf buf = encodeFrame(frame, ctx);
out.add(buf);
} else if (msg instanceof StompContentSubframe) {
StompContentSubframe stompContentSubframe = (StompContentSubframe) msg;
ByteBuf buf = encodeContent(stompContentSubframe, ctx);
out.add(buf);
}
}
private static ByteBuf encodeContent(StompContentSubframe content, ChannelHandlerContext ctx) {
if (content instanceof LastStompContentSubframe) {
ByteBuf buf = ctx.alloc().buffer(content.content().readableBytes() + 1);
buf.writeBytes(content.content());
buf.writeByte(StompConstants.NUL);
return buf;
} else {
return content.content().retain();
}
}
private static ByteBuf encodeFrame(StompHeadersSubframe frame, ChannelHandlerContext ctx) {
ByteBuf buf = ctx.alloc().buffer();
buf.writeCharSequence(frame.command().toString(), CharsetUtil.US_ASCII);
buf.writeByte(StompConstants.LF);
AsciiHeadersEncoder headersEncoder = new AsciiHeadersEncoder(buf, SeparatorType.COLON, NewlineType.LF);
for (Entry<CharSequence, CharSequence> entry : frame.headers()) {
headersEncoder.encode(entry);
}
buf.writeByte(StompConstants.LF);
return buf;
}
}