diff options
author | Manolo Carrasco <manolo@apache.org> | 2014-01-20 09:18:59 +0100 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2014-01-20 09:18:59 +0100 |
commit | 2dee8fb23ce1e6cf6ac8d227c01d3bc499fb5aeb (patch) | |
tree | a218fd1b7acc8dd2ead67a03cb26b843254419aa | |
parent | 7a5db3ca23d4db216aa705d0a9790293a672adc0 (diff) | |
parent | 1698ef35053af4ed3e22a5f9602e35ec2e8cf8e4 (diff) | |
download | gwtquery-2dee8fb23ce1e6cf6ac8d227c01d3bc499fb5aeb.tar.gz gwtquery-2dee8fb23ce1e6cf6ac8d227c01d3bc499fb5aeb.zip |
Merge branch 'mcm_jsonbuilder_jvm' of github.com:gwtquery/gwtquery into mcm_jsonbuilder_jvm
9 files changed, 62 insertions, 45 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java index 2f0e47e5..852a3c41 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Binder.java @@ -25,18 +25,18 @@ public interface Binder { /** * load a properties object. */ - <J> J load(Object prp); + <T extends Binder> T load(Object prp); /** * parses a json string and loads the resulting properties object. */ - <J> J parse(String json); + <T extends Binder> T parse(String json); /** * Returns the underlying object, normally a Properties jso in client * and a Json implementation in JVM. */ - <J> J getBound(); + <T> T getBound(); /** * Return the Object with the given key. @@ -46,7 +46,7 @@ public interface Binder { /** * Set an Object with the given key. */ - <T> T set(Object key, Object val); + <T extends Binder> T set(Object key, Object val); /** * return a list of field names. diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index 60ac6a6b..d24a7f98 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -202,7 +202,7 @@ public class Properties extends JavaScriptObject implements Binder { return c().length() == 0; } - public final <J> J load(Object prp) { + public final <J extends Binder> J load(Object prp) { c().clear(); if (prp instanceof JsCache) { c().copy((JsCache)prp); @@ -210,7 +210,7 @@ public class Properties extends JavaScriptObject implements Binder { return getBound(); } - public final <J> J parse(String json) { + public final <J extends Binder> J parse(String json) { return load(JsUtils.parseJSON(json)); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index 40dbdae7..cfa77296 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -123,7 +123,7 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J } @SuppressWarnings("unchecked") - public <T> T set(Object key, Object val) { + public <T extends Binder> T set(Object key, Object val) { if (val instanceof Binder) { p.set(key, ((Binder)val).getBound()); } else { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index 9df0f582..6591fc0a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -11,10 +11,10 @@ import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQ; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; -import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.Plugin; +import com.google.gwt.user.client.ui.FormPanel; /** * Ajax class for GQuery. @@ -31,6 +31,10 @@ import com.google.gwt.query.client.plugins.Plugin; * */ public class Ajax extends GQuery { + + public static final String JSON_CONTENT_TYPE = "application/json"; + + public static final String JSON_CONTENT_TYPE_UTF8 = JSON_CONTENT_TYPE + "; charset=utf-8"; public static interface AjaxTransport { Promise getJsonP(Settings settings); @@ -80,7 +84,7 @@ public class Ajax extends GQuery { } }); - public static Promise ajax(Properties p) { + public static Promise ajax(Binder p) { Settings s = createSettings(); s.load(p); return ajax(s); @@ -194,13 +198,19 @@ public class Ajax extends GQuery { Binder data = settings.getData(); if (data != null) { + String dataString = null, contentType = null; if (data.getBound() instanceof JavaScriptObject && JsUtils.isFormData(data.<JavaScriptObject>getBound())) { - settings.setDataString(null); + dataString = null; + contentType = FormPanel.ENCODING_URLENCODED; } else if (settings.getType().matches("(POST|PUT)") && "json".equalsIgnoreCase(settings.getDataType())) { - settings.setDataString(data.toJson()); + dataString = data.toJson(); + contentType = JSON_CONTENT_TYPE_UTF8; } else { - settings.setDataString(data.toQueryString()); + dataString = data.toQueryString(); + contentType = FormPanel.ENCODING_URLENCODED; } + settings.setDataString(dataString); + settings.setContentType(contentType); } if ("GET".equals(settings.getType()) && settings.getDataString() != null) { @@ -221,7 +231,7 @@ public class Ajax extends GQuery { return ajax(s); } - public static Promise ajax(String url, Properties p) { + public static Promise ajax(String url, Binder p) { Settings s = createSettings(); s.load(p); s.setUrl(url); @@ -249,7 +259,15 @@ public class Ajax extends GQuery { return s; } - public static Promise get(String url, Properties data, Function onSuccess) { + public static Promise get(String url) { + return get(url, null); + } + + public static Promise get(String url, Binder data) { + return get(url, (Binder)data, null); + } + + public static Promise get(String url, Binder data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); s.setDataType("txt"); @@ -259,7 +277,11 @@ public class Ajax extends GQuery { return ajax(s); } - public static Promise getJSON(String url, Properties data, Function onSuccess) { + public static Promise getJSON(String url, Binder data) { + return getJSON(url, data, null); + } + + public static Promise getJSON(String url, Binder data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); s.setDataType("json"); @@ -268,12 +290,16 @@ public class Ajax extends GQuery { s.setSuccess(onSuccess); return ajax(s); } - + public static Promise getJSONP(String url) { - return getJSONP(url, null, null); + return getJSONP(url, null); + } + + public static Promise getJSONP(String url, Binder data) { + return getJSONP(url, (Binder)data, null); } - public static Promise getJSONP(String url, Properties data, Function onSuccess) { + public static Promise getJSONP(String url, Binder data, Function onSuccess) { Settings s = createSettings(); s.setUrl(url); s.setDataType("jsonp"); @@ -325,12 +351,12 @@ public class Ajax extends GQuery { .setSuccess(success) ); } - - public static Promise post(String url, Properties data) { - return post(url, data, null); + + public static Promise post(String url, Binder data) { + return post(url, (Binder)data, null); } - - public static Promise post(String url, Properties data, final Function onSuccess) { + + public static Promise post(String url, Binder data, final Function onSuccess) { Settings s = createSettings(); s.setUrl(url); s.setDataType("txt"); @@ -344,11 +370,11 @@ public class Ajax extends GQuery { super(gq); } - public Ajax load(String url, Properties data) { + public Ajax load(String url, Binder data) { return load(url, data); } - public Ajax load(String url, Properties data, final Function onSuccess) { + public Ajax load(String url, Binder data, final Function onSuccess) { Settings s = createSettings(); final String filter = url.contains(" ") ? url.replaceFirst("^[^\\s]+\\s+", "") : ""; s.setUrl(url.replaceAll("\\s+.+$", "")); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/AjaxTransportJs.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/AjaxTransportJs.java index fe4e91b8..12ce7cda 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/AjaxTransportJs.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/AjaxTransportJs.java @@ -8,7 +8,6 @@ import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.plugins.ajax.Ajax.AjaxTransport; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; -import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl; import com.google.gwt.query.client.plugins.deferred.PromiseFunction; import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilder; import com.google.gwt.query.client.plugins.deferred.PromiseReqBuilderJSONP; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java index e6bc6d63..79d3fa8d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java @@ -27,7 +27,6 @@ import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; import com.google.gwt.query.client.plugins.deferred.Deferred.DeferredPromiseImpl; -import com.google.gwt.user.client.ui.FormPanel; import com.google.gwt.xhr.client.ReadyStateChangeHandler; import com.google.gwt.xhr.client.XMLHttpRequest; @@ -116,12 +115,6 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal } if (data != null && !isFormData && !"GET".equalsIgnoreCase(httpMethod)) { - String type = settings.getDataType(); - if (type != null && type.toLowerCase().startsWith("json")) { - ctype = "application/json; charset=utf-8"; - } else { - ctype = FormPanel.ENCODING_URLENCODED; - } xmlHttpRequest.setRequestHeader("Content-Type", ctype); } @@ -130,7 +123,6 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal JsUtils.prop(xmlHttpRequest, "withCredentials", true); final Request request = createRequestVltr(xmlHttpRequest, settings.getTimeout(), this); - System.out.println("REQ timeout " + settings.getTimeout()); xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() { public void onReadyStateChange(XMLHttpRequest xhr) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java index 0725465f..0433738b 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java @@ -129,12 +129,7 @@ public class AjaxTransportJre implements AjaxTransport { } if (s.getType().matches("POST|PUT")) { - String ctype = s.getDataType(); - if (s.getDataType().toLowerCase().startsWith("json")) { - ctype = "application/json; charset=utf-8"; - } - c.setRequestProperty("Content-Type", ctype); - + c.setRequestProperty("Content-Type", s.getContentType()); c.setDoOutput(true); DataOutputStream wr = new DataOutputStream(c.getOutputStream()); wr.writeBytes(s.getDataString()); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index d234d854..05c2bff0 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -191,6 +191,9 @@ public class JsonFactoryJre implements JsonFactory { } else if (largs == 0 || mname.startsWith("get")) { Class<?> ret = method.getReturnType(); return getValue(null, 0, jsonObject, attr, ret, method); + } else if (largs == 2 && mname.equals("set")) { + setValue(null, jsonObject, String.valueOf(args[0]), args[1], method); + return proxy; } else if (largs == 1 || mname.startsWith("set")) { setValue(null, jsonObject, attr, args[0], method); return proxy; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java b/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java index a41dd1d7..5152de4c 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java @@ -10,6 +10,8 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.google.gwt.query.client.plugins.ajax.Ajax; + public class GQAjaxTestServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -29,16 +31,14 @@ public class GQAjaxTestServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String t = req.getParameter("timeout"); if (t != null && t.matches("\\d+")) { try { int ms = Integer.parseInt(t); - System.out.println(" Sleeping: " + ms); + System.out.println(name + "sleeping: " + ms); Thread.sleep(ms); } catch (Exception e) { } - System.out.println(name + "timeout"); return; } @@ -49,11 +49,13 @@ public class GQAjaxTestServlet extends HttpServlet { data = req.getParameter("callback") + "(" + data + ");"; } } else if (req.getMethod().equalsIgnoreCase("post") - && req.getContentType().toLowerCase().startsWith("application/json")) { + && req.getContentType() != null + && req.getContentType().toLowerCase().startsWith(Ajax.JSON_CONTENT_TYPE)) { BufferedReader reader = req.getReader(); String line; - while ((line = reader.readLine()) != null) + while ((line = reader.readLine()) != null) { data += line; + } } String origin = req.getHeader("Origin"); |