.getBindings(ScriptContext.ENGINE_SCOPE);
SlingScriptHelper helper = (SlingScriptHelper) bindings
.get(SlingBindings.SLING);
if (helper == null) {
throw new ScriptException("SlingScriptHelper missing from bindings");
}
// ensure GET request
if (helper.getRequest() != null && !"GET".equals(helper.getRequest().getMethod())) {
throw new ScriptException(
"Python scripting only supports GET requests");
}
final ClassLoader oldClassLoader = Thread.currentThread()
.getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(
getClass().getClassLoader());
StringBuffer scriptString = new StringBuffer();
BufferedReader bufferedScript = new BufferedReader(script);
String nextLine = bufferedScript.readLine();
String newLine = System.getProperty("line.separator");
while (nextLine != null) {
scriptString.append(nextLine);
scriptString.append(newLine);
nextLine = bufferedScript.readLine();
}
// set writer
interp.setOut(scriptContext.getWriter());
interp.setErr(scriptContext.getErrorWriter());
// set all bindings
for (Object entryObj : bindings.entrySet()) {
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
interp.set((String) entry.getKey(), entry.getValue());
}
// execute Python code
interp.exec(scriptString.toString());
} catch (Throwable t) {
final ScriptException ex = new ScriptException(
"Failure running Python script:" + t);
ex.initCause(t);
throw ex;
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
return null;