use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebAppSessionListener method sessionDestroyed.
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_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.exceptions.ScriptException in project jaggery by wso2.
the class ResourceHostObject method jsGet_content.
public Object jsGet_content() throws ScriptException {
try {
Object result = this.resource.getContent();
String mediaType = this.resource.getMediaType();
if (result instanceof byte[]) {
//if mediaType is xml related one, we return an e4x xml object
if (mediaType != null) {
if (mediaType.matches(".*[\\/].*[xX][mM][lL].*")) {
return context.newObject(this, "XML", new Object[] { new String((byte[]) result) });
}
}
return new String((byte[]) result);
} else if (result instanceof String[]) {
String[] content = (String[]) result;
return context.newArray(this, Arrays.copyOf(content, content.length, Object[].class));
} else {
return Context.toObject(result, this);
}
} catch (RegistryException e) {
throw new ScriptException("Registry Exception while reading content property", e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class URIMatcherHostObject method jsFunction_match.
/**
* Match function that takes the URI template as an argument
*
* @param cx
* @param thisObj
* @param args
* @param funObj
* @return
* @throws ScriptException
*/
public static ScriptableObject jsFunction_match(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "match";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs("RhinoTopLevel", functionName, argsCount, false);
}
String template = (String) args[0];
URIMatcherHostObject uriho = (URIMatcherHostObject) thisObj;
Map<String, String> urlParts = new HashMap<String, String>();
try {
URITemplate uriTemplate = new URITemplate(template);
boolean uriMatch = uriTemplate.matches(uriho.uriToBeMatched, urlParts);
if (!uriMatch) {
return null;
}
} catch (URITemplateException e) {
throw new ScriptException(e);
}
ScriptableObject nobj = (ScriptableObject) cx.newObject(thisObj);
for (Map.Entry<String, String> entry : urlParts.entrySet()) {
nobj.put(entry.getKey(), nobj, entry.getValue());
}
uriho.uriParts = nobj;
return nobj;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RequestHostObject method parseMultipart.
private static void parseMultipart(RequestHostObject rho) throws ScriptException {
if (rho.files != null) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(rho.request);
} catch (FileUploadException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
// Process the uploaded items
String name;
rho.files = rho.context.newObject(rho);
for (Object obj : items) {
FileItem item = (FileItem) obj;
name = item.getFieldName();
if (item.isFormField()) {
ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
if (x == null) {
ArrayList<FileItem> array = new ArrayList<FileItem>(1);
array.add(item);
rho.parameterMap.put(name, array);
} else {
x.add(item);
}
} else {
rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[] { item }));
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RequestHostObject method jsFunction_getStream.
public static Object jsFunction_getStream(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getStream";
int argsCount = args.length;
if (argsCount != 0) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
RequestHostObject rho = (RequestHostObject) thisObj;
try {
return cx.newObject(thisObj, "Stream", new Object[] { rho.request.getInputStream() });
} catch (IOException e) {
String msg = "Error occurred while reading Servlet InputStream";
log.warn(msg, e);
throw new ScriptException(msg, e);
}
}
Aggregations