Search in sources :

Example 1 with RpcResponse

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

the class RemoteServerAuth method updateCredentials.

public void updateCredentials(final ServerRequestCallback<Integer> requestCallback) {
    // safely cleanup any previously active update credentials forms
    safeCleanupPreviousUpdateCredentials();
    // create a hidden form panel to submit the update credentials to
    // (we do this so GWT manages the trickiness associated with 
    // managing and reading the contents of a hidden iframe) 
    final FormPanel updateCredentialsForm = new FormPanel();
    updateCredentialsForm.setMethod(FormPanel.METHOD_GET);
    updateCredentialsForm.setEncoding(FormPanel.ENCODING_URLENCODED);
    // form url
    String url = remoteServer_.getApplicationURL("auth-update-credentials");
    updateCredentialsForm.setAction(url);
    // request log entry (fake up a json rpc method call to conform
    // to the data format expected by RequestLog
    String requestId = Integer.toString(Random.nextInt());
    String requestData = createRequestData();
    final RequestLogEntry logEntry = RequestLog.log(requestId, requestData);
    // form submit complete handler
    updateCredentialsForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {

        public void onSubmitComplete(SubmitCompleteEvent event) {
            // parse the results
            String results = event.getResults();
            RpcResponse response = RpcResponse.parse(event.getResults());
            if (response != null) {
                logEntry.logResponse(ResponseType.Normal, results);
                // check for error
                RpcError rpcError = response.getError();
                if (rpcError != null) {
                    if (rpcError.getCode() == RpcError.METHOD_NOT_FOUND) {
                        requestCallback.onResponseReceived(new Integer(CREDENTIALS_UPDATE_UNSUPPORTED));
                    } else {
                        requestCallback.onError(new RemoteServerError(rpcError));
                    }
                } else // must be a valid response
                {
                    Bool authenticated = response.getResult();
                    if (authenticated.getValue()) {
                        requestCallback.onResponseReceived(new Integer(CREDENTIALS_UPDATE_SUCCESS));
                    } else {
                        requestCallback.onResponseReceived(new Integer(CREDENTIALS_UPDATE_FAILURE));
                    }
                }
            } else // error parsing results
            {
                logEntry.logResponse(ResponseType.Error, results);
                // form message
                String msg = "Error parsing results: " + (results != null ? results : "(null)");
                // we don't expect this so debug log to flag our attention
                Debug.log("UPDATE CREDENTIALS: " + msg);
                // return the error
                RpcError rpcError = RpcError.create(RpcError.PARSE_ERROR, msg);
                requestCallback.onError(new RemoteServerError(rpcError));
            }
            // remove the hidden form (from both last-ditch list and DOM)
            previousUpdateCredentialsForms_.remove(updateCredentialsForm);
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                public void execute() {
                    RootPanel.get().remove(updateCredentialsForm);
                }
            });
        }
    });
    // add the (hidden) form panel to the document and last ditch list
    RootPanel.get().add(updateCredentialsForm, -1000, -1000);
    previousUpdateCredentialsForms_.add(updateCredentialsForm);
    // submit the form
    updateCredentialsForm.submit();
}
Also used : FormPanel(com.google.gwt.user.client.ui.FormPanel) RequestLogEntry(org.rstudio.core.client.jsonrpc.RequestLogEntry) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Bool(org.rstudio.studio.client.server.Bool) RpcError(org.rstudio.core.client.jsonrpc.RpcError) JSONString(com.google.gwt.json.client.JSONString) SubmitCompleteEvent(com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent) SubmitCompleteHandler(com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse)

Example 2 with RpcResponse

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

the class FileUploadDialog method parseResults.

@Override
protected PendingFileUpload parseResults(String results) throws Exception {
    RpcResponse response = RpcResponse.parse(results);
    if (response == null)
        throw new Exception("Unexpected response from server");
    // check for errors
    RpcError error = response.getError();
    if (error != null) {
        // select a directory
        if (error.getCode() == RpcError.PARAM_INVALID && fileUpload_.getFilename().length() == 0) {
            throw new Exception("You must specify a file to upload.");
        } else {
            throw new Exception(error.getEndUserMessage());
        }
    }
    // return PendingFileUpload
    PendingFileUpload pendingFileUpload = response.getResult();
    return pendingFileUpload;
}
Also used : PendingFileUpload(org.rstudio.studio.client.workbench.views.files.model.PendingFileUpload) RpcError(org.rstudio.core.client.jsonrpc.RpcError) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse)

