Search in sources :

Example 66 with ScriptException

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);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) XMLObject(org.mozilla.javascript.xml.XMLObject) Callable(java.util.concurrent.Callable) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ExecutorService(java.util.concurrent.ExecutorService) XMLObject(org.mozilla.javascript.xml.XMLObject)

Example 67 with ScriptException

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");
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) Gson(com.google.gson.Gson) ServiceBuilder(org.scribe.builder.ServiceBuilder)

Example 68 with ScriptException

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

Example 69 with ScriptException

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

Example 70 with ScriptException

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

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