use of org.apache.ignite.internal.processors.rest.request.GridRestRequest in project ignite by apache.
the class GridJettyRestHandler method processRequest.
/**
* Process HTTP request.
*
* @param act Action.
* @param req Http request.
* @param res Http response.
*/
private void processRequest(String act, HttpServletRequest req, HttpServletResponse res) {
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
GridRestCommand cmd = command(req);
if (cmd == null) {
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!authChecker.apply(req.getHeader("X-Signature"))) {
res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
GridRestResponse cmdRes;
Map<String, String> params = parameters(req);
try {
GridRestRequest cmdReq = createRequest(cmd, params, req);
if (log.isDebugEnabled())
log.debug("Initialized command request: " + cmdReq);
cmdRes = hnd.handle(cmdReq);
if (cmdRes == null)
throw new IllegalStateException("Received null result from handler: " + hnd);
if (getAllAsArray && cmd == GridRestCommand.CACHE_GET_ALL) {
List<Object> resKeyValue = new ArrayList<>();
for (Map.Entry<Object, Object> me : ((Map<Object, Object>) cmdRes.getResponse()).entrySet()) resKeyValue.add(new IgniteBiTuple<>(me.getKey(), me.getValue()));
cmdRes.setResponse(resKeyValue);
}
byte[] sesTok = cmdRes.sessionTokenBytes();
if (sesTok != null)
cmdRes.setSessionToken(U.byteArray2HexString(sesTok));
res.setStatus(cmdRes.getSuccessStatus() == GridRestResponse.SERVICE_UNAVAILABLE ? HttpServletResponse.SC_SERVICE_UNAVAILABLE : HttpServletResponse.SC_OK);
} catch (Throwable e) {
res.setStatus(HttpServletResponse.SC_OK);
U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e);
if (e instanceof Error)
throw (Error) e;
cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage());
}
try (ServletOutputStream os = res.getOutputStream()) {
try {
// Try serialize.
jsonMapper.writeValue(NULL_OUTPUT_STREAM, cmdRes);
jsonMapper.writeValue(os, cmdRes);
} catch (JsonProcessingException e) {
U.error(log, "Failed to convert response to JSON: " + cmdRes, e);
jsonMapper.writeValue(os, new GridRestResponse(STATUS_FAILED, 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: " + cmdRes, e);
}
}
Aggregations