use of org.jaggeryjs.scriptengine.engine.JaggeryContext 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.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class FileHostObject method jsConstructor.
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
}
FileHostObject fho = new FileHostObject();
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
if (obj instanceof JavaScriptFileManager) {
fho.manager = (JavaScriptFileManager) obj;
} else {
fho.manager = new JavaScriptFileManagerImpl();
}
fho.file = fho.manager.getJavaScriptFile(args[0]);
fho.file.construct();
fho.context = cx;
return fho;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class FileHostObject method jsFunction_unZip.
/**
* To unzip a zip file
*
* @param cx Context
* @param thisObj FileHostObject to be unzipped
* @param args Path to unzip the zip file
* @param funObj Function Object
* @throws ScriptException
*/
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
public static boolean jsFunction_unZip(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
String functionName = "unZip";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
FileHostObject fho = (FileHostObject) thisObj;
ZipInputStream zin = null;
BufferedOutputStream out = null;
if (fho.file.isExist()) {
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
if (obj instanceof JavaScriptFileManager) {
fho.manager = (JavaScriptFileManager) obj;
} else {
fho.manager = new JavaScriptFileManagerImpl();
}
File zipfile = new File(fho.manager.getFile(fho.file.getPath()).getAbsolutePath());
File outdir = new File(fho.manager.getDirectoryPath(args[0].toString()));
if (outdir.getParentFile().exists() || outdir.getParentFile().mkdirs()) {
if (outdir.exists() || outdir.mkdir()) {
try {
zin = new ZipInputStream(new FileInputStream(zipfile));
ZipEntry entry;
String name, dir;
byte[] buffer = new byte[1024];
while ((entry = zin.getNextEntry()) != null) {
name = entry.getName();
if (entry.isDirectory()) {
mkdirs(outdir, name);
continue;
}
int hasParentDirs = name.lastIndexOf(File.separatorChar);
dir = (hasParentDirs == -1) ? null : name.substring(0, hasParentDirs);
if (dir != null) {
mkdirs(outdir, dir);
}
try {
out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
int count;
while ((count = zin.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
} catch (Exception ex) {
log.error("Unable to perform unZip operation for file : " + fho.file.getName(), ex);
return false;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException er) {
log.error("Unable to close the output stream " + er);
}
}
}
}
return true;
} catch (IOException ex) {
log.error("Cannot unzip the file " + ex);
throw new IOException(ex);
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException er) {
log.error("Unable to close the zip input stream " + er);
}
}
}
} else {
log.error("Unable to create directories to handle file : " + fho.file.getName());
}
} else {
log.error("Unable to create directories to handle file : " + fho.file.getName());
}
} else {
log.error("Zip file not exists");
}
return false;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class FileHostObject method jsFunction_zip.
/**
* To zip a folder
*
* @param cx Context
* @param thisObj FileHostObject
* @param args Zip file path to zip the folder
* @param funObj Function
* @throws ScriptException
*/
@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_OUT", "PATH_TRAVERSAL_IN" })
public static boolean jsFunction_zip(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException, IOException {
String functionName = "zip";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
FileHostObject fho = (FileHostObject) thisObj;
ZipOutputStream zip = null;
if (fho.file.isExist()) {
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
Object obj = context.getProperty(JAVASCRIPT_FILE_MANAGER);
if (obj instanceof JavaScriptFileManager) {
fho.manager = (JavaScriptFileManager) obj;
} else {
fho.manager = new JavaScriptFileManagerImpl();
}
String destinationPath = fho.manager.getFile(args[0].toString()).getAbsolutePath();
String sourcePath = fho.manager.getDirectoryPath(fho.file.getPath());
File destinationFile = new File(destinationPath);
if (destinationFile.getParentFile().exists() || destinationFile.getParentFile().mkdirs()) {
try {
zip = new ZipOutputStream(new FileOutputStream(destinationPath));
File folder = new File(sourcePath);
if (folder.list() != null) {
for (String fileName : folder.list()) {
addFileToZip("", sourcePath + File.separator + fileName, zip);
}
}
return true;
} catch (IOException ex) {
log.error("Cannot zip the folder. " + ex);
throw new IOException(ex);
} finally {
if (zip != null) {
try {
zip.flush();
zip.close();
} catch (IOException er) {
log.error("Unable to close the zip output stream " + er);
}
}
}
} else {
log.error("Unable to create the directory path for file : " + fho.file.getName());
}
} else {
log.error("Zip operation cannot be done. Folder not found");
}
return false;
}
use of org.jaggeryjs.scriptengine.engine.JaggeryContext in project jaggery by wso2.
the class RequestHostObject method jsFunction_getMappedPath.
public static String jsFunction_getMappedPath(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getMappedPath";
int argsCount = args.length;
if (argsCount != 0) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
RequestHostObject rho = (RequestHostObject) thisObj;
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
Stack stack = (Stack) context.getProperty(LogHostObject.JAGGERY_INCLUDES_CALLSTACK);
String path = (String) stack.firstElement();
if (rho.request.getRequestURI().equals(path)) {
return null;
}
return path;
}
Aggregations