// Find and execute the corresponding callback
String id = json.has("id") ? json.get("id").asText() : null;
AsyncCallback<JSONResponse> callback =
(id != null) ? callbacks.pull(id) : null;
if (callback != null) {
callback.onSuccess(new JSONResponse(body));
}
else {
/*
// TODO: is it needed to send this error back?
// can possibly result in weird loops?
throw new Exception("Callback with id '" + id + "' not found");
*/
}
}
else if (isRequest(json)) {
// this is a request
String senderUrl = message.getFrom();
JSONRequest request = new JSONRequest(json);
invoke(senderUrl, request);
}
else {
throw new Exception("Request does not contain a valid JSON-RPC request or response");
}
}
catch (Exception err) {
// generate JSON error response
JSONRPCException jsonError = new JSONRPCException(
JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());
JSONResponse response = new JSONResponse(jsonError);
// send exception as response
Message reply = new Message();
reply.setTo(message.getFrom());
reply.setBody(response.toString());
conn.sendPacket(reply);
}
}
}