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);
}
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);
}
}
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();
}
}
}
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());
}
}
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);
}
Aggregations