// needed to support naughty browsers such as Konqueror and Safari
// which do not honour the charset set in the response
response.setContentType("text/plain;charset=utf-8");
JSONObject jsonReq = null;
JSONRPCResult jsonRes = null;
// Decode using the charset in the request if it exists otherwise
// use UTF-8 as this is what all browser implementations use.
// The JSON-RPC-Java JavaScript client is ASCII clean so it
// although here we can correctly handle data from other clients
// that do not escape non ASCII data
String charset = request.getCharacterEncoding();
if (charset == null) {
charset = "UTF-8";
}
BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));
// Read the request
CharArrayWriter data = new CharArrayWriter();
char[] buf = new char[BUFFER_SIZE];
int ret;
while ((ret = in.read(buf, 0, BUFFER_SIZE)) != -1) {
data.write(buf, 0, ret);
}
writeReceive(data.toString());
try {
jsonReq = new JSONObject(data.toString());
// Process the request
jsonRes = jsonBridge.call(new Object[] { request }, jsonReq);
} catch (ParseException e) {
log.error("can't parse call: " + data, e);
jsonRes = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
}
// Write the response
JsonObjectWriter writer = new JsonObjectWriter(response.getWriter(), 3);
writer.write(jsonRes);