Search in sources :

Example 1 with WebServiceScriptable

use of com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable in project servoy-client by Servoy.

the class DebugNGClient method createScriptEngine.

@Override
protected IExecutingEnviroment createScriptEngine() {
    RemoteDebugScriptEngine engine = new RemoteDebugScriptEngine(this);
    WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
    PluginScope scope = (PluginScope) engine.getSolutionScope().get("plugins", engine.getSolutionScope());
    scope.setLocked(false);
    for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
        if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
            scope.put(serviceSpecification.getScriptingName(), scope, new WebServiceScriptable(this, serviceSpecification, engine.getSolutionScope()));
        }
    }
    scope.setLocked(true);
    if (designerCallback != null) {
        designerCallback.addScriptObjects(this, engine.getSolutionScope());
    }
    return engine;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) PluginScope(com.servoy.j2db.scripting.PluginScope)

Example 2 with WebServiceScriptable

use of com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable in project servoy-client by Servoy.

the class NGClient method createScriptEngine.

@Override
protected IExecutingEnviroment createScriptEngine() {
    IExecutingEnviroment scriptEngine = super.createScriptEngine();
    WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
    PluginScope scope = (PluginScope) scriptEngine.getSolutionScope().get("plugins", scriptEngine.getSolutionScope());
    scope.setLocked(false);
    for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
        if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
            scope.put(ClientService.convertToJSName(serviceSpecification.getName()), scope, new WebServiceScriptable(this, serviceSpecification, scriptEngine.getSolutionScope()));
        }
    }
    scope.setLocked(true);
    return scriptEngine;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) PluginScope(com.servoy.j2db.scripting.PluginScope)

Example 3 with WebServiceScriptable

use of com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable in project servoy-client by Servoy.

the class NGClient method executeMethod.

/*
	 * @see org.sablo.websocket.IServerService#executeMethod(java.lang.String, org.json.JSONObject)
	 */
@Override
public Object executeMethod(String methodName, JSONObject args) throws Exception {
    switch(methodName) {
        case "login":
            try {
                credentials.setUserName(args.optString("username"));
                credentials.setPassword(args.optBoolean("encrypted") ? SecuritySupport.decrypt(Settings.getInstance(), args.optString("password")) : args.optString("password"));
                authenticate(null, null, new Object[] { credentials.getUserName(), credentials.getPassword() });
                if (getClientInfo().getUserUid() != null) {
                    wsSession.getEventDispatcher().postEvent(new Runnable() {

                        public void run() {
                            try {
                                loadSolution(getSolution().getName());
                            } catch (RepositoryException ex) {
                                Debug.error(ex);
                            }
                        }
                    });
                    if (args.optBoolean("remember")) {
                        JSONObject r = new JSONObject();
                        r.put("username", credentials.getUserName());
                        r.put("password", SecuritySupport.encrypt(Settings.getInstance(), credentials.getPassword()));
                        return r;
                    } else
                        return Boolean.TRUE;
                }
            } catch (Exception ex) {
                Debug.error(ex);
            }
            return Boolean.FALSE;
        case "autosave":
            getFoundSetManager().getEditRecordList().stopEditing(false);
            break;
        case "callServerSideApi":
            {
                String serviceScriptingName = args.getString("service");
                PluginScope scope = (PluginScope) getScriptEngine().getSolutionScope().get("plugins", getScriptEngine().getSolutionScope());
                Object service = scope.get(serviceScriptingName, scope);
                if (service instanceof WebServiceScriptable) {
                    WebServiceScriptable webServiceScriptable = (WebServiceScriptable) service;
                    JSONArray methodArguments = args.getJSONArray("args");
                    String serviceMethodName = args.getString("methodName");
                    // apply browser to sablo java value conversion - using server-side-API definition from the service's spec file if available (otherwise use default conversion)
                    // the call to webServiceScriptable.executeScopeFunction will do the java to Rhino one
                    // find spec for method
                    // get specification from plugins scope (which uses getScriptName() of service, then use the getClientService using the real name, to make sure client service is created if needed)
                    WebObjectSpecification serviceSpec = webServiceScriptable.getServiceSpecification();
                    BaseWebObject serviceWebObject = (BaseWebObject) getWebsocketSession().getClientService(serviceSpec.getName());
                    WebObjectFunctionDefinition functionSpec = (serviceSpec != null ? serviceSpec.getInternalApiFunction(serviceMethodName) : null);
                    if (functionSpec == null) {
                        functionSpec = (serviceSpec != null ? serviceSpec.getApiFunction(serviceMethodName) : null);
                    }
                    List<PropertyDescription> argumentPDs = (functionSpec != null ? functionSpec.getParameters() : null);
                    // apply conversion
                    Object[] arrayOfJavaConvertedMethodArgs = new Object[methodArguments.length()];
                    for (int i = 0; i < methodArguments.length(); i++) {
                        arrayOfJavaConvertedMethodArgs[i] = JSONUtils.fromJSON(null, methodArguments.get(i), (argumentPDs != null && argumentPDs.size() > i) ? argumentPDs.get(i) : null, new BrowserConverterContext(serviceWebObject, PushToServerEnum.allow), new ValueReference<Boolean>(false));
                    }
                    Object retVal = webServiceScriptable.executeScopeFunction(functionSpec, arrayOfJavaConvertedMethodArgs);
                    if (functionSpec != null && functionSpec.getReturnType() != null) {
                        // this means that when this return value is sent to client it will be converted to browser JSON correctly - if we give it the type
                        retVal = new TypedData<Object>(retVal, functionSpec.getReturnType());
                    }
                    return retVal;
                } else {
                    Debug.warn("callServerSideApi for unknown service '" + serviceScriptingName + "'");
                }
                break;
            }
    }
    return null;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) TypedData(org.sablo.websocket.TypedData) JSONArray(org.json.JSONArray) RepositoryException(com.servoy.j2db.persistence.RepositoryException) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) ServoyException(com.servoy.j2db.util.ServoyException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) MalformedURLException(java.net.MalformedURLException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) BaseWebObject(org.sablo.BaseWebObject) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) JSONObject(org.json.JSONObject) PluginScope(com.servoy.j2db.scripting.PluginScope) BaseWebObject(org.sablo.BaseWebObject) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext)

Aggregations

PluginScope (com.servoy.j2db.scripting.PluginScope)3 WebServiceScriptable (com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable)3 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)3 ApplicationException (com.servoy.j2db.ApplicationException)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)1 ServoyException (com.servoy.j2db.util.ServoyException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 BaseWebObject (org.sablo.BaseWebObject)1 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)1 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)1 TypedData (org.sablo.websocket.TypedData)1