From f0949bb68e5a753ecf6527af8b5d1c9bc84a9f7c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Sun, 19 Jan 2014 17:29:27 +0000 Subject: [PATCH] Use Constants to set Json content-type and simply code --- .../gwt/query/client/plugins/ajax/Ajax.java | 17 ++++++++++++++--- .../plugins/deferred/PromiseReqBuilder.java | 8 -------- .../google/gwt/query/vm/AjaxTransportJre.java | 7 +------ .../gwt/query/servlet/GQAjaxTestServlet.java | 12 +++++++----- 4 files changed, 22 insertions(+), 22 deletions(-) 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..b22516d8 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 @@ -15,6 +15,7 @@ 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 +32,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); @@ -194,13 +199,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.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) { 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/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"); -- 2.39.5