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);
}
}
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");
}
}
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");
}
}
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);
}
}
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);
}
}
Aggregations