use of org.jaggeryjs.scriptengine.engine.JaggeryContext 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.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method include.
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include";
int argsCount = args.length;
if (argsCount != 1 && argsCount != 2) {
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);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
CommonManager.include(cx, thisObj, args, funObj);
return;
}
if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
}
ScriptableObject scope;
if (argsCount == 2) {
scope = (ScriptableObject) args[1];
} else {
scope = jaggeryContext.getScope();
}
executeScript(jaggeryContext, scope, fileURL, false, false, false);
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method setInterval.
public static String setInterval(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
JaggeryContext context = CommonManager.getJaggeryContext();
ServletContext servletContext = (ServletContext) context.getProperty(Constants.SERVLET_CONTEXT);
String contextPath = servletContext.getContextPath();
String taskId = RhinoTopLevel.setInterval(cx, thisObj, args, funObj);
List<String> taskIds = intervals.get(contextPath);
if (taskIds == null) {
taskIds = new ArrayList<String>();
intervals.put(contextPath, taskIds);
}
taskIds.add(taskId);
return taskId;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method print.
/**
* JaggeryMethod responsible of writing to the output stream
*/
public static void print(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
if (!isWebSocket) {
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
//If the script itself havent set the content type we set the default content type to be text/html
HttpServletResponse servletResponse = (HttpServletResponse) jaggeryContext.getProperty(SERVLET_RESPONSE);
//a sessionDestroyedListeners
if (servletResponse != null) {
if (servletResponse.getContentType() == null) {
servletResponse.setContentType(DEFAULT_CONTENT_TYPE);
}
if (servletResponse.getCharacterEncoding() == null) {
servletResponse.setCharacterEncoding(DEFAULT_CHAR_ENCODING);
}
CommonManager.print(cx, thisObj, args, funObj);
}
}
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class WebAppManager method getScriptCachingContext.
protected static ScriptCachingContext getScriptCachingContext(HttpServletRequest request, String scriptPath) throws ScriptException {
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
String tenantId = jaggeryContext.getTenantDomain();
String[] parts = getKeys(request.getContextPath(), scriptPath, scriptPath);
/**
* tenantId = tenantId
* context = webapp context
* path = relative path to the directory of *.js file
* cacheKey = name of the *.js file being cached
*/
ScriptCachingContext sctx = new ScriptCachingContext(tenantId, parts[0], parts[1], parts[2]);
ServletContext servletContext = request.getServletContext();
sctx.setSecurityDomain(new JaggerySecurityDomain(getNormalizedScriptPath(parts), servletContext));
long lastModified = getScriptLastModified(servletContext, scriptPath);
sctx.setSourceModifiedTime(lastModified);
return sctx;
}
Aggregations