Search in sources :

Example 21 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method newDocument.

public void newDocument(String filetype, String contents, JsObject properties, ServerRequestCallback<SourceDocument> requestCallback) {
    JSONArray params = new JSONArray();
    params.set(0, new JSONString(filetype));
    params.set(1, contents != null ? new JSONString(contents) : JSONNull.getInstance());
    params.set(2, new JSONObject(properties));
    sendRequest(RPC_SCOPE, NEW_DOCUMENT, params, requestCallback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray) JSONString(com.google.gwt.json.client.JSONString)

Example 22 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method connectionExecuteAction.

@Override
public void connectionExecuteAction(ConnectionId connection, String action, ServerRequestCallback<Void> callback) {
    JSONArray params = new JSONArray();
    params.set(0, new JSONObject(connection));
    params.set(1, new JSONString(action));
    sendRequest(RPC_SCOPE, CONNECTION_EXECUTE_ACTION, params, callback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray) JSONString(com.google.gwt.json.client.JSONString)

Example 23 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method sendRequest.

private <T> void sendRequest(String scope, String method, JavaScriptObject param, ServerRequestCallback<T> requestCallback) {
    JSONArray params = new JSONArray();
    // pass JSONNull if the object is null
    params.set(0, param != null ? new JSONObject(param) : JSONNull.getInstance());
    sendRequest(scope, method, params, requestCallback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray)

Example 24 with JSONObject

use of com.google.gwt.json.client.JSONObject in project rstudio by rstudio.

the class RemoteServer method updateBreakpoints.

@Override
public void updateBreakpoints(ArrayList<Breakpoint> breakpoints, boolean set, boolean arm, ServerRequestCallback<Void> requestCallback) {
    JSONArray bps = new JSONArray();
    for (int i = 0; i < breakpoints.size(); i++) {
        bps.set(i, new JSONObject(breakpoints.get(i)));
    }
    JSONArray params = new JSONArray();
    params.set(0, bps);
    params.set(1, JSONBoolean.getInstance(set));
    params.set(2, JSONBoolean.getInstance(arm));
    sendRequest(RPC_SCOPE, UPDATE_BREAKPOINTS, params, requestCallback);
}
Also used : JSONObject(com.google.gwt.json.client.JSONObject) JSONArray(com.google.gwt.json.client.JSONArray) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint) Point(org.rstudio.studio.client.workbench.views.plots.model.Point)

Example 25 with JSONObject

use of com.google.gwt.json.client.JSONObject in project perun by CESNET.

the class JsonClient method retrieveData.

/**
	 * Sends the data
	 */
public void retrieveData(String url, String params, final JsonCallback callback) {
    // create events
    if (callback != null) {
        this.events = new JsonCallbackEvents() {

            @Override
            public void onFinished(JavaScriptObject jso) {
                callback.onFinished(jso);
            }

            @Override
            public void onError(PerunError error) {
                callback.onError(error);
            }

            @Override
            public void onLoadingStart() {
                callback.onLoadingStart();
            }
        };
    }
    final PerunRequest perunRequest = new JSONObject().getJavaScriptObject().cast();
    perunRequest.setStartTime();
    // url to call
    final String requestUrl = URL.encode(PerunWebSession.getInstance().getRpcUrl() + url + "?callback=" + perunRequest.getStartTime() + "&" + params);
    perunRequest.setManager(url.split("\\/")[0]);
    perunRequest.setMethod(url.split("\\/")[1]);
    perunRequest.setParamString("?callback=" + perunRequest.getStartTime() + "&" + params);
    // request building
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, requestUrl);
    try {
        // sends the request
        onRequestLoadingStart();
        if (isCacheEnabled) {
            if (cache.get(requestUrl) != null) {
                // jso
                JavaScriptObject jso = cache.get(requestUrl);
                // Return DATA if not error, otherwise start new call
                PerunError error = (PerunError) jso;
                if ("".equalsIgnoreCase(error.getErrorId()) && "".equalsIgnoreCase(error.getErrorInfo())) {
                    // not error, OK
                    if (session.getUiElements() != null) {
                        session.getUiElements().setLogText("Response not NULL, not ERROR.");
                    }
                    onRequestFinished(jso);
                    return;
                }
            }
        }
        final Request request = builder.sendRequest("", new RequestCallback() {

            @Override
            public void onResponseReceived(Request req, Response resp) {
                // if response = OK
                if (resp.getStatusCode() == 200) {
                    // jso
                    JavaScriptObject jso = parseResponse(perunRequest.getStartTime() + "", resp.getText());
                    // if null - finished
                    if (jso == null) {
                        if (session.getUiElements() != null) {
                            session.getUiElements().setLogText("Response NULL.");
                        }
                        runningRequests.remove(requestUrl);
                        onRequestFinished(null);
                        return;
                    }
                    // if error?
                    PerunError error = (PerunError) jso;
                    if ("".equalsIgnoreCase(error.getErrorId()) && "".equalsIgnoreCase(error.getErrorInfo())) {
                        // not error, OK
                        if (session.getUiElements() != null) {
                            session.getUiElements().setLogText("Response not NULL, not ERROR.");
                        }
                        runningRequests.remove(requestUrl);
                        onRequestFinished(jso);
                        if (isCacheEnabled) {
                            cache.put(requestUrl, jso);
                        }
                        return;
                    }
                    // triggers onError
                    if (session.getUiElements() != null) {
                        session.getUiElements().setLogText("Response ERROR.");
                    }
                    error.setRequest(perunRequest);
                    error.setRequestURL(requestUrl);
                    error.setPostData("");
                    onRequestError(error);
                    runningRequests.remove(requestUrl);
                    return;
                } else {
                    // if response not OK
                    PerunError error = new JSONObject().getJavaScriptObject().cast();
                    error.setErrorId("" + resp.getStatusCode());
                    error.setName(resp.getStatusText());
                    error.setErrorInfo("Server responded with HTTP error: " + resp.getStatusCode() + " - " + resp.getStatusText());
                    error.setObjectType("PerunError");
                    error.setPostData("");
                    error.setRequestURL(requestUrl);
                    error.setRequest(perunRequest);
                    if (resp.getStatusCode() == 401 || resp.getStatusCode() == 403) {
                        error.setErrorInfo("You are not authorized to server. Your session might have expired. Please refresh the browser window to re-login.");
                    } else if (resp.getStatusCode() == 500) {
                        if (runningRequests.get(requestUrl) != null) {
                            if ((runningRequests.get(requestUrl).getDuration() / (1000 * 60)) >= 5) {
                                // 5 minute timeout
                                error.setName("Request Timeout");
                                error.setErrorInfo("Your request couldn't be handled within 5 minutes. Please refresh your view (table) to try it again.");
                            } else {
                                error.setName("Server Internal Error");
                                error.setErrorInfo("Server encounter internal error while processing your request. Please report this error and retry.");
                            }
                        }
                    } else if (resp.getStatusCode() == 503) {
                        error.setName("Server Temporarily Unavailable");
                        error.setErrorInfo("Server is temporarily unavailable. Please try again later.");
                    } else if (resp.getStatusCode() == 404) {
                        error.setName("Not found");
                        error.setErrorInfo("Server is probably being restarted at the moment. Please try again later.");
                    } else if (resp.getStatusCode() == 0) {
                        error.setName("Can't contact remote server.");
                        error.setErrorInfo("You are either without network connection or your session has expired. Click \"OK\" to reload the page (all unsaved changes will be lost).");
                        // force reload page if it's first GUI call, otherwise keep it to alert box
                        if (!hidden && runningRequests.get(requestUrl).getManager().equals("authzResolver") && runningRequests.get(requestUrl).getMethod().equals("getPerunPrincipal")) {
                            Window.Location.reload();
                        }
                    }
                    runningRequests.remove(requestUrl);
                    onRequestError(error);
                    return;
                }
            }

            @Override
            public void onError(Request req, Throwable exc) {
                // request not sent
                runningRequests.remove(requestUrl);
                onRequestError(parseResponse(perunRequest.getStartTime() + "", exc.toString()));
            }
        });
        runningRequests.put(requestUrl, perunRequest);
    } catch (RequestException exc) {
        // usually couldn't connect to server
        onRequestError(parseResponse(perunRequest.getStartTime() + "", exc.toString()));
    }
}
Also used : PerunRequest(cz.metacentrum.perun.webgui.model.PerunRequest) JSONObject(com.google.gwt.json.client.JSONObject) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError) PerunRequest(cz.metacentrum.perun.webgui.model.PerunRequest)

Aggregations

JSONObject (com.google.gwt.json.client.JSONObject)227 JSONNumber (com.google.gwt.json.client.JSONNumber)109 JSONString (com.google.gwt.json.client.JSONString)74 JSONArray (com.google.gwt.json.client.JSONArray)62 PerunError (cz.metacentrum.perun.webgui.model.PerunError)55 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)54 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)54 JsonPostClient (cz.metacentrum.perun.webgui.json.JsonPostClient)50 JSONValue (com.google.gwt.json.client.JSONValue)13 HashMap (java.util.HashMap)12 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 JsArrayString (com.google.gwt.core.client.JsArrayString)3 User (cz.metacentrum.perun.webgui.model.User)3 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)2 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)2 Command (com.google.gwt.user.client.Command)2 Contact (com.googlecode.gwtphonegap.client.contacts.Contact)2 ContactField (com.googlecode.gwtphonegap.client.contacts.ContactField)2 StringCallback (com.gwtmobile.phonegap.client.plugins.Bluetooth.StringCallback)2