aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2014-01-19 17:29:27 +0000
committerManuel Carrasco Moñino <manuel.carrasco.m@gmail.com>2014-01-19 17:29:27 +0000
commitf0949bb68e5a753ecf6527af8b5d1c9bc84a9f7c (patch)
treec1ef9bfceba960f3b1b463d5461e07a86f5714a5
parent6f8ccb8062ca975d2d75104cc4b08088944bc974 (diff)
downloadgwtquery-f0949bb68e5a753ecf6527af8b5d1c9bc84a9f7c.tar.gz
gwtquery-f0949bb68e5a753ecf6527af8b5d1c9bc84a9f7c.zip
Use Constants to set Json content-type and simply code
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java17
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java8
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java7
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java12
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.<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) {
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");