Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
Servlets Post 2.1.0
-
None
-
OS: Windows 2k8, Windows 7
Web Servers: Tomcat 6
Sling 6
Description
The ImportOperation on the SlingPostServlet (2.1.0 to Trunk) does not support encoding specified in the form charset parameter on a POST request.
REPRODUCTION STEPS:
Create a POST request using the SlingPostServlet and ImportOperation:
curl -F":operation=import" -F"charset=UTF-8" -F":contentType=json" -F":replace=true" -F":replaceProperties=true" -F":content=
{'latin':'øµå', 'chinese':'玄牛'}" http://admin:admin@localhost:8080
PROPOSED SOLUTION:
In ImportOperation.java, line 137 (as of 2.1.0 tag):
contentStream = new ByteArrayInputStream(content.getBytes());
This line will take the :content parameter on a request, properly encoded (ex. UTF-8) and get the bytes using system-level encoding. This causes all unicode characters in the content to be encoded, on windows, with the wrong encoding.
The simple fix, which resolves the issue and allows UTF-8 encoding across all operating systems:
contentStream = new ByteArrayInputStream(content.getBytes("UTF-8"));
My proposed fix, is to support the form parameter charset, since :content is a parameter on the form in the post request:
RequestParameter encodingParam = request.getRequestParameter("charset");
byte[] contentBytes;
if (encodingParam != null && encodingParam.getString() != null) {
contentBytes = content.getBytes(encodingParam.getString());
} else {
contentBytes = content.getBytes();
}
contentStream = new ByteArrayInputStream(contentBytes);