Search in sources :

Example 1 with ScriptException

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();
}
Also used : JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) Context(org.mozilla.javascript.Context) ServletContext(javax.servlet.ServletContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext) IOException(java.io.IOException) RhinoEngine(org.jaggeryjs.scriptengine.engine.RhinoEngine) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptProperty(org.jaggeryjs.scriptengine.engine.JavaScriptProperty) StringReader(java.io.StringReader) ServletContext(javax.servlet.ServletContext) List(java.util.List) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptReader(org.jaggeryjs.jaggery.core.ScriptReader)

Example 2 with ScriptException

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);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) XMLObject(org.mozilla.javascript.xml.XMLObject) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 3 with ScriptException

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;
}
Also used : URITemplateException(org.wso2.uri.template.URITemplateException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) HashMap(java.util.HashMap) URITemplate(org.wso2.uri.template.URITemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with ScriptException

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 }));
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 5 with ScriptException

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);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) IOException(java.io.IOException)

Aggregations

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)83 IOException (java.io.IOException)15 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)9 URL (java.net.URL)8 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)8 MalformedURLException (java.net.MalformedURLException)7 ServletContext (javax.servlet.ServletContext)6 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)6 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 Context (org.mozilla.javascript.Context)5 File (java.io.File)4 StringReader (java.io.StringReader)4 Callable (java.util.concurrent.Callable)4 ExecutorService (java.util.concurrent.ExecutorService)4 FileItem (org.apache.commons.fileupload.FileItem)4 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)4 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)4