Search in sources :

Example 1 with ArrayOfByteArraysOutput

use of com.fizzed.rocker.runtime.ArrayOfByteArraysOutput in project ratpack by ratpack.

the class DefaultRockerRenderer method render.

@Override
public void render(Context context, RockerModel rockerModel) throws Exception {
    try {
        ArrayOfByteArraysOutput output = rockerModel.render(ArrayOfByteArraysOutput.FACTORY);
        List<byte[]> arrays = output.getArrays();
        ByteBuf byteBuf;
        int size = arrays.size();
        if (size == 0) {
            byteBuf = Unpooled.EMPTY_BUFFER;
        } else if (size == 1) {
            byteBuf = Unpooled.wrappedBuffer(arrays.get(0));
        } else {
            byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, size, Iterables.transform(arrays, Unpooled::wrappedBuffer));
        }
        AsciiString contentType = output.getContentType() == ContentType.HTML ? HTML : TEXT;
        context.getResponse().contentTypeIfNotSet(contentType).send(byteBuf);
    } catch (Exception e) {
        // This can be removed when the above issue is rectified.
        throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) RendererException(ratpack.render.RendererException) ArrayOfByteArraysOutput(com.fizzed.rocker.runtime.ArrayOfByteArraysOutput) AsciiString(io.netty.util.AsciiString) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) RendererException(ratpack.render.RendererException)

Aggregations

ArrayOfByteArraysOutput (com.fizzed.rocker.runtime.ArrayOfByteArraysOutput)1 ByteBuf (io.netty.buffer.ByteBuf)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 AsciiString (io.netty.util.AsciiString)1 RendererException (ratpack.render.RendererException)1