@Test public void write() throws IOException { final String url = "/test/htmlfile?c=%63allback"; final EmbeddedChannel ch = newHtmlFileChannel("/test/htmlfile?c=%63allback"); ch.writeInbound(new DefaultHttpRequest(HTTP_1_1, GET, url)); ch.writeOutbound(new OpenFrame()); final HttpResponse response = ch.readOutbound(); assertThat(response.getStatus(), equalTo(OK)); assertThat(response.headers().get(CONTENT_TYPE), equalTo(Transports.CONTENT_TYPE_HTML)); verifyNoCacheHeaders(response); final HttpContent headerChunk = ch.readOutbound(); assertThat(headerChunk.content().readableBytes(), is(greaterThan(1024))); final String header = headerChunk.content().toString(UTF_8); assertThat(header, containsString("var c = parent.callback")); final HttpContent chunk = ch.readOutbound(); assertThat(chunk.content().toString(UTF_8), equalTo("<script>\np(\"o\");\n</script>\r\n")); ch.write(new MessageFrame("x")); final HttpContent messageContent = ch.readOutbound(); assertThat(messageContent.content().toString(UTF_8), equalTo("<script>\np(\"a[\\\"x\\\"]\");\n</script>\r\n")); }
@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)) { if (logger.isDebugEnabled()) { logger.debug("max bytesSize limit reached [{}]", config.maxStreamingBytesSize()); } ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); } } else { ctx.write(ReferenceCountUtil.retain(msg), promise); } }
HtmlFileTransport extends ChannelHandlerAdapter { @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)) { if (logger.isDebugEnabled()) { logger.debug("max bytesSize limit reached [{}]", config.maxStreamingBytesSize()); } ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); } } else { ctx.write(ReferenceCountUtil.retain(msg), promise); } } }
HtmlFileTransport extends ChannelHandlerAdapter { @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)) { if (logger.isDebugEnabled()) { logger.debug("max bytesSize limit reached [{}]", config.maxStreamingBytesSize()); } ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); } } else { ctx.write(ReferenceCountUtil.retain(msg), promise); } } HtmlFileTransport(final SockJsConfig config, final HttpRequest request); }
HtmlFileTransport extends ChannelHandlerAdapter { @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)) { if (logger.isDebugEnabled()) { logger.debug("max bytesSize limit reached [{}]", config.maxStreamingBytesSize()); } ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); } } else { ctx.write(ReferenceCountUtil.retain(msg), promise); } } HtmlFileTransport(final SockJsConfig config, final HttpRequest request); @Override void channelRead(final ChannelHandlerContext ctx, final Object msg); @Override void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise); }
HtmlFileTransport extends ChannelHandlerAdapter { @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)) { if (logger.isDebugEnabled()) { logger.debug("max bytesSize limit reached [{}]", config.maxStreamingBytesSize()); } ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener(ChannelFutureListener.CLOSE); } } else { ctx.write(ReferenceCountUtil.retain(msg), promise); } } HtmlFileTransport(final SockJsConfig config, final HttpRequest request); @Override void channelRead(final ChannelHandlerContext ctx, final Object msg); @Override void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise);  }