Search in sources :

Example 1 with RenderContextImpl

use of org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl in project sling by apache.

the class SightlyCompiledScript method eval.

@Override
public Object eval(ScriptContext context) throws ScriptException {
    Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingBindings slingBindings = new SlingBindings();
    slingBindings.putAll(bindings);
    SlingHttpServletRequest request = slingBindings.getRequest();
    if (request == null) {
        throw new SightlyException("Missing SlingHttpServletRequest from ScriptContext.");
    }
    Object oldBindings = request.getAttribute(SlingBindings.class.getName());
    try {
        request.setAttribute(SlingBindings.class.getName(), slingBindings);
        RenderContext renderContext = new RenderContextImpl(context);
        PrintWriter out = new PrintWriter(context.getWriter());
        renderUnit.render(out, renderContext, new SimpleBindings());
    } finally {
        request.setAttribute(SlingBindings.class.getName(), oldBindings);
    }
    return null;
}
Also used : RenderContext(org.apache.sling.scripting.sightly.render.RenderContext) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SimpleBindings(javax.script.SimpleBindings) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) Bindings(javax.script.Bindings) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SimpleBindings(javax.script.SimpleBindings) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 2 with RenderContextImpl

use of org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl in project sling by apache.

the class SightlyJavaCompilerServiceTest method getInstancePojoTest.

private void getInstancePojoTest(String className) throws Exception {
    RenderContextImpl renderContext = Mockito.mock(RenderContextImpl.class);
    JavaCompiler javaCompiler = Mockito.mock(JavaCompiler.class);
    CompilationResult compilationResult = Mockito.mock(CompilationResult.class);
    when(compilationResult.getErrors()).thenReturn(new ArrayList<CompilerMessage>());
    when(javaCompiler.compile(Mockito.any(CompilationUnit[].class), Mockito.any(Options.class))).thenReturn(compilationResult);
    when(classLoaderWriter.getClassLoader().loadClass(className)).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return MockPojo.class;
        }
    });
    Whitebox.setInternalState(compiler, "classLoaderWriter", classLoaderWriter);
    Whitebox.setInternalState(compiler, "javaCompiler", javaCompiler);
    Whitebox.setInternalState(compiler, "scriptingResourceResolverProvider", scriptingResourceResolverProvider);
    Object obj = compiler.getResourceBackedUseObject(renderContext, className);
    assertTrue("Expected to obtain a " + MockPojo.class.getName() + " object.", obj instanceof MockPojo);
}
Also used : Options(org.apache.sling.commons.compiler.Options) CompilerMessage(org.apache.sling.commons.compiler.CompilerMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) JavaCompiler(org.apache.sling.commons.compiler.JavaCompiler) CompilationResult(org.apache.sling.commons.compiler.CompilationResult)

Aggregations

RenderContextImpl (org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl)2 PrintWriter (java.io.PrintWriter)1 Bindings (javax.script.Bindings)1 SimpleBindings (javax.script.SimpleBindings)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1 CompilationResult (org.apache.sling.commons.compiler.CompilationResult)1 CompilerMessage (org.apache.sling.commons.compiler.CompilerMessage)1 JavaCompiler (org.apache.sling.commons.compiler.JavaCompiler)1 Options (org.apache.sling.commons.compiler.Options)1 SightlyException (org.apache.sling.scripting.sightly.SightlyException)1 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1