Search in sources :

Example 1 with CodecPrintWriter

use of org.grails.buffer.CodecPrintWriter in project grails-core by grails.

the class MockGrailsApplication method testCodecAndNoCodecGRAILS8405.

@Test
public void testCodecAndNoCodecGRAILS8405() throws IOException {
    FastStringWriter target = new FastStringWriter();
    GrailsWebRequest webRequest = bindMockHttpRequest();
    // Initialize out and codecOut as it is done in GroovyPage.initRun
    OutputEncodingStack outputStack = OutputEncodingStack.currentStack(true, target, false, true);
    GrailsPrintWriter out = outputStack.getOutWriter();
    webRequest.setOut(out);
    GrailsPrintWriter codecOut = new CodecPrintWriter(out, getEncoder(new MockGrailsApplication(), CodecWithClosureProperties.class), registry);
    // print some output
    codecOut.print("hola");
    codecOut.flush();
    out.print("1");
    out.print("2");
    out.print("3");
    // similar as taglib call
    FastStringWriter bufferWriter = new FastStringWriter();
    GrailsPrintWriter out2 = new GrailsPrintWriter(bufferWriter);
    outputStack.push(out2);
    out.print("4");
    codecOut.print("A");
    codecOut.flush();
    outputStack.pop();
    // add output before appending "taglib output"
    out.print("added");
    codecOut.print("too");
    codecOut.flush();
    // append "taglib output"
    out.leftShift(bufferWriter.getBuffer());
    // print some more output
    codecOut.print("B");
    codecOut.flush();
    out.print("5");
    codecOut.print("C");
    codecOut.flush();
    // clear thread local
    RequestContextHolder.resetRequestAttributes();
    assertEquals("-> hola <-123added-> too <-4-> A <--> B <-5-> C <-", target.getValue());
    codecOut.close();
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) GrailsPrintWriter(org.grails.buffer.GrailsPrintWriter) GrailsWebRequest(org.grails.web.servlet.mvc.GrailsWebRequest) OutputEncodingStack(org.grails.taglib.encoder.OutputEncodingStack) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) Test(org.junit.Test)

Example 2 with CodecPrintWriter

use of org.grails.buffer.CodecPrintWriter in project grails-core by grails.

the class MockGrailsApplication method testPrintString.

@Test
public void testPrintString() {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), HTMLCodec.class), registry);
    writer.print("&&");
    writer.flush();
    assertEquals("&amp;&amp;", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) HTMLCodec(org.grails.plugins.codecs.HTMLCodec) Test(org.junit.Test)

Example 3 with CodecPrintWriter

use of org.grails.buffer.CodecPrintWriter in project grails-core by grails.

the class GroovyPagesTemplateRenderer method wrapWriterWithEncoder.

private Writer wrapWriterWithEncoder(GrailsWebRequest webRequest, Map<String, Object> attrs, Writer out) {
    Object encodeAs = attrs.get(GroovyPage.ENCODE_AS_ATTRIBUTE_NAME);
    if (encodeAs != null) {
        Map<String, Object> codecSettings = WithCodecHelper.makeSettingsCanonical(encodeAs);
        String codecForTaglibs = (String) codecSettings.get(OutputEncodingSettings.TAGLIB_CODEC_NAME);
        if (codecForTaglibs != null) {
            Encoder encoder = WithCodecHelper.lookupEncoder(webRequest.getAttributes().getGrailsApplication(), codecForTaglibs);
            if (out instanceof EncodedAppenderWriterFactory) {
                out = ((EncodedAppenderWriterFactory) out).getWriterForEncoder(encoder, webRequest.getEncodingStateRegistry());
            } else if (encoder instanceof StreamingEncoder) {
                out = new StreamingEncoderWriter(out, (StreamingEncoder) encoder, webRequest.getEncodingStateRegistry());
            } else {
                out = new CodecPrintWriter(out, encoder, webRequest.getEncodingStateRegistry());
            }
        }
    }
    return out;
}
Also used : Encoder(org.grails.encoder.Encoder) StreamingEncoder(org.grails.encoder.StreamingEncoder) StreamingEncoderWriter(org.grails.encoder.StreamingEncoderWriter) GroovyObject(groovy.lang.GroovyObject) StreamingEncoder(org.grails.encoder.StreamingEncoder) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) EncodedAppenderWriterFactory(org.grails.encoder.EncodedAppenderWriterFactory)

Example 4 with CodecPrintWriter

use of org.grails.buffer.CodecPrintWriter in project grails-core by grails.

the class MockGrailsApplication method testPrintStreamCharBufferWithClosure.

@Test
public void testPrintStreamCharBufferWithClosure() throws IOException {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), CodecWithClosureProperties.class), registry);
    StreamCharBuffer buf = new StreamCharBuffer();
    buf.getWriter().write("hola");
    writer.write(buf);
    writer.flush();
    assertEquals("-> hola <-", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) Test(org.junit.Test)

Example 5 with CodecPrintWriter

use of org.grails.buffer.CodecPrintWriter in project grails-core by grails.

the class MockGrailsApplication method testPrintStreamCharBuffer.

@Test
public void testPrintStreamCharBuffer() throws IOException {
    FastStringWriter stringwriter = new FastStringWriter();
    CodecPrintWriter writer = new CodecPrintWriter(stringwriter, getEncoder(new MockGrailsApplication(), HTMLCodec.class), registry);
    StreamCharBuffer buf = new StreamCharBuffer();
    buf.getWriter().write("&&");
    writer.write(buf);
    writer.flush();
    assertEquals("&amp;&amp;", stringwriter.getValue());
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) CodecPrintWriter(org.grails.buffer.CodecPrintWriter) StreamCharBuffer(org.grails.buffer.StreamCharBuffer) HTMLCodec(org.grails.plugins.codecs.HTMLCodec) Test(org.junit.Test)

Aggregations

CodecPrintWriter (org.grails.buffer.CodecPrintWriter)6 FastStringWriter (org.grails.buffer.FastStringWriter)5 Test (org.junit.Test)5 StreamCharBuffer (org.grails.buffer.StreamCharBuffer)2 HTMLCodec (org.grails.plugins.codecs.HTMLCodec)2 GroovyObject (groovy.lang.GroovyObject)1 GrailsPrintWriter (org.grails.buffer.GrailsPrintWriter)1 EncodedAppenderWriterFactory (org.grails.encoder.EncodedAppenderWriterFactory)1 Encoder (org.grails.encoder.Encoder)1 StreamingEncoder (org.grails.encoder.StreamingEncoder)1 StreamingEncoderWriter (org.grails.encoder.StreamingEncoderWriter)1 OutputEncodingStack (org.grails.taglib.encoder.OutputEncodingStack)1 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)1