Search in sources :

Example 36 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RegistryHostObject method jsFunction_get.

public static Scriptable jsFunction_get(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
    RegistryHostObject rho = (RegistryHostObject) thisObj;
    if (arguments.length == 1) {
        if (arguments[0] instanceof String) {
            try {
                Scriptable hostObject;
                Resource resource = rho.registry.get((String) arguments[0]);
                if (resource instanceof Collection) {
                    hostObject = cx.newObject(rho, "Collection", new Object[] { resource });
                } else {
                    hostObject = cx.newObject(rho, "Resource", new Object[] { resource });
                }
                return hostObject;
            } catch (RegistryException e) {
                throw new ScriptException("Registry error occurred while executing get() operation", e);
            }
        } else {
            throw new ScriptException("Path argument of method get() should be a string");
        }
    } else if (arguments.length == 3) {
        if (arguments[0] instanceof String && arguments[1] instanceof Number && arguments[2] instanceof Number) {
            try {
                Collection collection = rho.registry.get((String) arguments[0], ((Number) arguments[1]).intValue(), ((Number) arguments[2]).intValue());
                CollectionHostObject cho = (CollectionHostObject) cx.newObject(rho, "Collection", new Object[] { collection });
                return cho;
            } catch (RegistryException e) {
                throw new ScriptException("Registry error occurred while executing get() operation", e);
            }
        } else {
            throw new ScriptException("Invalid argument types for get() method");
        }
    } else {
        throw new ScriptException("Invalid no. of arguments for get() method");
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 37 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RegistryHostObject method jsFunction_getComments.

public static Scriptable jsFunction_getComments(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getComments";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
    }
    try {
        List<ScriptableObject> commentsArray = new ArrayList<ScriptableObject>();
        RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
        Comment[] comments = registryHostObject.registry.getComments((String) args[0]);
        for (Comment comment : comments) {
            ScriptableObject commentObj = (ScriptableObject) cx.newObject(thisObj);
            commentObj.put("cid", commentObj, comment.getCommentID());
            commentObj.put("author", commentObj, comment.getUser());
            commentObj.put("content", commentObj, comment.getText());
            commentObj.put("created", commentObj, comment.getCreatedTime().getTime());
            commentsArray.add(commentObj);
        }
        return cx.newArray(thisObj, commentsArray.toArray());
    } catch (RegistryException e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 38 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RegistryHostObject method jsFunction_getAvgRating.

public static Number jsFunction_getAvgRating(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getAvgRating";
    int argsCount = args.length;
    if (argsCount != 1) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
    }
    try {
        return registryHostObject.registry.getAverageRating((String) args[0]);
    } catch (RegistryException e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 39 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class RegistryHostObject method jsFunction_newCollection.

public static Scriptable jsFunction_newCollection(Context cx, Scriptable thisObj, Object[] arguments, Function funObj) throws ScriptException {
    RegistryHostObject rho = (RegistryHostObject) thisObj;
    if (arguments.length == 0) {
        if (rho.registry != null) {
            try {
                Collection collection = rho.registry.newCollection();
                CollectionHostObject cho = (CollectionHostObject) cx.newObject(rho, "Collection", new Object[] { collection });
                return cho;
            } catch (RegistryException e) {
                throw new ScriptException("Error occurred while creating a new Collection", e);
            }
        } else {
            throw new ScriptException("Registry has not initialized");
        }
    } else {
        throw new ScriptException("newCollection() Method doesn't accept arguments");
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 40 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class WebAppFileManager method getJavaScriptFile.

@Override
public JavaScriptFile getJavaScriptFile(Object object) throws ScriptException {
    if (object instanceof String) {
        String path = (String) object;
        if (path.startsWith(FILE_PATH)) {
            return new JavaScriptFileManagerImpl().getJavaScriptFile(path);
        }
        WebAppFile webAppFile = new WebAppFile(path, context);
        webAppFile.setFileManager(this);
        return webAppFile;
    } else if (object instanceof FileItem) {
        UploadedFile uploadedFile = new UploadedFile((FileItem) object);
        uploadedFile.setFileManager(this);
        return uploadedFile;
    } else {
        String msg = "Unsupported parameter to the File constructor : " + object.getClass();
        log.error(msg);
        throw new ScriptException(msg);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptFileManagerImpl(org.jaggeryjs.hostobjects.file.JavaScriptFileManagerImpl)

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