JsonParser parser = JsonParserFactory.get();
calls = (JsonRpcCalls) parser.parse(in, new JsonRpcCallsJsonDecoder(converterManager, moduleManager));
if (calls.getCallCount() != 1)
{
JsonRpcError error = new JsonRpcError(calls, "Non unique call", ERROR_CODE_INTERNAL, null);
writeResponse(error, response, SC_INTERNAL_SERVER_ERROR);
return;
}
if (!calls.isParseErrorClean())
{
JsonRpcError error = new JsonRpcError(calls, calls.getParseErrors(), ERROR_CODE_PARSE, null);
writeResponse(error, response, SC_INTERNAL_SERVER_ERROR);
return;
}
// Check the methods are accessible
for (Call c : calls)
{
accessControl.assertGeneralExecutionIsPossible(c.getScriptName(), c.getMethodDeclaration());
}
Replies replies = remoter.execute(calls);
Reply reply = replies.getReply(0);
// The existence of a throwable indicates that something went wrong
if (reply.getThrowable() != null)
{
Throwable ex = reply.getThrowable();
JsonRpcError error = new JsonRpcError(calls, ex.getMessage(), ERROR_CODE_SERVER, null);
writeResponse(error, response, SC_INTERNAL_SERVER_ERROR);
return;
}
JsonRpcResponse answer = new JsonRpcResponse(calls.getVersion(), calls.getId(), reply.getReply());
writeResponse(answer, response, HttpServletResponse.SC_OK);
}
catch (JsonRpcCallException ex)
{
writeResponse(new JsonRpcError(ex), response, ex.getHttpStatusCode());
return;
}
catch (JsonParseException ex)
{
JsonRpcError error = new JsonRpcError("2.0", null, ex.getMessage(), ERROR_CODE_PARSE, null);
writeResponse(error, response, SC_INTERNAL_SERVER_ERROR);
return;
}
catch (SecurityException ex)
{
JsonRpcError error = new JsonRpcError(calls, ex.getMessage(), ERROR_CODE_NO_METHOD, null);
writeResponse(error, response, SC_NOT_FOUND);
}
catch (IOException ex)
{
throw ex;
}
catch (Exception ex)
{
log.warn("Unexpected error:", ex);
JsonRpcError error = new JsonRpcError(calls, ex.getMessage(), ERROR_CODE_SERVER, null);
writeResponse(error, response, SC_INTERNAL_SERVER_ERROR);
}
}