Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.almende.eve.rpc.jsonrpc;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.almende.eve.agent.AgentInterface;
import com.almende.eve.agent.annotation.Access;
import com.almende.eve.agent.annotation.AccessType;
import com.almende.eve.agent.annotation.Name;
import com.almende.eve.agent.annotation.Required;
import com.almende.eve.agent.annotation.Sender;
import com.almende.eve.rpc.RequestParams;
import com.almende.eve.rpc.jsonrpc.jackson.JOM;
import com.almende.util.AnnotationUtil;
import com.almende.util.AnnotationUtil.AnnotatedClass;
import com.almende.util.AnnotationUtil.AnnotatedMethod;
import com.almende.util.AnnotationUtil.AnnotatedParam;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class JSONRPC {
// static private Logger logger = Logger.getLogger(JSONRPC.class.getName());
// TODO: the integration with requestParams is quite a mess.
// TODO: implement JSONRPC 2.0 Batch
/**
* Invoke a method on an object
*
* @param obj
* Request will be invoked on the given object
* @param request
* A request in JSON-RPC format
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
static public String invoke(AgentInterface destination, String request)
throws JsonGenerationException, JsonMappingException, IOException {
return invoke(destination, request, null);
}
/**
* Invoke a method on an object
*
* @param obj
* Request will be invoked on the given object
* @param request
* A request in JSON-RPC format
* @param requestParams
* Optional request parameters
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonGenerationException
*/
static public String invoke(AgentInterface destination, String request,
RequestParams requestParams) throws JsonGenerationException,
JsonMappingException, IOException {
JSONRequest jsonRequest = null;
JSONResponse jsonResponse = null;
try {
jsonRequest = new JSONRequest(request);
jsonResponse = invoke(destination, jsonRequest, requestParams);
} catch (JSONRPCException err) {
jsonResponse = new JSONResponse(err);
}
return jsonResponse.toString();
}
/**
* Invoke a method on an object
*
* @param sender
* Sender url
* @param obj
* will be invoked on the given object
* @return
*/
static public JSONResponse invoke(AgentInterface destination, JSONRequest request) {
return invoke(destination, request, null);
}
/**
* Invoke a method on an object
*
* @param obj
* Request will be invoked on the given object
* @param request
* A request in JSON-RPC format
* @param requestParams
* Optional request parameters
* @return
*/
static public JSONResponse invoke(AgentInterface destination, JSONRequest request,
RequestParams requestParams) {
JSONResponse resp = new JSONResponse();
resp.setId(request.getId());
try {
AnnotatedMethod annotatedMethod = getMethod(destination,
request.getMethod(), requestParams);
if (annotatedMethod == null) {
throw new JSONRPCException(
JSONRPCException.CODE.METHOD_NOT_FOUND, "Method '"
+ request.getMethod() + "' not found. The method does not exist or you are not authorized.");
}
Method method = annotatedMethod.getActualMethod();
Object[] params = castParams(request.getParams(),
annotatedMethod.getParams(), requestParams);
Object result = method.invoke(destination, params);
if (result == null) {
result = JOM.createNullNode();
}
resp.setResult(result);
} catch (Exception err) {
if (err instanceof JSONRPCException) {
resp.setError((JSONRPCException) err);
} else if (err.getCause() != null
&& err.getCause() instanceof JSONRPCException) {
resp.setError((JSONRPCException) err.getCause());
} else {
err.printStackTrace(); // TODO: cleanup printing stacktrace
JSONRPCException jsonError = new JSONRPCException(
JSONRPCException.CODE.INTERNAL_ERROR, getMessage(err));
// TODO: return useful, readable stacktrace
jsonError.setData(err);
resp.setError(jsonError);
}
}
return resp;
}
/**
* Validate whether the given class contains valid JSON-RPC methods. A class
* if valid when:
* - There are no public methods with equal names
* - The parameters of all public methods have the @Name annotation
* If the class is not valid, an Exception is thrown
*
* @param c
* The class to be verified
* @param requestParams
* optional request parameters
* @return errors A list with validation errors. When no problems are found,
* an empty list is returned
*/
static public List validate(Class> c, RequestParams requestParams) {
List errors = new ArrayList();
Set methodNames = new HashSet();
AnnotatedClass ac = null;
try {
ac = AnnotationUtil.get(c);
} catch (Exception e) {
e.printStackTrace();
errors.add("Class as a whole can't be wrapped for annotation, probably because it's not an AgentInterface implementation.");
}
if (ac != null) {
for (AnnotatedMethod method : ac.getMethods()) {
boolean available = false;
try {
available = isAvailable(method, null, requestParams);
} catch (Exception e) {
e.printStackTrace();
errors.add("Problems running isAvailable method on annotated class.");
}
if (available) {
// The method name may only occur once
String name = method.getName();
if (methodNames.contains(name)) {
errors.add("Public method '" + name
+ "' is defined more than once, which is not"
+ " allowed for JSON-RPC.");
}
methodNames.add(name);
// each of the method parameters must have the @Name
// annotation
List params = method.getParams();
for (int i = 0; i < params.size(); i++) {
List matches = new ArrayList();
for (Annotation a : params.get(i).getAnnotations()) {
if (requestParams != null && requestParams.has(a)) {
matches.add(a);
} else if (a instanceof Name) {
matches.add(a);
}
}
if (matches.size() == 0) {
errors.add("Parameter "
+ i
+ " in public method '"
+ name
+ "' is missing the @Name annotation, which is"
+ " required for JSON-RPC.");
} else if (matches.size() > 1) {
String str = "";
for (Annotation a : matches) {
str += a + " ";
}
errors.add("Parameter " + i + " in public method '"
+ name + "' contains " + matches.size()
+ " annotations " + "(" + str
+ "), but only one is allowed.");
}
}
}
}
}
return errors;
}
/**
* Describe all JSON-RPC methods of given class
*
* @param c
* The class to be described
* @param requestParams
* Optional request parameters.
* @param asString
* If false (default), the returned description is a JSON
* structure. If true, the described methods will be in an easy
* to read string.
* @return
*/
public static List