use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.
the class CommonManager 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(HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
}
JaggeryContext jaggeryContext = getJaggeryContext();
RhinoEngine engine = jaggeryContext.getEngine();
if (engine == null) {
log.error("Rhino Engine in Jaggery context is null");
throw new ScriptException("Rhino Engine in Jaggery context is null");
}
Stack<String> includesCallstack = getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = getIncludes(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (isHTTP(fileURL) || isHTTP(parent)) {
if (!isHTTP(fileURL)) {
fileURL = parent + fileURL;
}
if (includesCallstack.search(fileURL) != -1) {
return;
}
ScriptReader source;
ScriptableObject scope;
if (argsCount == 2) {
scope = (ScriptableObject) args[1];
} else {
scope = jaggeryContext.getScope();
}
//this is a remote file url
try {
URL url = new URL(fileURL);
url.openConnection();
source = new ScriptReader(url.openStream());
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (MalformedURLException e) {
String msg = "Malformed URL. function : import, url : " + fileURL;
log.warn(msg, e);
throw new ScriptException(msg, e);
} catch (IOException e) {
String msg = "IO exception while importing content from url : " + fileURL;
log.warn(msg, e);
throw new ScriptException(msg, e);
}
} else {
String msg = "Unsupported file include : " + fileURL;
throw new ScriptException(msg);
}
}
use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.
the class CommonManager method include_once.
public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include_once";
int argsCount = args.length;
if (argsCount != 1 && argsCount != 2) {
HostObjectUtil.invalidNumberOfArgs(HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
if (argsCount == 2 && !(args[1] instanceof ScriptableObject)) {
HostObjectUtil.invalidArgsError(HOST_OBJECT_NAME, functionName, "2", "Object", args[1], false);
}
JaggeryContext jaggeryContext = getJaggeryContext();
RhinoEngine engine = jaggeryContext.getEngine();
if (engine == null) {
log.error("Rhino Engine in Jaggery context is null");
throw new ScriptException("Rhino Engine in Jaggery context is null");
}
Stack<String> includesCallstack = getCallstack(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (isHTTP(fileURL) || isHTTP(parent)) {
if (!isHTTP(fileURL)) {
fileURL = parent + fileURL;
}
if (includesCallstack.search(fileURL) != -1) {
return;
}
Map<String, Boolean> includedScripts = getIncludes(jaggeryContext);
if (includedScripts.get(fileURL)) {
return;
}
ScriptReader source;
ScriptableObject scope;
if (argsCount == 2) {
scope = (ScriptableObject) args[1];
} else {
scope = jaggeryContext.getScope();
}
//this is a remote file url
try {
URL url = new URL(fileURL);
url.openConnection();
source = new ScriptReader(url.openStream());
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (MalformedURLException e) {
String msg = "Malformed URL. function : import, url : " + fileURL;
log.warn(msg, e);
throw new ScriptException(msg, e);
} catch (IOException e) {
String msg = "IO exception while importing content from url : " + fileURL;
log.warn(msg, e);
throw new ScriptException(msg, e);
}
} else {
String msg = "Unsupported file include : " + fileURL;
throw new ScriptException(msg);
}
}
use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.
the class WebAppSessionListener method sessionCreated.
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_CREATED_LISTENERS);
if (jsListeners == null) {
return;
}
JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
Context cx = shared.getEngine().enterContext();
JaggeryContext context = CommonManager.getJaggeryContext();
if (CommonManager.getJaggeryContext() == null) {
context = WebAppManager.clonedJaggeryContext(ctx);
CommonManager.setJaggeryContext(context);
}
RhinoEngine engine = context.getEngine();
ScriptableObject clonedScope = context.getScope();
JavaScriptProperty session = new JavaScriptProperty("session");
session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
session.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(clonedScope, session);
for (Object jsListener : jsListeners) {
CommonManager.getCallstack(context).push((String) jsListener);
try {
ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {
@Override
protected void build() throws IOException {
try {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
} catch (ScriptException e) {
throw new IOException(e);
}
}
};
engine.exec(sr, clonedScope, null);
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
CommonManager.getCallstack(context).pop();
}
}
Context.exit();
}
use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.
the class CommandLineManager method include_once.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include_once(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include_once";
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);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
CommonManager.include_once(cx, thisObj, args, funObj);
return;
}
ScriptableObject scope = jaggeryContext.getScope();
RhinoEngine engine = jaggeryContext.getEngine();
if (fileURL.startsWith("/")) {
fileURL = includesCallstack.firstElement() + fileURL;
} else {
fileURL = FilenameUtils.getFullPath(parent) + fileURL;
}
fileURL = FilenameUtils.normalize(fileURL);
if (includesCallstack.search(fileURL) != -1) {
return;
}
if (includedScripts.get(fileURL) != null) {
return;
}
try {
ScriptReader source = new ScriptReader(new FileInputStream(fileURL));
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.jaggeryjs.jaggery.core.ScriptReader in project jaggery by wso2.
the class CommandLineManager method include.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include";
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);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
CommonManager.include(cx, thisObj, args, funObj);
return;
}
ScriptableObject scope = jaggeryContext.getScope();
RhinoEngine engine = jaggeryContext.getEngine();
if (fileURL.startsWith("/")) {
fileURL = includesCallstack.firstElement() + fileURL;
} else {
fileURL = FilenameUtils.getFullPath(parent) + fileURL;
}
fileURL = FilenameUtils.normalize(fileURL);
if (includesCallstack.search(fileURL) != -1) {
return;
}
ScriptReader source = null;
try {
source = new ScriptReader(new FileInputStream(fileURL));
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
Aggregations