// parse request
String jsonRpc = node.get("jsonrpc").getValueAsText();
String methodName = node.get("method").getValueAsText();
String id = node.get("id").getValueAsText();
JsonNode params = node.get("params");
int paramCount = (params!=null) ? params.size() : 0;
// find methods
Set<Method> methods = new HashSet<Method>();
methods.addAll(ReflectionUtil.findMethods(getHandlerClass(), methodName));
// iterate through the methods and remove
// the one's who's parameter count's don't
// match the request
Iterator<Method> itr = methods.iterator();
while (itr.hasNext()) {
Method method = itr.next();
if (method.getParameterTypes().length!=paramCount) {
itr.remove();
}
}
// method not found
if (methods.isEmpty()) {
mapper.writeValue(ops, createErrorResponse(
jsonRpc, id, -32601, "Method not found", null));
return;
}
// choose a method
Method method = null;
List<JsonNode> paramNodes = new ArrayList<JsonNode>();
// handle param arrays, no params
if (paramCount==0 || params.isArray()) {
method = methods.iterator().next();
for (int i=0; i<paramCount; i++) {
paramNodes.add(params.get(i));
}
// handle named params
} else if (params.isObject()) {
// loop through each method
for (Method m : methods) {
// get method annotations
Annotation[][] annotations = m.getParameterAnnotations();
boolean found = true;
List<JsonNode> namedParams = new ArrayList<JsonNode>();
for (int i=0; i<annotations.length; i++) {
// look for param name annotations
String paramName = null;
for (int j=0; j<annotations[i].length; j++) {
if (!JsonRpcParamName.class.isInstance(annotations[i][j])) {
continue;
} else {
paramName = JsonRpcParamName.class.cast(annotations[i][j]).value();
continue;
}
}
// bail if param name wasn't found
if (paramName==null) {
found = false;
break;
// found it by name
} else if (params.has(paramName)) {
namedParams.add(params.get(paramName));
}
}
// did we find it?
if (found) {
method = m;
paramNodes.addAll(namedParams);
break;
}
}
}
// invalid parameters
if (method==null) {
mapper.writeValue(ops, createErrorResponse(
jsonRpc, id, -32602, "Invalid method parameters.", null));
return;
}
// get @JsonRpcErrors and nested @JsonRpcError annotations on method
Annotation[] methodAnnotations = method.getAnnotations();
JsonRpcError[] errorMappings = {};
for (Annotation a : methodAnnotations) {
if (!JsonRpcErrors.class.isInstance(a)) {
continue;
} else {
errorMappings = JsonRpcErrors.class.cast(a).value();
}
}
// invoke the method
JsonNode result = null;
ObjectNode error = null;
Throwable thrown = null;
try {
result = invoke(method, paramNodes);
} catch (Throwable e) {