3

Task: execute groovy script with Groovy sandbox:

Groovy Script to execute:

query.reverse(); // QUERY is a some string that should be reversed

File "GroovyScriptSandbox.groovy" should get two parameters(script and values for this script):

package test.my.groovy.sandbox

import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.ImportCustomizer
import org.codehaus.groovy.control.customizers.SecureASTCustomizer
import org.springframework.stereotype.Component


@Component
class GroovyScriptSandbox {

def config
def shell


    public String runScript(final String script, final String query) {
        final ImportCustomizer imports = new ImportCustomizer()
                                                .addStarImports('groovyx.net.http')
                                                .addStaticStars('groovyx.net.http.ContentType', 'groovyx.net.http.Method')
        config = new CompilerConfiguration()
        config.addCompilationCustomizers(imports)
        def newScript = "{ query -> " + script + "}"

        shell = new GroovyShell(config)
        def clos = shell.evaluate(newScript)
        return clos.call(query)
    }
}

Java method that executes "GroovyScriptSandbox.groovy":

@Resource
private GroovyScriptSandbox groovyScriptSandbox;

@RequestMapping(value = "/run", method = RequestMethod.POST)
@ResponseBody
public String runScript(@RequestParam("script") final String script, 
                       @RequestParam("query") final String query) {
    return groovyScriptSandbox.runScript(script, query);
}

In that case all works fine:

  • Java controller getting script parameter equal "query.reverse()" and query parameter equals "0123"
  • Groovy file executes script "query.reverse()" in sandbox where query equals "0123"
  • Result equals "3210"

Question: I'm trying to replace "GroovyScriptSandbox.groovy" file with "GroovyScriptSandbox.java" and I don't know how to write the same groovy code in Java.

1 Answer 1

2

Finally found solution:

public String scriptRunner(final String script, final String query) {
    final ImportCustomizer imports = new ImportCustomizer();
    imports.addStaticStars("java.lang.Math");
    imports.addStarImports("groovyx.net.http");
    imports.addStaticStars("groovyx.net.http.ContentType", "groovyx.net.http.Method");

    final SecureASTCustomizer secure = new SecureASTCustomizer();
    secure.setClosuresAllowed(true);

    final CompilerConfiguration config = new CompilerConfiguration();
    config.addCompilationCustomizers(imports, secure); 

    final Binding intBinding = new Binding(); // alow parameters in the script
    intBinding.setVariable("query", query);

    final GroovyShell shell = new GroovyShell(intBinding, config); // create shall

    // code execution
    final Object clos = shell.evaluate(script);  

    if (clos == null) {
        return "No result avalible!";
    }
    return clos.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.