use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class ModuleManager method initModule.
private void initModule(InputStream modulesXML, boolean isCustom) throws ScriptException {
try {
Context cx = RhinoEngine.enterGlobalContext();
JAXBContext jaxbContext = JAXBContext.newInstance(Module.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Module moduleObject = (Module) jaxbUnmarshaller.unmarshal(modulesXML);
String moduleName = moduleObject.getName();
if (modules.get(moduleName) != null) {
log.info("A module with the name : " + moduleName + " already exists and it will be overwritten.");
}
String namespace = moduleObject.getNamespace();
boolean expose = (Boolean.parseBoolean(moduleObject.getExpose()) == true);
JavaScriptModule module = new JavaScriptModule(moduleName);
module.setNamespace(namespace);
module.setExpose(expose);
initHostObjects(moduleObject, module);
initMethods(moduleObject, module);
initScripts(moduleObject, cx, module, isCustom);
modules.put(moduleName, module);
} catch (JAXBException e) {
String msg = "Error while reading the module.xml";
log.error(msg, e);
throw new ScriptException(msg, e);
} finally {
RhinoEngine.exitContext();
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class ModuleManager method initScripts.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private void initScripts(Module moduleObject, Context cx, JavaScriptModule module, boolean isCustom) throws ScriptException {
String name = null;
String path = null;
JavaScriptScript script;
List scriptList = moduleObject.getScripts();
Iterator itr = scriptList.iterator();
while (itr.hasNext()) {
try {
//process methods
org.jaggeryjs.jaggery.core.Script scriptObject = (org.jaggeryjs.jaggery.core.Script) itr.next();
name = scriptObject.getName();
path = scriptObject.getPath();
script = new JavaScriptScript(name);
Reader reader;
final String fileName;
ScriptCachingContext sctx;
if (isCustom) {
String filteredPath = filterPath(path);
fileName = modulesDir + File.separator + module.getName() + File.separator + filterPath(path);
reader = new FileReader(fileName);
int endIndex = filteredPath.lastIndexOf(File.separator);
sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), '<' + module.getName() + '>', filteredPath.substring(0, endIndex), filteredPath.substring(endIndex));
} else {
reader = new InputStreamReader(ModuleManager.class.getClassLoader().getResourceAsStream(path));
fileName = modulesDir + File.separator + name;
int endIndex = path.lastIndexOf('/');
sctx = new ScriptCachingContext(String.valueOf(MultitenantConstants.SUPER_TENANT_ID), "<<" + name + ">>", '/' + path.substring(0, endIndex), path.substring(endIndex));
}
CacheManager cacheManager = new CacheManager(null);
sctx.setSecurityDomain(new RhinoSecurityDomain() {
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
@Override
public CodeSource getCodeSource() throws ScriptException {
try {
URL url = new File(fileName).getCanonicalFile().toURI().toURL();
return new CodeSource(url, (Certificate[]) null);
} catch (MalformedURLException e) {
throw new ScriptException(e);
} catch (IOException e) {
throw new ScriptException(e);
}
}
});
sctx.setSourceModifiedTime(1);
Script cachedScript = cacheManager.getScriptObject(reader, sctx);
if (cachedScript == null) {
cacheManager.cacheScript(reader, sctx);
cachedScript = cacheManager.getScriptObject(reader, sctx);
}
script.setScript(cachedScript);
module.addScript(script);
} catch (FileNotFoundException e) {
String msg = "Error executing script. Script cannot be found, name : " + name + ", path : " + path;
log.error(msg, e);
throw new ScriptException(msg, e);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppManager method executeScript.
private static ScriptableObject executeScript(JaggeryContext jaggeryContext, ScriptableObject scope, String fileURL, final boolean isJSON, boolean isBuilt, boolean isIncludeOnce) throws ScriptException {
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT);
String parent = includesCallstack.lastElement();
String[] keys = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
fileURL = getNormalizedScriptPath(keys);
if (includesCallstack.search(fileURL) != -1) {
return scope;
}
if (isIncludeOnce && includedScripts.get(fileURL) != null) {
return scope;
}
ScriptReader source;
RhinoEngine engine = jaggeryContext.getEngine();
if (isBuilt) {
source = new ScriptReader(context.getResourceAsStream(fileURL)) {
@Override
protected void build() throws IOException {
try {
if (isJSON) {
sourceReader = new StringReader("(" + HostObjectUtil.streamToString(sourceIn) + ")");
} else {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
}
} catch (ScriptException e) {
throw new IOException(e);
}
}
};
} else {
source = new ScriptReader(context.getResourceAsStream(fileURL));
}
ScriptCachingContext sctx = new ScriptCachingContext(jaggeryContext.getTenantDomain(), keys[0], keys[1], keys[2]);
sctx.setSecurityDomain(new JaggerySecurityDomain(fileURL, context));
long lastModified = WebAppManager.getScriptLastModified(context, fileURL);
sctx.setSourceModifiedTime(lastModified);
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
if (isJSON) {
scope = (ScriptableObject) engine.eval(source, scope, sctx);
} else {
engine.exec(source, scope, sctx);
}
includesCallstack.pop();
return scope;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException 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.scriptengine.exceptions.ScriptException 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);
}
}
Aggregations