Search in sources :

Example 11 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class PortalSessionAuthorizationFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (request instanceof HttpServletRequest) {
        HttpServletRequest hr = (HttpServletRequest) request;
        HttpSession session = hr.getSession(false);
        if (session != null) {
            Object userId = session.getAttribute("userId");
            if (userId != null) {
                Groovity groovity = (Groovity) servletContext.getAttribute(GroovityServlet.SERVLET_CONTEXT_GROOVITY_INSTANCE);
                Script factory;
                try {
                    factory = groovity.load("/data/factory", new Binding());
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                    throw new ServletException(e);
                }
                Object user = factory.invokeMethod("call", new String[] { "person", userId.toString() });
                if (user != null && user instanceof Principal) {
                    request = new AuthenticatedRequestWrapper(hr, (Principal) user);
                }
            }
        }
    }
    chain.doFilter(request, response);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) HttpSession(javax.servlet.http.HttpSession) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Groovity(com.disney.groovity.Groovity) AuthenticatedRequestWrapper(com.disney.http.auth.server.AuthenticatedRequestWrapper) GroovyObject(groovy.lang.GroovyObject) Principal(java.security.Principal)

Example 12 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class ScriptHelper method startRun.

public void startRun(final Script script) {
    final Binding oldBinding = THREAD_BINDING.get();
    final Binding scriptBinding = script.getBinding();
    if (oldBinding != scriptBinding) {
        if (oldBinding != null) {
            scriptBinding.setVariable(THREAD_BINDING_PREVIOUS, oldBinding);
        }
        scriptBinding.setVariable(THREAD_BINDING_OWNER, script);
        THREAD_BINDING.set(scriptBinding);
    }
}
Also used : Binding(groovy.lang.Binding)

Example 13 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class ScriptHelper method endRun.

public void endRun(final Script script) {
    @SuppressWarnings("rawtypes") final Map vars = script.getBinding().getVariables();
    final Object owner = vars.get(THREAD_BINDING_OWNER);
    if (owner == script) {
        vars.remove(THREAD_BINDING_OWNER);
        Binding oldBinding = (Binding) vars.get(THREAD_BINDING_PREVIOUS);
        if (oldBinding != null) {
            THREAD_BINDING.set(oldBinding);
            vars.remove(THREAD_BINDING_PREVIOUS);
        } else {
            THREAD_BINDING.remove();
        }
    }
}
Also used : Binding(groovy.lang.Binding) GroovyObject(groovy.lang.GroovyObject) Map(java.util.Map)

Example 14 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestGroovityTags method testLog.

@Test
public void testLog() throws Exception {
    Binding binding = new Binding();
    Script logScript = groovity.load("/logTestScript", binding);
    logScript.run();
    /*
		System.out.println("--------------------");
		for(LogRecord r: logRecords){
			System.out.println(r.getLevel()+" "+r.getSourceClassName()+" "+r.getSourceMethodName()+" "+r.getMessage());
		}
		System.out.println("--------------------");
		*/
    ArrayList<LogRecord> expectedRecords = new ArrayList<>();
    LogRecord r1 = new LogRecord(Level.INFO, "INNER-STATICALLY");
    r1.setSourceClassName("/logTestScript->Melog");
    r1.setSourceMethodName("init");
    expectedRecords.add(r1);
    LogRecord r2 = new LogRecord(Level.FINE, "STATICALLY");
    r2.setSourceClassName("/logTestScript");
    r2.setSourceMethodName("init");
    expectedRecords.add(r2);
    LogRecord r3 = new LogRecord(Level.INFO, "LOADALLY");
    r3.setSourceClassName("/logTestScript");
    r3.setSourceMethodName("load");
    expectedRecords.add(r3);
    LogRecord r4 = new LogRecord(Level.WARNING, "Testing 123");
    r4.setSourceClassName("/logTestScript");
    r4.setSourceMethodName("run");
    expectedRecords.add(r4);
    LogRecord r5 = new LogRecord(Level.SEVERE, "INNER-FOO");
    r5.setSourceClassName("/logTestScript->Melog");
    r5.setSourceMethodName("foo");
    expectedRecords.add(r5);
    LogRecord r6 = new LogRecord(Level.INFO, "LOADERLY");
    r6.setSourceClassName("/logTestScript");
    r6.setSourceMethodName("run");
    expectedRecords.add(r6);
    LogRecord r7 = new LogRecord(Level.FINE, "Cannot go there");
    r7.setSourceClassName("/logTestScript->Melog");
    r7.setSourceMethodName("{44-49}");
    expectedRecords.add(r7);
    expecting: for (LogRecord expected : expectedRecords) {
        for (LogRecord logged : logRecords) {
            if (logged.getLevel().equals(expected.getLevel()) && logged.getMessage().equals(expected.getMessage()) && logged.getSourceClassName().equals(expected.getSourceClassName()) && logged.getSourceMethodName().equals(expected.getSourceMethodName())) {
                continue expecting;
            }
        }
        Assert.fail("Found no logged record matching " + expected.getLevel() + " " + expected.getSourceClassName() + " " + expected.getSourceMethodName() + " " + expected.getMessage());
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) LogRecord(java.util.logging.LogRecord) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with Binding

use of groovy.lang.Binding in project groovity by disney.

the class TestGroovityTags method testSetTag.

@Test
public void testSetTag() throws Exception {
    Binding binding = new Binding();
    Script gs = groovity.load("/set", binding);
    StringWriter out = new StringWriter();
    binding.setVariable("out", out);
    gs.run();
    String result = out.toString();
    Assert.assertEquals(81, binding.getVariable("foo"));
    Assert.assertEquals("Something 9", binding.getVariable("bar"));
    Assert.assertEquals("Foo 81 bar Something 9 abc 9 xyz", result);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) StringWriter(java.io.StringWriter) Test(org.junit.Test)

Aggregations

Binding (groovy.lang.Binding)219 GroovyShell (groovy.lang.GroovyShell)77 Script (groovy.lang.Script)58 Test (org.junit.Test)39 IOException (java.io.IOException)30 File (java.io.File)24 HashMap (java.util.HashMap)24 Closure (groovy.lang.Closure)23 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)23 Map (java.util.Map)21 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 InputStreamReader (java.io.InputStreamReader)10 StringWriter (java.io.StringWriter)10 Writer (java.io.Writer)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9