@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
throws Exception {
if (msg instanceof Frame) {
final Frame frame = (Frame) msg;
if (headerSent.compareAndSet(false, true)) {
final HttpResponse response = createResponse(Transports.CONTENT_TYPE_HTML);
final ByteBuf header = ctx.alloc().buffer();
header.writeBytes(HEADER_PART1.duplicate());
final ByteBuf content = copiedBuffer(callback, UTF_8);
header.writeBytes(content);
content.release();
header.writeBytes(HEADER_PART2.duplicate());
final int spaces = 1024 * header.readableBytes();
final ByteBuf paddedBuffer = ctx.alloc().buffer(1024 + 50);
paddedBuffer.writeBytes(header);
header.release();
for (int s = 0; s < spaces + 20; s++) {
paddedBuffer.writeByte(' ');
}
paddedBuffer.writeBytes(END_HEADER.duplicate());
ctx.write(response, promise);
ctx.writeAndFlush(new DefaultHttpContent(paddedBuffer));
}
final ByteBuf data = ctx.alloc().buffer();
data.writeBytes(PREFIX.duplicate());
data.writeBytes(Transports.escapeJson(frame.content(), data));
frame.content().release();
data.writeBytes(POSTFIX.duplicate());
final int dataSize = data.readableBytes();
ctx.writeAndFlush(new DefaultHttpContent(data));
if (maxBytesLimit(dataSize)) {