use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppManager method exec.
private static void exec(HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream sourceIn;
Context cx;
JaggeryContext context;
RhinoEngine engine = null;
ServletContext servletContext = request.getServletContext();
try {
engine = CommonManager.getInstance().getEngine();
cx = engine.enterContext();
String scriptPath = getScriptPath(request);
OutputStream out = response.getOutputStream();
context = createJaggeryContext(cx, out, scriptPath, request, response);
context.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext()));
if (ModuleManager.isModuleRefreshEnabled()) {
//reload init scripts
refreshServletContext(servletContext);
InputStream jaggeryConf = servletContext.getResourceAsStream(JaggeryCoreConstants.JAGGERY_CONF_FILE);
if (jaggeryConf != null) {
StringWriter writer = new StringWriter();
IOUtils.copy(jaggeryConf, writer, null);
String jsonString = writer.toString();
JSONObject conf = (JSONObject) JSONValue.parse(jsonString);
JSONArray initScripts = (JSONArray) conf.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS);
if (initScripts != null) {
JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(servletContext);
ScriptableObject sharedScope = sharedContext.getScope();
Object[] scripts = initScripts.toArray();
for (Object script : scripts) {
if (!(script instanceof String)) {
log.error("Invalid value for initScripts in jaggery.conf : " + script);
continue;
}
String path = (String) script;
path = path.startsWith("/") ? path : "/" + path;
Stack<String> callstack = CommonManager.getCallstack(sharedContext);
callstack.push(path);
engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {
@Override
protected void build() throws IOException {
try {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
} catch (ScriptException e) {
throw new IOException(e);
}
}
}, sharedScope, null);
callstack.pop();
}
}
}
}
Object serveFunction = request.getServletContext().getAttribute(SERVE_FUNCTION_JAGGERY);
if (serveFunction != null) {
Function function = (Function) serveFunction;
ScriptableObject scope = context.getScope();
function.call(cx, scope, scope, new Object[] { scope.get("request", scope), scope.get("response", scope), scope.get("session", scope) });
} else {
//resource rendering model proceeding
sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
if (sourceIn == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
return;
}
CommonManager.getInstance().getEngine().exec(new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath));
}
} catch (ScriptException e) {
String msg = e.getMessage();
log.error(msg, e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
} catch (Error e) {
//Rhino doesn't catch Error instances and it is been used to stop the script execution
//from any specific place. Hence, Error exception propagates up to Java and we silently
//ignore it, assuming it was initiated by an exit() method call.
log.debug("Script has called exit() method", e);
} finally {
// Exiting from the context
if (engine != null) {
RhinoEngine.exitContext();
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppManager method getScriptLastModified.
@SuppressFBWarnings({ "CRLF_INJECTION_LOGS", "CRLF_INJECTION_LOGS", "CRLF_INJECTION_LOGS" })
private static long getScriptLastModified(ServletContext context, String scriptPath) throws ScriptException {
long result = -1;
URLConnection uc = null;
try {
URL scriptUrl = context.getResource(canonicalURI(scriptPath));
if (scriptUrl == null) {
String msg = "Requested resource " + scriptPath + " cannot be found";
log.error(msg);
throw new ScriptException(msg);
}
uc = scriptUrl.openConnection();
if (uc instanceof JarURLConnection) {
result = ((JarURLConnection) uc).getJarEntry().getTime();
} else {
result = uc.getLastModified();
}
} catch (IOException e) {
log.warn("Error getting last modified time for " + scriptPath, e);
result = -1;
} finally {
if (uc != null) {
try {
uc.getInputStream().close();
} catch (IOException e) {
log.error("Error closing input stream for script " + scriptPath, e);
}
}
}
return result;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppManager method require.
public static ScriptableObject require(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
String functionName = "require";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
String moduleId = (String) args[0];
int dotIndex = moduleId.lastIndexOf(".");
if (moduleId.length() == dotIndex + 1) {
String msg = "Invalid file path for require method : " + moduleId;
log.error(msg);
throw new ScriptException(msg);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Map<String, ScriptableObject> requiredModules = (Map<String, ScriptableObject>) jaggeryContext.getProperty(Constants.JAGGERY_REQUIRED_MODULES);
ScriptableObject object = requiredModules.get(moduleId);
if (object != null) {
return object;
}
if (dotIndex == -1) {
object = CommonManager.require(cx, thisObj, args, funObj);
initModule(cx, jaggeryContext, moduleId, object);
} else {
object = (ScriptableObject) cx.newObject(thisObj);
object.setPrototype(thisObj);
object.setParentScope(thisObj);
//sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope()
//object.setParentScope(sharedJaggeryContext((ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT)).getScope());
String ext = moduleId.substring(dotIndex + 1);
if (ext.equalsIgnoreCase("json")) {
object = executeScript(jaggeryContext, object, moduleId, true, true, false);
} else if (ext.equalsIgnoreCase("js")) {
object = executeScript(jaggeryContext, object, moduleId, false, true, false);
} else if (ext.equalsIgnoreCase("jag")) {
object = executeScript(jaggeryContext, object, moduleId, false, false, false);
} else {
String msg = "Unsupported file type for require() method : ." + ext;
log.error(msg);
throw new ScriptException(msg);
}
}
requiredModules.put(moduleId, object);
return object;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class UploadedFile method open.
@Override
public void open(String mode) throws ScriptException {
if ("r".equals(mode)) {
readable = true;
try {
stream = fileItem.getInputStream();
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
} else {
String msg = "Invalid or unsupported file mode, path : " + path + ", mode : " + mode;
log.error(msg);
throw new ScriptException(msg);
}
opened = true;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class UploadedFile method move.
@Override
public boolean move(String dest) throws ScriptException {
if (opened) {
log.warn("Please close the file before moving");
return false;
}
try {
InputStream inputStream = fileItem.getInputStream();
FileOutputStream outputStream = new FileOutputStream(fileManager.getFile(dest));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
inputStream.close();
outputStream.flush();
outputStream.close();
return true;
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
Aggregations