Package com.almende.eve.rpc.jsonrpc

Examples of com.almende.eve.rpc.jsonrpc.JSONResponse


            // to add namespace to method name for JSON-RPC.
            if (method.getName().equals("getId")) {
              return proxyId;
            } else if (method.getName().equals("receive")
                && args.length > 1) {
              JSONResponse response = null;
              if (args[0] != null) {
                response = receive(args[0]);
              }
              if (response != null) {
                JsonNode id = null;
                if (response.getId() != null) {
                  id = response.getId();
                }
                final AsyncCallbackQueue<JSONResponse> cbs = host
                    .getCallbackQueue(proxyId,
                        JSONResponse.class);
                if (cbs != null) {
                  final AsyncCallback<JSONResponse> callback = cbs
                      .pull(id);
                  if (callback != null) {
                    if (response.getError() != null) {
                      callback.onFailure(response
                          .getError());
                    } else {
                      callback.onSuccess(response);
                    }
                  }
                }
              }
              return null;
            } else {
             
              final JSONRequest request = JSONRPC.createRequest(
                  method, args);
             
              final SyncCallback<JSONResponse> callback = new SyncCallback<JSONResponse>();
              final AsyncCallbackQueue<JSONResponse> cbs = host
                  .getCallbackQueue(proxyId,
                      JSONResponse.class);
              if (cbs != null) {
                cbs.push(request.getId(), "", callback);
              }
              try {
                host.sendAsync(receiverUrl, request, agent,
                    null);
              } catch (final IOException e1) {
                throw new JSONRPCException(
                    CODE.REMOTE_EXCEPTION, "", e1);
              }
             
              JSONResponse response;
              try {
                response = callback.get();
              } catch (final Exception e) {
                throw new JSONRPCException(
                    CODE.REMOTE_EXCEPTION, "", e);
              }
              final JSONRPCException err = response.getError();
              if (err != null) {
                throw err;
              } else if (response.getResult() != null
                  && !method.getReturnType()
                      .equals(Void.TYPE)) {
                return TypeUtil.inject(response.getResult(),
                    method.getGenericReturnType());
              } else {
                return null;
              }
            }
View Full Code Here


              + agentId + " request=" + json + ", sender="
              + from.getId());
         
          // invoke the agent
          JSONRequest request = new JSONRequest(json);
          JSONResponse response = agentFactory.receive(agentId,
              request, params);
         
          // reply to message
          Message msg = new MessageBuilder().withRecipientJids(from)
              .withFromJid(to).withBody(response.toString())
              .build();
          xmpp.sendMessage(msg);
        } 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 msg = new MessageBuilder().withRecipientJids(from)
            .withFromJid(to).withBody(response.toString()).build();
        xmpp.sendMessage(msg);
      }
    }
  }
View Full Code Here

              return method.invoke(agent, args);
            }
            else {
              // remote agent
              JSONRequest request = JSONRPC.createRequest(method, args);
              JSONResponse response = send(senderId, receiverUrl, request);
             
              JSONRPCException err = response.getError();
              if (err != null) {
                throw err;
              }
              else if (response.getResult() != null &&
                  !method.getReturnType().equals(Void.TYPE)) {
                return response.getResult(method.getReturnType());
              }
              else {
                return null;
              }
            }
