use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class XSLTHostObject method jsFunction_transform.
/**
* transform(xml)
* transform(xml, callback)
* transform(xml, paramMap)
* transform(xml, paramMap, callback)
* trasformer(xml, callback, uriResolver)
* trasformer(xml, paramMap, callback, uriResolver)
*/
public static String jsFunction_transform(final Context cx, final Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "transform";
int argsCount = args.length;
if (argsCount > 4) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
StringReader xml = null;
Function uriResolver = null;
NativeObject paramMap = null;
Function callback = null;
if (args[0] instanceof String) {
xml = new StringReader((String) args[0]);
} else if (args[0] instanceof XMLObject) {
xml = new StringReader(args[0].toString());
} else {
HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string | xml", args[0], true);
}
if (argsCount == 2) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
} else if (args[1] instanceof Function) {
callback = (Function) args[1];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object | function", args[1], false);
}
} else if (argsCount == 3) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
if (args[2] instanceof Function) {
callback = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
} else if (args[1] instanceof Function) {
callback = (Function) args[1];
if (args[2] instanceof Function) {
uriResolver = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object", args[1], false);
}
} else if (argsCount == 4) {
if (args[1] instanceof NativeObject) {
paramMap = (NativeObject) args[1];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "object", args[1], false);
}
if (args[2] instanceof Function) {
callback = (Function) args[2];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "3", "function", args[2], false);
}
if (args[3] instanceof Function) {
uriResolver = (Function) args[3];
} else {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "4", "function", args[3], false);
}
}
final XSLTHostObject xho = (XSLTHostObject) thisObj;
try {
final StringWriter result = new StringWriter();
if (callback == null) {
if (xho.transformer == null) {
xho.transformer = getTransformer(cx, thisObj, xho, paramMap, uriResolver);
} else {
if (paramMap != null) {
setParams(xho.transformer, paramMap);
}
if (uriResolver != null) {
xho.transformer.setURIResolver(getUriResolver(cx, funObj, uriResolver));
}
}
xho.transformer.transform(new StreamSource(xml), new StreamResult(result));
return result.toString();
}
final StringReader finalXml = xml;
final Function finalCallback = callback;
final Function finalUriResolver = uriResolver;
final NativeObject finalParamMap = paramMap;
final ContextFactory factory = cx.getFactory();
final ExecutorService es = Executors.newCachedThreadPool();
es.submit(new Callable() {
public Object call() throws Exception {
Context ctx = RhinoEngine.enterContext(factory);
try {
getTransformer(ctx, thisObj, xho, finalParamMap, finalUriResolver).transform(new StreamSource(finalXml), new StreamResult(result));
finalCallback.call(ctx, xho, xho, new Object[] { result.toString() });
} catch (TransformerException e) {
log.warn(e);
} finally {
es.shutdown();
RhinoEngine.exitContext();
}
return null;
}
});
return null;
} catch (TransformerException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class OAuthHostObject method jsConstructor.
/**
* var provider = {
* "oauth_version" : "1",
* "authorization_url" : "https://www.linkedin.com/uas/oauth/authorize",
* "access_token_url" : "https://api.linkedin.com/uas/oauth/accessToken",
* "request_token_url" : "https://api.linkedin.com/uas/oauth/requestToken",
* "api_key" : "key",
* "api_secret" : "secret"
* }
* new OAuthProvider(provider);
*/
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
OAuthHostObject oauthho = new OAuthHostObject();
if (args.length == 1) {
if (!(args[0] == Context.getUndefinedValue()) && args[0] instanceof NativeObject) {
NativeObject config = (NativeObject) args[0];
Gson gson = new Gson();
ProviderConfig providerConfig = gson.fromJson(HostObjectUtil.serializeJSON(config), ProviderConfig.class);
if (providerConfig.getApi_key() == null || providerConfig.getApi_secret() == null || providerConfig.getAccess_token_url() == null || providerConfig.getAuthorization_url() == null || providerConfig.getOAuth_version() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.apiKey = providerConfig.getApi_key();
oauthho.apiSecret = providerConfig.getApi_secret();
if (providerConfig.getOAuth_version() == 1.0) {
if (providerConfig.getRequest_token_url() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.oAuthVersion = OAuthVersion.OAUTH1;
GenericOAuth10aApi oauth10aApi = new GenericOAuth10aApi();
oauth10aApi.setAccessTokenEndpoint(providerConfig.getAccess_token_url());
oauth10aApi.setAuthorizationUrl(providerConfig.getAuthorization_url());
oauth10aApi.setRequestTokenEndpoint(providerConfig.getRequest_token_url());
oauthho.oauthService = new ServiceBuilder().provider(oauth10aApi).apiKey(oauthho.apiKey).apiSecret(oauthho.apiSecret).build();
} else if (providerConfig.getOAuth_version() == 2.0) {
if (providerConfig.getCallback_url() == null) {
throw new ScriptException("API configuration not specified");
}
oauthho.oAuthVersion = OAuthVersion.OAUTH2;
GenericOAuth20Api oauth20Api = new GenericOAuth20Api();
oauth20Api.setAccessTokenEP(providerConfig.getAccess_token_url());
oauth20Api.setAuthorizeUrl(providerConfig.getAuthorization_url());
oauthho.oauthService = new ServiceBuilder().provider(oauth20Api).apiKey(oauthho.apiKey).apiSecret(oauthho.apiSecret).build();
}
}
return oauthho;
} else {
throw new ScriptException("API configuration not specified");
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_getRating.
public static Number jsFunction_getRating(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "getRating";
int argsCount = args.length;
if (argsCount != 2) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
if (!(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "string", args[1], false);
}
try {
return registryHostObject.registry.getRating((String) args[0], (String) args[1]);
} catch (RegistryException e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_addComment.
public static void jsFunction_addComment(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "addComment";
int argsCount = args.length;
if (argsCount != 2) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
if (!(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "string", args[1], false);
}
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
try {
registryHostObject.registry.addComment((String) args[0], new org.wso2.carbon.registry.core.Comment((String) args[1]));
} catch (RegistryException e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RegistryHostObject method jsFunction_addRating.
public static void jsFunction_addRating(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "addRating";
int argsCount = args.length;
if (argsCount != 2) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
}
if (!(args[1] instanceof Number)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "number", args[1], false);
}
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
try {
registryHostObject.registry.rateResource((String) args[0], ((Number) args[1]).intValue());
} catch (RegistryException e) {
throw new ScriptException(e);
}
}
Aggregations