/** * Sets the callback timeout. (in seconds) * * @param timeout * the new callback timeout */ public void setCallbackTimeout(final int timeout) { this.put("rpcTimeout", timeout); }
/** * @param params * @param annotatedParams * @param requestParams * @return the Object[] */ private static Object[] castParams(final ObjectNode params, final List<AnnotatedParam> annotatedParams, final RequestParams requestParams) { return castParams(null, params, annotatedParams, requestParams); }
/** * Invoke a method on an object. * * @param destination * destination url * @param request * the request * @param auth * the auth * @return the jSON response */ public static JSONResponse invoke(final Object destination, final JSONRequest request, final Authorizor auth) { return invoke(destination, request, null, auth); }
final JSONRequest request, final RequestParams requestParams, final Authorizor auth) { final JSONResponse resp = new JSONResponse(request.getId(), null); try { final CallTuple tuple = NamespaceUtil.get(destination, request.getMethod()); final Object realDest = tuple.getDestination(); final AnnotatedMethod annotatedMethod = tuple.getMethod(); if (!isAvailable(annotatedMethod, realDest, requestParams, auth)) { throw new JSONRPCException( JSONRPCException.CODE.METHOD_NOT_FOUND, "Method '" + request.getMethod() + "' not found. The method does not exist or you are not authorized."); final Object[] params = castParams(realDest, request.getParams(), annotatedMethod.getParams(), requestParams); result = methodHandle.invokeExact(params); } else { final Object[] params = castParams(request.getParams(), annotatedMethod.getParams(), requestParams); result = method.invoke(realDest, params); resp.setResult(result); } catch (final JSONRPCException err) { resp.setError(err); } catch (final Throwable err) {
final JSONMessage jsonMsg = jsonConvert(msg); if (jsonMsg == null) { LOG.log(Level.INFO, "Received non-JSONRPC message:'" + msg + "'"); return null; final JsonNode id = jsonMsg.getId(); try { if (jsonMsg.isRequest()) { final JSONRequest request = (JSONRequest) jsonMsg; final RequestParams params = new RequestParams(); params.put(Sender.class, senderUrl.toASCIIString()); return JSONRpc.invoke(destination.get(), request, params, auth); } else if (jsonMsg.isResponse() && callbacks != null && id != null && !id.isNull()) { final AsyncCallback<JSONResponse> callback = callbacks.pull(id); if (callback != null) { final JSONResponse response = (JSONResponse) jsonMsg; final JSONRPCException error = response.getError(); if (error != null) { callback.onFailure(error); final JSONRPCException jsonError = new JSONRPCException( JSONRPCException.CODE.INTERNAL_ERROR, e.getMessage(), e); LOG.log(Level.WARNING, "Exception in receiving message", jsonError); if (id != null){ final JSONResponse response = new JSONResponse(jsonError); response.setId(id); return response;
@Override public JSONRpcProtocol build() { JSONRpcProtocolConfig config = new JSONRpcProtocolConfig(getParams()); String id = config.getId(); if (id == null) { id = new UUID().toString(); if (INSTANCES.containsKey(id)) { result = INSTANCES.get(id); final Handler<Object> oldHandle = result.getHandle(); oldHandle.update(TYPEUTIL.inject(getHandle())); } else { result = new JSONRpcProtocol(config, TYPEUTIL.inject(getHandle())); @SuppressWarnings("unchecked") final Class<Authorizor> clazz = (Class<Authorizor>) Class .forName(config.getAuthorizor()); result.setAuth(clazz.newInstance()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { LOG.log(Level.WARNING, "Couldn't instantiate authorizor class:" + config.getAuthorizor(), e);
@Override public void delete() { callbacks.clear(); JSONRpcProtocolConfig config = new JSONRpcProtocolConfig(getParams()); JSONRpcProtocolBuilder.delete(config.getId()); }
final JSONRpcProtocolConfig conf = new JSONRpcProtocolConfig(); if (agentId != null && conf.getId() == null) { conf.setId(agentId); protocolStack.add(new JSONRpcProtocolBuilder().withConfig(conf) .withHandle(handler).build());
/** * Invoke a method on an object. * * @param destination * the destination * @param request * A request in JSON-RPC format * @param requestParams * Optional request parameters * @param auth * the auth * @return the string * @throws IOException * Signals that an I/O exception has occurred. */ public static String invoke(final Object destination, final String request, final RequestParams requestParams, final Authorizor auth) throws IOException { JSONRequest jsonRequest = null; JSONResponse jsonResponse = null; try { jsonRequest = new JSONRequest(request); jsonResponse = invoke(destination, jsonRequest, requestParams, auth); } catch (final JSONRPCException err) { jsonResponse = new JSONResponse(err); } return jsonResponse.toString(); }
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final CallTuple result = new CallTuple(); String reducedPath = ""; String reducedMethod = path; final String newSteps = destination.getClass().getName(); CACHE.put("", new AnnotatedMethod[0]); populateCache(destination, newSteps, methods); result.setDestination(newDestination); final AnnotatedClass newClazz = AnnotationUtil.get(newDestination .getClass()); .getMethods(reducedMethod); if (!methods.isEmpty()) { result.setMethod(methods.get(0));
/** * Instantiates a new JSON rpc protocol. * * @param params * the params * @param handle * the handle */ public JSONRpcProtocol(final ObjectNode params, final Handler<Object> handle) { destination = handle; myParams = params; final JSONRpcProtocolConfig config = new JSONRpcProtocolConfig(params); callbacks.setDefTimeout(config.getCallbackTimeout()); }
/** * Gets the methods. * * @return the methods */ @Access(AccessType.PUBLIC) @Override @JsonIgnore public List<Object> getMethods() { return ((JSONRpcProtocol) protocolStack.getLast()).getMethods(); }
/** * Instantiates a new JSON rpc protocol config. * * @param node * the node */ public JSONRpcProtocolConfig(final ObjectNode node) { super(node); if (!this.has("class")) { setClassName(JSONRpcProtocolBuilder.class.getName()); } }
public Meta outbound(final Object msg, final URI recipientUrl) { if (msg instanceof JSONRequest) { final JSONRequest request = (JSONRequest) msg; addCallback(request, request.getCallback()); } return new Meta(msg); }
/** * Gets the. * * @param destination * the destination * @param path * the path * @return the call tuple * @throws IllegalAccessException * the illegal access exception * @throws InvocationTargetException * the invocation target exception * @throws NoSuchMethodException * the no such method exception */ public static CallTuple get(final Object destination, final String path) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return INSTANCE._get(destination, path); }
@Override public Meta inbound(final Object msg, final URI senderUrl) { final JSONResponse response = invoke(msg, senderUrl); return new Meta(response, response == null, response != null); }
/** * Describe all JSON-RPC methods of given class. * * @param c * The class to be described * @param requestParams * Optional request parameters. * @param auth * the authorizor * @return the list */ public static List<Object> describe(final Object c, final RequestParams requestParams, final Authorizor auth) { try { final Map<String, Object> methods = _describe(c, requestParams, "", auth); // create a sorted array final TreeSet<String> methodNames = new TreeSet<String>( methods.keySet()); final List<Object> sortedMethods = new ArrayList<Object>(methodNames.size()); for (final String methodName : methodNames) { sortedMethods.add(methods.get(methodName)); } return sortedMethods; } catch (final Exception e) { LOG.log(Level.WARNING, "Failed to describe class:" + c.toString(), e); return null; } }
/** * Invoke a method on an object. * * @param destination * the destination * @param request * A request in JSON-RPC format * @param auth * the auth * @return the string * @throws IOException * Signals that an I/O exception has occurred. */ public static String invoke(final Object destination, final String request, final Authorizor auth) throws IOException { return invoke(destination, request, null, auth); }