Search in sources :

Example 1 with RegistryException

use of org.wso2.carbon.registry.api.RegistryException 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 2 with RegistryException

use of org.wso2.carbon.registry.api.RegistryException in project jaggery by wso2.

the class RegistryHostObject method jsFunction_newResource.

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

Example 3 with RegistryException

use of org.wso2.carbon.registry.api.RegistryException 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 4 with RegistryException

use of org.wso2.carbon.registry.api.RegistryException 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 5 with RegistryException

use of org.wso2.carbon.registry.api.RegistryException 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)

Aggregations

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)10 RegistryException (org.wso2.carbon.registry.api.RegistryException)10 ResourceData (org.wso2.carbon.registry.common.ResourceData)3 Collection (org.wso2.carbon.registry.core.Collection)3 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 XMLObject (org.mozilla.javascript.xml.XMLObject)1 CarbonException (org.wso2.carbon.CarbonException)1 TagCount (org.wso2.carbon.registry.common.TagCount)1 org.wso2.carbon.registry.core (org.wso2.carbon.registry.core)1 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)1 SolrClient (org.wso2.carbon.registry.indexing.solr.SolrClient)1 AdvancedSearchResultsBean (org.wso2.carbon.registry.search.beans.AdvancedSearchResultsBean)1 CustomSearchParameterBean (org.wso2.carbon.registry.search.beans.CustomSearchParameterBean)1 UserRealm (org.wso2.carbon.user.core.UserRealm)1 UserStoreException (org.wso2.carbon.user.core.UserStoreException)1