Package com.almende.eve.rpc.jsonrpc

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


              signalData[0] = request;
              signalData[1] = params;
              signalAgent(new AgentSignal<Object[]>(
                  AgentSignal.INVOKE, signalData));
             
              final JSONResponse response = JSONRPC.invoke(me,
                  request, params, me);
             
              signalAgent(new AgentSignal<JSONResponse>(
                  AgentSignal.RESPOND, response));
              try {
                send(response, senderUrl, null, tag);
              } catch (final IOException e) {
                LOG.log(Level.WARNING, getId()
                    + ": Failed to send response.", e);
              }
            }
          });
         
        } else if (jsonMsg instanceof JSONResponse && callbacks != null
            && id != null && !id.isNull()) {
          final JSONResponse response = (JSONResponse) jsonMsg;
          final AsyncCallback<JSONResponse> callback = callbacks.pull(id);
          if (callback != null) {
            host.getPool().execute(new Runnable() {
              @Override
              public void run() {
                signalAgent(new AgentSignal<JSONResponse>(
                    AgentSignal.RESPONSE, response));
                if (response.getError() != null) {
                  callback.onFailure(response.getError());
                } else {
                  callback.onSuccess(response);
                }
              }
            });
          }
        }
      } else {
        LOG.log(Level.WARNING, getId()
            + ": Received non-JSON message:'" + msg + "'");
      }
    } catch (final Exception e) {
      LOG.log(Level.WARNING, "Exception in receiving message", 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);
      final JSONResponse response = new JSONResponse(jsonError);
      response.setId(id);
      signalAgent(new AgentSignal<JSONResponse>(AgentSignal.EXCEPTION,
          response));
      try {
        send(response, senderUrl, null, tag);
      } catch (final Exception e1) {
View Full Code Here


    // This should already been done!
    if (msg instanceof JSONRPCException) {
      LOG.log(Level.WARNING,
          "Send has been called to send an JSONRPCException i.s.o. a JSONMessage...");
      host.sendAsync(receiverUrl,
          new JSONResponse((JSONRPCException) msg), this, tag);
      return;
    }
    host.sendAsync(receiverUrl, msg, this, tag);
  }
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.receive(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

   * @throws Exception
   */
  @Override
  public JSONResponse send(final String senderUrl, final String receiverUrl,
      final JSONRequest request) throws Exception {
    JSONResponse response;
    String req = request.toString();
   
    // invoke via Apache HttpClient request:
    HttpPost httpPost = new HttpPost(receiverUrl);
    httpPost.setEntity(new StringEntity(req));
   
    // Add token for HTTP handshake
    httpPost.addHeader("X-Eve-Token", TokenStore.create().toString());
    httpPost.addHeader("X-Eve-SenderUrl", senderUrl);
   
    HttpResponse webResp = ApacheHttpClient.get().execute(httpPost);
    try {
      String result = EntityUtils.toString(webResp.getEntity());
      if (result != null) {
        response = new JSONResponse(result);
      } else {
        response = new JSONResponse();
      }
    } catch (JSONRPCException err) {
      response = new JSONResponse(err);
    } finally {
      httpPost.reset();
    }
    return response;
  }
View Full Code Here

      final JSONRequest request,
      final AsyncCallback<JSONResponse> callback) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        JSONResponse response;
        try {
          response = send(senderUrl, receiverUrl, request);
          callback.onSuccess(response);
        } catch (Exception e) {
          callback.onFailure(e);
View Full Code Here

      if (separator != -1) {
        protocol = receiverUrl.substring(0, separator);
        service = getTransportService(protocol);
      }
      if (service != null) {
        JSONResponse response = service.send(
            senderUrl,
            receiverUrl,
            request);
        return response;
      } else {
View Full Code Here

    if (doesShortcut && receiverId != null) {
      //local shortcut
      new Thread(new Runnable() {
        @Override
        public void run() {
          JSONResponse response;
          try {
            String senderUrl = null;
            if (sender != null) {
              senderUrl = getSenderUrl(sender.getId(), receiverUrl);
            }
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);

      agentUrl = req.getRequestURI();
      agentId = httpTransport.getAgentId(agentUrl);
      if (agentId == null || agentId.isEmpty()) {
        resp.sendError(400, "No agentId found in url.");
        return;
      }
     
      if (JSONRPC.hasPrivate(agentFactory.getAgent(agentId).getClass()) && !handleSession(req, resp)) {
        if (!resp.isCommitted()) resp.sendError(401);
        return;
      }
      // Attach the claimed senderId, or null if not given.
      RequestParams requestParams = new RequestParams();
      String senderUrl = req.getHeader("X-Eve-SenderUrl");
      if (senderUrl == null || senderUrl.equals("")){
        senderUrl = "web://"+req.getRemoteUser()+"@"+req.getRemoteAddr();
      }
      requestParams.put(Sender.class, senderUrl);
     
      // invoke the agent
      jsonResponse = agentFactory.receive(agentId, jsonRequest,
          requestParams);
    } catch (Exception err) {
      // generate JSON error response
      err.printStackTrace();
      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

   
    // 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(this, 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

      // TODO: provide authorized sender url
      RequestParams requestParams = new RequestParams();
      requestParams.put(Sender.class, null);
     
      // invoke the agent
      JSONResponse response = factory.receive(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

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.