From 6f093541f25b48a7e531805ea09faeff280530d0 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 6 Feb 2012 16:48:01 +0100 Subject: [PATCH] Revert "SONAR-3051 support POST, PUT and DELETE queries in GWT API." This reverts commit 808360559dc1349d59363fc89b468345e5a26fc9. --- .../main/java/org/sonar/gwt/JsonUtils.java | 137 +++++++----------- .../wsclient/gwt/AbstractSimpleCallback.java | 65 --------- .../sonar/wsclient/gwt/SimpleCallback.java | 28 ---- .../java/org/sonar/wsclient/gwt/Sonar.java | 36 +---- 4 files changed, 53 insertions(+), 213 deletions(-) delete mode 100644 sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/AbstractSimpleCallback.java delete mode 100644 sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/SimpleCallback.java diff --git a/sonar-gwt-api/src/main/java/org/sonar/gwt/JsonUtils.java b/sonar-gwt-api/src/main/java/org/sonar/gwt/JsonUtils.java index 020209e3eaa..ca64c74ca81 100644 --- a/sonar-gwt-api/src/main/java/org/sonar/gwt/JsonUtils.java +++ b/sonar-gwt-api/src/main/java/org/sonar/gwt/JsonUtils.java @@ -17,15 +17,13 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - package org.sonar.gwt; import com.google.gwt.core.client.JavaScriptException; import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.http.client.*; +import com.google.gwt.http.client.URL; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.json.client.*; -import org.sonar.wsclient.services.WSUtils; import java.util.Date; @@ -36,107 +34,70 @@ public final class JsonUtils { // only static methods } - public interface SimpleHandler extends Handler { - void onSuccess(); - } - - public interface JSONHandler extends Handler { - void onResponse(JavaScriptObject json); - } + public interface JSONHandler { + void onResponse(JavaScriptObject obj); - public interface Handler { void onTimeout(); void onError(int errorCode, String errorMessage); } - public static void request(String url, final SimpleHandler handler, RequestBuilder.Method method, String body) { - RequestBuilder requestBuilder = new RequestBuilder(method, URL.encode(formatURL(url))); - requestBuilder.setRequestData(body); - requestBuilder.setTimeoutMillis(120000); - requestBuilder.setCallback(new RequestCallback() { - public void onResponseReceived(Request request, Response response) { - if (!errorsHandled(response.getText(), handler)) { - handler.onSuccess(); - } - } - - public void onError(Request request, Throwable throwable) { - handler.onError(-1, throwable.getMessage()); - } - }); - try { - requestBuilder.send(); - } catch (RequestException e) { - handler.onError(-1, e.getMessage()); - } - } - - public static void requestJson(String url, final JSONHandler handler, RequestBuilder.Method method, String body) { - RequestBuilder requestBuilder = new RequestBuilder(method, URL.encode(formatURL(url))); - requestBuilder.setRequestData(body); - requestBuilder.setCallback(new RequestCallback() { - public void onResponseReceived(Request request, Response response) { - if (!errorsHandled(response.getText(), handler)) { - dispatchJSON(response.getText(), handler); - } - } - - public void onError(Request request, Throwable throwable) { - handler.onError(-1, throwable.getMessage()); - } - }); - requestBuilder.setTimeoutMillis(120000); - try { - requestBuilder.send(); - } catch (RequestException e) { - handler.onError(-1, e.getMessage()); - } - } - - public static void requestJson(String url, final JSONHandler handler) { - requestJson(url, handler, RequestBuilder.GET, null); - } - - private static JavaScriptObject parseJSON(String json) { - JSONValue value = JSONParser.parse(json); - if (value.isObject() != null) { - return value.isObject().getJavaScriptObject(); - } - return value.isArray().getJavaScriptObject(); - } - - private static String formatURL(String url) { + public static void requestJson(String url, JSONHandler handler) { if (!url.endsWith("&") && !url.endsWith("?")) { url += "&"; } if (!url.contains("format=json")) { url += "format=json&"; } - return url; + if (!url.contains("callback=")) { + // IMPORTANT : the url should ended with ?callback= or &callback= for JSONP calls + url += "callback="; + } + makeJSONRequest(requestId++, URL.encode(url), handler); } - public static native void log(String str) - /*-{ - console.log(str); - }-*/; - - private static boolean errorsHandled(String text, Handler handler) { - if (text != null && !text.matches("^.*$")) { - JSONObject obj = new JSONObject(parseJSON(text)); - if (obj.isObject() != null) { - if (obj.containsKey("err_code")) { - handler.onError(new Double(obj.get("err_code").isNumber().doubleValue()).intValue(), - obj.get("err_msg").isString().stringValue()); - return true; - } - } + public static native void makeJSONRequest(int requestId, String url, JSONHandler handler) + /*-{ + var callback = "callback" + requestId; + + // create SCRIPT tag, and set SRC attribute equal to JSON feed URL + callback function name + var script = document.createElement("script"); + script.setAttribute("src", url + callback); + script.setAttribute("type", "text/javascript"); + + window[callback] = function(jsonObj) { + @org.sonar.gwt.JsonUtils::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;Lorg/sonar/gwt/JsonUtils$JSONHandler;)(jsonObj, handler); + window[callback + "done"] = true; } - return false; - } - public static void dispatchJSON(String text, JSONHandler handler) { - handler.onResponse(parseJSON(text)); + setTimeout(function() { + if (!window[callback + "done"]) { + handler.@org.sonar.gwt.JsonUtils.JSONHandler::onTimeout(); + } + + // cleanup + document.body.removeChild(script); + if (window[callback]) { + delete window[callback]; + } + if (window[callback + "done"]) { + delete window[callback + "done"]; + } + }, 120000); + + document.body.appendChild(script); + }-*/; + + public static void dispatchJSON(JavaScriptObject jsonObj, JSONHandler handler) { + JSONObject obj = new JSONObject(jsonObj); + if (obj.isObject() != null) { + if (obj.containsKey("err_code")) { + handler.onError(new Double(obj.get("err_code").isNumber().doubleValue()).intValue(), + obj.get("err_msg").isString().stringValue()); + return; + } + } + handler.onResponse(jsonObj); } public static String getString(JSONObject json, String field) { diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/AbstractSimpleCallback.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/AbstractSimpleCallback.java deleted file mode 100644 index 74bbd798cd7..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/AbstractSimpleCallback.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.wsclient.gwt; - -import com.google.gwt.user.client.ui.Widget; - -public abstract class AbstractSimpleCallback implements SimpleCallback { - - private Widget loadingWidget = null; - - protected AbstractSimpleCallback(Widget loadingWidget) { - this.loadingWidget = loadingWidget; - } - - protected AbstractSimpleCallback() { - } - - public void onSuccess() { - hideLoadingWidget(); - doOnSuccess(); - } - - protected abstract void doOnSuccess(); - - public final void onTimeout() { - doOnTimeout(); - hideLoadingWidget(); - } - - public final void onError(int errorCode, String errorMessage) { - doOnError(errorCode, errorMessage); - hideLoadingWidget(); - } - - protected void doOnError(int errorCode, String errorMessage) { - - } - - protected void doOnTimeout() { - - } - - private void hideLoadingWidget() { - if (loadingWidget != null) { - loadingWidget.removeFromParent(); - } - } -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/SimpleCallback.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/SimpleCallback.java deleted file mode 100644 index abb6e88fc23..00000000000 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/SimpleCallback.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * Sonar is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.wsclient.gwt; - -public interface SimpleCallback { - void onSuccess(); - - void onTimeout(); - - void onError(int errorCode, String errorMessage); -} diff --git a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java index 046a4a6b91e..ee439203d2d 100644 --- a/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java +++ b/sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/Sonar.java @@ -17,15 +17,15 @@ * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ - package org.sonar.wsclient.gwt; import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.i18n.client.Dictionary; import com.google.gwt.json.client.JSONObject; import org.sonar.gwt.JsonUtils; -import org.sonar.wsclient.services.*; +import org.sonar.wsclient.services.Model; +import org.sonar.wsclient.services.Query; +import org.sonar.wsclient.services.WSUtils; import org.sonar.wsclient.unmarshallers.Unmarshaller; import org.sonar.wsclient.unmarshallers.Unmarshallers; @@ -89,35 +89,7 @@ public class Sonar { }); } - public void delete(final DeleteQuery query, final SimpleCallback callback) { - createUpdate(query, callback, RequestBuilder.DELETE); - } - - public void create(final CreateQuery query, final SimpleCallback callback) { - createUpdate(query, callback, RequestBuilder.POST); - } - - public void update(final UpdateQuery query, final SimpleCallback callback) { - createUpdate(query, callback, RequestBuilder.PUT); - } - - private void createUpdate(final AbstractQuery query, final SimpleCallback callback, RequestBuilder.Method method) { - JsonUtils.request(getUrl(query), new JsonUtils.SimpleHandler() { - public void onSuccess() { - callback.onSuccess(); - } - - public void onTimeout() { - callback.onTimeout(); - } - - public void onError(int errorCode, String errorMessage) { - callback.onError(errorCode, errorMessage); - } - }, method, query.getBody()); - } - - private String getUrl(AbstractQuery query) { + private String getUrl(Query query) { return host + query.getUrl(); } } -- 2.39.5