Refactored GridJettyRestHandler.processRequest(): replace mapper.writeValueAsString with writeValue(stream, v).
@@ -34,6 +34,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -356,41 +357,29 @@ public class GridJettyRestHandler extends AbstractHandler {
U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e);
- cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage());
-
if (e instanceof Error)
throw (Error)e;
- }
-
- String json;
- try {
- json = jsonMapper.writeValueAsString(cmdRes);
+ cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage());
}
- catch (JsonProcessingException e1) {
- U.error(log, "Failed to convert response to JSON: " + cmdRes, e1);
-
- GridRestResponse resFailed = new GridRestResponse(STATUS_FAILED, e1.getMessage());
try {
- json = jsonMapper.writeValueAsString(resFailed);
- }
- catch (JsonProcessingException e2) {
- json = "{\"successStatus\": \"1\", \"error:\" \"" + e2.getMessage() + "\"}}";
- }
- }
+ ServletOutputStream os = res.getOutputStream();
try {
- if (log.isDebugEnabled())
- log.debug("Parsed command response into JSON object: " + json);
+ jsonMapper.writeValue(os, cmdRes);
+ }
+ catch (JsonProcessingException e) {
+ U.error(log, "Failed to convert response to JSON: " + cmdRes, e);
- res.getWriter().write(json);
+ jsonMapper.writeValue(os, F.asMap("successStatus", STATUS_FAILED, "error", e.getMessage()));
+ }
if (log.isDebugEnabled())
log.debug("Processed HTTP request [action=" + act + ", jsonRes=" + cmdRes + ", req=" + req + ']');
}
catch (IOException e) {
- U.error(log, "Failed to send HTTP response: " + json, e);
+ U.error(log, "Failed to send HTTP response: " + cmdRes, e);
}
}
