use parseStrict instead of parseLenient in URLFieldWithFileUpload; fixed <pre> wrapping by setting content type to text/html instead of application/json

Change-Id: I37852c582333b787e3a9ff8dfeef59e5703c1442
This commit is contained in:
Axel Uhl committed 2016-01-08 11:23:32 +01:00
1 parent 8246a64a3f
commit bfd33b20cd
4 files changed
+22 -4

No files matched your search

@@ -1,4 +1,5 @@
eclipse.preferences.version=1
encoding//src/com/sap/sailing/domain/common/AbstractBearing.java=UTF-8
encoding//src/com/sap/sailing/domain/common/confidence/impl/ConfidenceBasedAveragerImpl.java=utf-8
encoding//src/com/sap/sailing/domain/common/impl/AbstractSpeedWithAbstractBearingImpl.java=UTF-8
encoding//src/com/sap/sailing/domain/common/tracking/impl/CompactGPSFixMovingImpl.java=UTF-8
@@ -76,8 +76,12 @@ public class FileUploadServlet extends AbstractFileUploadServlet {
}
resultList.add(result);
}
resp.setContentType("application/json");
// surprise, surprise: see https://www.sencha.com/forum/showthread.php?132949-Fileupload-Invalid-JSON-string
// When sending a JSON response for a file upload, don't use application/json as the content type. It would lead
// to wrapping the content by a <pre> tag. Use text/html instead which should deliver the content to the app running
// in the browser unchanged.
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
resp.getOutputStream().write(resultList.toJSONString().getBytes("UTF-8"));
resp.getWriter().append(resultList.toJSONString());
}
}
@@ -11,6 +11,7 @@ import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
@@ -54,7 +55,7 @@ public class URLFieldWithFileUpload extends Composite implements HasValue<String
removePanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
final JSONObject resultJson = JSONParser.parseLenient(event.getResults().replaceFirst("<pre[^>]*>(.*)</pre>", "$1")).isObject();
final JSONObject resultJson = parseAfterReplacingSurroundingPreElement(event.getResults()).isObject();
Window.alert(stringMessages.removeResult(resultJson.get("status").isString().stringValue(),
resultJson.get("message") == null ? "" : resultJson.get("message").isString().stringValue()));
setURL("");
@@ -110,7 +111,7 @@ public class URLFieldWithFileUpload extends Composite implements HasValue<String
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
String result = event.getResults();
JSONArray resultJson = (JSONArray) JSONParser.parseLenient(result.replaceFirst("<pre[^>]*>(.*)</pre>", "$1"));
JSONArray resultJson = parseAfterReplacingSurroundingPreElement(result).isArray();
if (resultJson != null) {
if (resultJson.get(0).isObject().get("file_uri") != null) {
uri = resultJson.get(0).isObject().get("file_uri").isString().stringValue();
@@ -185,4 +186,14 @@ public class URLFieldWithFileUpload extends Composite implements HasValue<String
ValueChangeEvent.fire(this, value);
}
}
/**
* See https://github.com/twilson63/ngUpload/issues/43 and
* https://www.sencha.com/forum/showthread.php?132949-Fileupload-Invalid-JSON-string. The JSON response of the file
* upload is wrapped by a &lt;pre&gt; element which needs to be stripped off if present to allow the JSON parser to
* succeed.
*/
private JSONValue parseAfterReplacingSurroundingPreElement(String jsonString) {
return JSONParser.parseStrict(jsonString.replaceFirst("<pre[^>]*>(.*)</pre>", "$1"));
}
}
@@ -1,5 +1,7 @@
eclipse.preferences.version=1
encoding//src/main/java/com/sap/sse/security/ui/client/UserService.java=utf-8
encoding//src/main/java/com/sap/sse/security/ui/client/i18n/StringMessages_de.properties=UTF-8
encoding//src/main/java/com/sap/sse/security/ui/client/shared/oauthlogin/OAuthLogin.java=utf-8
encoding//src/main/java/com/sap/sse/security/ui/login/LoginView.java=utf-8
encoding//src/main/java/com/sap/sse/security/ui/oauth/client/util/ClientUtils.java=utf-8
encoding//src/main/java/com/sap/sse/security/ui/servlet/RedirectServlet.java=utf-8