Example 3 with RpcResponse

use of org.rstudio.core.client.jsonrpc.RpcResponse 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 4 with RpcResponse

use of org.rstudio.core.client.jsonrpc.RpcResponse 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)

Example 5 with RpcResponse

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

the class RemoteServer method sendRequest.

// lowest level sendRequest method -- called from the main workbench
// in two scenarios: direct internal call and servicing a proxied
// request from a satellite window
private RpcRequest sendRequest(String sourceWindow, String scope, String method, JSONArray params, boolean redactLog, final RpcResponseHandler responseHandler, final RetryHandler retryHandler) {
    // ensure we are listening for events. note that we do this here
    // because we are no longer so aggressive about retrying on failed
    // get_events calls. therefore, if we retry and fail a few times
    // we may need to restart event listening.
    ensureListeningForEvents();
    // create request
    String rserverURL = getApplicationURL(scope) + "/" + method;
    RpcRequest rpcRequest = new RpcRequest(rserverURL, method, params, null, redactLog, sourceWindow, clientId_, clientVersion_);
    if (isDisconnected())
        return rpcRequest;
    // send the request
    rpcRequest.send(new RpcRequestCallback() {

        public void onError(RpcRequest request, RpcError error) {
            // ignore errors if we are disconnected
            if (isDisconnected())
                return;
            // error and then retry
            if (resolveRpcErrorAndRetry(error, retryHandler))
                return;
            // first crack goes to globally registered rpc error handlers
            if (!handleRpcErrorInternally(error)) {
                // no global handlers processed it, send on to caller
                responseHandler.onResponseReceived(RpcResponse.create(error));
            }
        }

        public void onResponseReceived(final RpcRequest request, RpcResponse response) {
            //   - handler was cancelled
            if (isDisconnected())
                return;
            // check for error
            if (response.getError() != null) {
                // ERROR: explicit error returned by server
                RpcError error = response.getError();
                // error and then retry
                if (resolveRpcErrorAndRetry(error, retryHandler))
                    return;
                // give first crack to internal handlers, then forward to caller
                if (!handleRpcErrorInternally(error))
                    responseHandler.onResponseReceived(response);
            } else if (response.getAsyncHandle() != null) {
                serverEventListener_.registerAsyncHandle(response.getAsyncHandle(), request, this);
            } else // no error, process the result
            {
                // no error, forward to caller
                responseHandler.onResponseReceived(response);
                // to the queue by the call)
                if (eventsPending(response))
                    serverEventListener_.ensureEvents();
            }
        }
    });
    // return the request
    return rpcRequest;
}
Also used : RpcRequest(org.rstudio.core.client.jsonrpc.RpcRequest) RpcError(org.rstudio.core.client.jsonrpc.RpcError) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) RpcRequestCallback(org.rstudio.core.client.jsonrpc.RpcRequestCallback) RpcResponse(org.rstudio.core.client.jsonrpc.RpcResponse)

Aggregations

RpcError (org.rstudio.core.client.jsonrpc.RpcError)5 RpcResponse (org.rstudio.core.client.jsonrpc.RpcResponse)5 JSONString (com.google.gwt.json.client.JSONString)3 JsArrayString (com.google.gwt.core.client.JsArrayString)2 RequestLogEntry (org.rstudio.core.client.jsonrpc.RequestLogEntry)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 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 JSONArray (com.google.gwt.json.client.JSONArray)1 JSONObject (com.google.gwt.json.client.JSONObject)1 FormPanel (com.google.gwt.user.client.ui.FormPanel)1 SubmitCompleteEvent (com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent)1 SubmitCompleteHandler (com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler)1 WindowEx (org.rstudio.core.client.dom.WindowEx)1 JsArrayEx (org.rstudio.core.client.js.JsArrayEx)1 RpcRequest (org.rstudio.core.client.jsonrpc.RpcRequest)1 RpcRequestCallback (org.rstudio.core.client.jsonrpc.RpcRequestCallback)1 Bool (org.rstudio.studio.client.server.Bool)1 PendingFileUpload (org.rstudio.studio.client.workbench.views.files.model.PendingFileUpload)1