View Full Code Here

  // TOOD: cleanup this method?
  public JSONResponse invoke(String receiverId,
      JSONRequest request, RequestParams requestParams) throws Exception {
    Agent receiver = getAgent(receiverId);
    if (receiver != null) {
      JSONResponse response = JSONRPC.invoke(receiver, request, requestParams);
      receiver.destroy();
      return response;
    }
    else {
      throw new Exception("Agent with id '" + receiverId + "' not found");
View Full Code Here

    if (agentId != null) {
      // local agent, invoke locally
      // TODO: provide Sender in requestParams
      RequestParams requestParams = new RequestParams();
      requestParams.put(Sender.class, null);
      JSONResponse response = invoke(agentId, request, requestParams);
      return response;
    }
    else {
      TransportService service = null;
      String protocol = null;
      int separator = receiverUrl.indexOf(":");
      if (separator != -1) {
        protocol = receiverUrl.substring(0, separator);
        service = getTransportService(protocol);
      }
      if (service != null) {
        JSONResponse response = service.send(senderId, receiverUrl, request);
        return response;
      }
      else {
        throw new ProtocolException(
          "No transport service configured for protocol '" + protocol + "'.");
View Full Code Here

    final String receiverId = getAgentId(receiverUrl);
    if (receiverId != null) {
      new Thread(new Runnable () {
        @Override
        public void run() {
          JSONResponse response;
          try {
            // TODO: provide Sender in requestParams
            RequestParams requestParams = new RequestParams();
            requestParams.put(Sender.class, null);
            response = invoke(receiverId, request, requestParams);
View Full Code Here

   
    // invoke the other agent via the state, allowing the state
    // to route the request internally or externally
    String id = UUID.randomUUID().toString();
    JSONRequest request = new JSONRequest(id, method, jsonParams);
    JSONResponse response = getAgentFactory().send(getId(), url, request);
    JSONRPCException err = response.getError();
    if (err != null) {
      throw err;
    }
    if (type != null && type != void.class) {
      return response.getResult(type);
    }
   
    return null;
  }
View Full Code Here

   */
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    JSONRequest jsonRequest = null;
    JSONResponse jsonResponse = null;
    String body = null;
    String agentUrl = null;
    String agentId = null;
    try {
      // retrieve the agent url and the request body
      body = StringUtil.streamToString(req.getInputStream());
      jsonRequest = new JSONRequest(body);

      // TODO: append authorized sender url to the request parameters
      RequestParams requestParams =  new RequestParams();
      requestParams.put(Sender.class, null);

      // invoke the agent
      agentUrl = req.getRequestURI();
      agentId = httpTransport.getAgentId(agentUrl);
      if (agentId == null || agentId.isEmpty()) {
        resp.sendError(400, "No agentId found in url.");
        return;
      }
      jsonResponse = agentFactory.invoke(agentId, jsonRequest, requestParams);
    } catch (Exception err) {
      // generate JSON error response
      JSONRPCException jsonError = null;
      if (err instanceof JSONRPCException) {
        jsonError = (JSONRPCException) err;
      }
      else {
        jsonError = new JSONRPCException(
            JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());
        jsonError.setData(err);
      }
      jsonResponse = new JSONResponse(jsonError);
    }

    // return response
    resp.addHeader("Content-Type", "application/json");
    resp.getWriter().println(jsonResponse.toString());
    resp.getWriter().close();
  }
View Full Code Here

      // TODO: provide authorized sender url
      RequestParams requestParams = new RequestParams();
      requestParams.put(Sender.class, null);
     
      // invoke the agent
      JSONResponse response = factory.invoke(agentId, request, requestParams);

      // return response
      resp.addHeader("Content-Type", "application/json");
      resp.getWriter().println(response.getResult());
    } catch (Exception err) {
      resp.getWriter().println(err.getMessage());
    }
  }
View Full Code Here

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    JSONRequest jsonRequest = null;
    JSONResponse jsonResponse = null;   
    try {
      // retrieve the request body
      String body = StringUtil.streamToString(req.getInputStream());
      jsonRequest = new JSONRequest(body);
     
      // TODO: append authorized sender url to the request parameters
      RequestParams requestParams = new RequestParams();
      requestParams.put(Sender.class, null);

      // invoke the agent
      jsonResponse = agentFactory.invoke(agentId, jsonRequest, requestParams);
    } catch (Exception err) {
      // generate JSON error response
      JSONRPCException jsonError = null;
      if (err instanceof JSONRPCException) {
        jsonError = (JSONRPCException) err;
      }
      else {
        jsonError = new JSONRPCException(
            JSONRPCException.CODE.INTERNAL_ERROR, err.getMessage());       
        jsonError.setData(err);
      }
      jsonResponse = new JSONResponse(jsonError);
    }

    // return response
    resp.addHeader("Content-Type", "application/json");
    resp.getWriter().println(jsonResponse.toString());
  }
View Full Code Here

TOP

Related Classes of com.almende.eve.rpc.jsonrpc.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.