Package com.almende.eve.transform.rpc.formats

Examples of com.almende.eve.transform.rpc.formats.JSONResponse


   * java.net.URI, java.lang.String)
   */
  @Access(AccessType.UNAVAILABLE)
  @Override
  public void receive(final Object msg, final URI senderUrl, final String tag) {
    final JSONResponse response = rpc.invoke(msg, senderUrl);
    if (response != null) {
      try {
        transport.send(senderUrl, response.toString(), tag);
      } catch (final IOException e) {
        LOG.log(Level.WARNING, "Couldn't send message", e);
      }
    }
  }
View Full Code Here


   */
  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();
  }
View Full Code Here

   * @return the jSON response
   */
  public static JSONResponse invoke(final Object destination,
      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 MethodHandle methodHandle = annotatedMethod.getMethodHandle();
      final Method method = annotatedMethod.getActualMethod();
     
      Object result;
      if (useMethodHandles) {
        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);
      }
      if (result == null) {
        result = JOM.createNullNode();
      }
      resp.setResult(result);
    } catch (final JSONRPCException err) {
      resp.setError(err);
      // TODO: Can this be reduced to Exception? Which Errors do we want
      // to catch?
    } catch (final Throwable err) {
      final Throwable cause = err.getCause();
      if (cause instanceof JSONRPCException) {
        resp.setError((JSONRPCException) cause);
      } else {
        if (err instanceof InvocationTargetException && cause != null) {
          LOG.log(Level.WARNING,
              "Exception raised, returning its cause as JSONRPCException. Request:"
                  + request, cause);
         
          final JSONRPCException jsonError = new JSONRPCException(
              JSONRPCException.CODE.INTERNAL_ERROR,
              getMessage(cause), cause);
          jsonError.setData(cause);
          resp.setError(jsonError);
        } else {
          LOG.log(Level.WARNING,
              "Exception raised, returning it as JSONRPCException. Request:"
                  + request, err);
         
          final JSONRPCException jsonError = new JSONRPCException(
              JSONRPCException.CODE.INTERNAL_ERROR,
              getMessage(err), err);
          jsonError.setData(err);
          resp.setError(jsonError);
        }
      }
    }
    return resp;
  }
View Full Code Here

        } else {
          LOG.warning("Message unknown type:" + msg.getClass());
        }
        if (json != null) {
          if (JSONRPC.isResponse(json)) {
            final JSONResponse response = new JSONResponse(json);
            jsonMsg = response;
          } else if (JSONRPC.isRequest(json)) {
            final JSONRequest request = new JSONRequest(json);
            jsonMsg = request;
          } else {
View Full Code Here

        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);
          } else {
            callback.onSuccess(response);
          }
        }
      }
    } catch (final Exception e) {
      // generate JSON error response, skipped if it was an incoming
      // notification i.s.o. request.
      final JSONRPCException jsonError = new JSONRPCException(
          JSONRPCException.CODE.INTERNAL_ERROR, e.getMessage(), e);
      LOG.log(Level.WARNING, "Exception in receiving message", jsonError);
     
      final JSONResponse response = new JSONResponse(jsonError);
      response.setId(id);
      return response;
    }
    return null;
  }
View Full Code Here

TOP

Related Classes of com.almende.eve.transform.rpc.formats.JSONResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.