Search in sources :

Example 1 with RpcResponseHandler

use of org.rstudio.core.client.jsonrpc.RpcResponseHandler in project rstudio by rstudio.

the class RemoteServer method sendRemoteServerRequest.

// this code runs in the main workbench and implements the server request
// and then calls back the satellite on the provided js responseCallback
private void sendRemoteServerRequest(final JavaScriptObject sourceWindow, final String scope, final String method, final JavaScriptObject params, final boolean redactLog, final JavaScriptObject responseCallback) {
    // get the WindowEx from the sourceWindow
    final WindowEx srcWnd = sourceWindow.<WindowEx>cast();
    // unwrap the parameter array
    JsArrayEx array = params.cast();
    final JSONArray jsonParams = array.toJSONArray();
    // setup an rpc response handler that proxies back to the js object
    class ResponseHandler extends RpcResponseHandler {

        @Override
        public void onResponseReceived(RpcResponse response) {
            if (!srcWnd.isClosed())
                performCallback(responseCallback, response);
        }

        public void onError(RpcError error) {
            RpcResponse errorResponse = RpcResponse.create(error);
            if (!srcWnd.isClosed())
                performCallback(responseCallback, errorResponse);
        }

        private native void performCallback(JavaScriptObject responseCallback, RpcResponse response);
    }
    ;
    final ResponseHandler responseHandler = new ResponseHandler();
    // setup a retry handler which will call back the second time with
    // the same args (but no retryHandler, ensurin at most 1 retry)
    RetryHandler retryHandler = new RetryHandler() {

        public void onRetry() {
            // retry one time (passing null as last param ensures there
            // is no retry handler installed)
            sendRequest(getSourceWindowName(sourceWindow), scope, method, jsonParams, redactLog, responseHandler, null);
        }

        public void onError(RpcError error) {
            // propagate error which caused the retry to the caller
            responseHandler.onError(error);
        }
    };
    // submit request (retry same request up to one time)
    sendRequest(getSourceWindowName(sourceWindow), scope, method, jsonParams, redactLog, responseHandler, retryHandler);
}
Also used : JsArrayEx(org.rstudio.core.client.js.JsArrayEx) RpcResponseHandler(org.rstudio.core.client.jsonrpc.RpcResponseHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) RpcResponseHandler(org.rstudio.core.client.jsonrpc.RpcResponseHandler) JSONArray(com.google.gwt.json.client.JSONArray) RpcError(org.rstudio.core.client.jsonrpc.RpcError) WindowEx(org.rstudio.core.client.dom.WindowEx) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse)

Example 2 with RpcResponseHandler

use of org.rstudio.core.client.jsonrpc.RpcResponseHandler in project rstudio by rstudio.

the class RemoteServer method sendRequestViaMainWorkbench.

// call made from satellite -- this delegates to a native method which
// sets up a javascript callback and then calls the main workbench
private <T> void sendRequestViaMainWorkbench(String scope, String method, JSONArray params, boolean redactLog, final ServerRequestCallback<T> requestCallback) {
    JSONObject request = new JSONObject();
    request.put("method", new JSONString(method));
    if (params != null)
        request.put("params", params);
    final RequestLogEntry requestLogEntry = RequestLog.log(Integer.toString(Random.nextInt()), redactLog ? "[REDACTED]" : request.toString());
    sendRequestViaMainWorkbench(scope, method, params.getJavaScriptObject(), redactLog, new RpcResponseHandler() {

        @Override
        public void onResponseReceived(RpcResponse response) {
            String responseText = response.toString();
            requestLogEntry.logResponse(ResponseType.Normal, responseText);
            if (response.getError() != null) {
                RpcError error = response.getError();
                requestCallback.onError(new RemoteServerError(error));
            } else {
                T result = response.<T>getResult();
                requestCallback.onResponseReceived(result);
            }
        }
    });
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) RequestLogEntry(org.rstudio.core.client.jsonrpc.RequestLogEntry) GWT(com.google.gwt.core.client.GWT) RpcResponseHandler(org.rstudio.core.client.jsonrpc.RpcResponseHandler) RpcError(org.rstudio.core.client.jsonrpc.RpcError) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse) JSONString(com.google.gwt.json.client.JSONString)

Aggregations

RpcError (org.rstudio.core.client.jsonrpc.RpcError)2 RpcResponse (org.rstudio.core.client.jsonrpc.RpcResponse)2 RpcResponseHandler (org.rstudio.core.client.jsonrpc.RpcResponseHandler)2 GWT (com.google.gwt.core.client.GWT)1 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 JsArrayString (com.google.gwt.core.client.JsArrayString)1 JSONArray (com.google.gwt.json.client.JSONArray)1 JSONObject (com.google.gwt.json.client.JSONObject)1 JSONString (com.google.gwt.json.client.JSONString)1 WindowEx (org.rstudio.core.client.dom.WindowEx)1 JsArrayEx (org.rstudio.core.client.js.JsArrayEx)1 RequestLogEntry (org.rstudio.core.client.jsonrpc.RequestLogEntry)1