use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RhinoEngine method execFunc.
private static Object execFunc(String funcName, Object[] args, ScriptableObject thiz, ScriptableObject scope, Context cx) throws ScriptException {
Object object = scope.get(funcName, scope);
if (!(object instanceof Function)) {
String msg = "Function cannot be found with the name '" + funcName + "', but a " + object.toString();
log.error(msg);
throw new ScriptException(msg);
}
try {
return ((Function) object).call(cx, scope, thiz, args);
} catch (Exception e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class FileHostObject method loadMimeMap.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private static FileTypeMap loadMimeMap() throws ScriptException {
String configDirPath = CarbonUtils.getEtcCarbonConfigDirPath();
File configFile = new File(configDirPath, RESOURCE_MEDIA_TYPE_MAPPINGS_FILE);
if (!configFile.exists()) {
String msg = "Resource media type definitions file (mime.types) file does " + "not exist in the path " + configDirPath;
log.error(msg);
throw new ScriptException(msg);
}
final Map<String, String> mimeMappings = new HashMap<String, String>();
final String mappings;
try {
mappings = FileUtils.readFileToString(configFile, "UTF-8");
} catch (IOException e) {
String msg = "Error opening resource media type definitions file " + "(mime.types) : " + e.getMessage();
throw new ScriptException(msg, e);
}
String[] lines = mappings.split("[\\r\\n]+");
for (String line : lines) {
if (!line.startsWith("#")) {
String[] parts = line.split("\\s+");
for (int i = 1; i < parts.length; i++) {
mimeMappings.put(parts[i], parts[0]);
}
}
}
return new FileTypeMap() {
@Override
public String getContentType(File file) {
return getContentType(file.getName());
}
@Override
public String getContentType(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > 0) {
String mimeType = mimeMappings.get(fileName.substring(i + 1));
if (mimeType != null) {
return mimeType;
}
}
return "application/octet-stream";
}
};
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class JavaScriptFileImpl method close.
@Override
public void close() throws ScriptException {
if (!opened) {
return;
}
try {
file.close();
opened = false;
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class JavaScriptFileImpl method open.
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public void open(String mode) throws ScriptException {
if ("r".equals(mode)) {
try {
file = new RandomAccessFile(path, "r");
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
} else if ("r+".equals(mode)) {
try {
file = new RandomAccessFile(path, "rw");
file.seek(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else if ("w".equals(mode)) {
try {
file = new RandomAccessFile(path, "rw");
file.setLength(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
writable = true;
} else if ("w+".equals(mode)) {
try {
file = new RandomAccessFile(path, "rw");
file.setLength(0);
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else if ("a".equals(mode)) {
try {
file = new RandomAccessFile(path, "rw");
file.seek(file.length());
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
writable = true;
} else if ("a+".equals(mode)) {
try {
file = new RandomAccessFile(path, "rw");
file.seek(file.length());
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
readable = true;
writable = true;
} else {
String msg = "Invalid 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 JaggeryDeployerManager method executeScripts.
private static void executeScripts(Context context, JSONArray arr) {
if (arr != null) {
try {
JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(context.getServletContext());
CommonManager.setJaggeryContext(sharedContext);
RhinoEngine engine = sharedContext.getEngine();
org.mozilla.javascript.Context cx = engine.enterContext();
ServletContext servletContext = (ServletContext) sharedContext.getProperty(org.jaggeryjs.hostobjects.web.Constants.SERVLET_CONTEXT);
ScriptableObject sharedScope = sharedContext.getScope();
Object[] scripts = arr.toArray();
for (Object script : scripts) {
if (!(script instanceof String)) {
log.error("Invalid value for initScripts/destroyScripts in jaggery.conf : " + script);
continue;
}
String path = (String) script;
path = path.startsWith("/") ? path : "/" + path;
Stack<String> callstack = CommonManager.getCallstack(sharedContext);
callstack.push(path);
String[] parts = WebAppManager.getKeys(servletContext.getContextPath(), path, path);
ScriptCachingContext sctx = new ScriptCachingContext(sharedContext.getTenantDomain(), parts[0], parts[1], parts[2]);
sctx.setSecurityDomain(new JaggerySecurityDomain(path, servletContext));
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, sctx);
}
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
if (org.mozilla.javascript.Context.getCurrentContext() != null) {
RhinoEngine.exitContext();
}
}
}
}
Aggregations