Package com.almende.eve.rpc.jsonrpc

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


   */
  @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(HttpServletResponse.SC_BAD_REQUEST,
            "No agentId found in url.");
        return;
      }
      Agent agent = agentHost.getAgent(agentId);
      if (agent == null){
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
            "Agent not found at this host.");
        return;
      }
     
      if (JSONRPC.hasPrivate(agent.getClass())
          && !handleSession(req, resp)) {
        if (!resp.isCommitted()) {
          resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        }
        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 = agentHost.receive(agentId, jsonRequest,
          requestParams);
    } catch (Exception err) {
      // generate JSON error response
      LOG.log(Level.WARNING, "", err);
      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


            String id = json.has("id") ? json.get("id").asText()
                : null;
            AsyncCallback<JSONResponse> callback = (id != null) ? callbacks
                .pull(id) : null;
            if (callback != null) {
              callback.onSuccess(new JSONResponse(body));
            }
          } else if (isRequest(json)) {
            // this is a request
            String senderUrl = "xmpp:"+message.getFrom();
            JSONRequest request = new JSONRequest(json);
            invoke(senderUrl, request);
          } else {
            throw new Exception(
                "Request does not contain a valid JSON-RPC request or response");
          }
        } catch (Exception e) {
          // generate JSON error response
          JSONRPCException jsonError = new JSONRPCException(
              JSONRPCException.CODE.INTERNAL_ERROR,
              e.getMessage(), e);
          JSONResponse response = new JSONResponse(jsonError);
         
          // send exception as response
          Message reply = new Message();
          reply.setTo(message.getFrom());
          reply.setBody(response.toString());
          conn.sendPacket(reply);
        }
      }
    }
View Full Code Here

     */
    private void invoke(final String senderUrl, final JSONRequest request) {
      new Thread(new Runnable() {
        @Override
        public void run() {
          JSONResponse response;
          try {
            // append the sender to the request parameters
            RequestParams params = new RequestParams();
            params.put(Sender.class, senderUrl);
           
            // invoke the agent
            response = host.receive(agentId, request,
                params);
          } catch (Exception err) {
            // generate JSON error response
            JSONRPCException jsonError = new JSONRPCException(
                JSONRPCException.CODE.INTERNAL_ERROR,
                err.getMessage(), err);
            response = new JSONResponse(jsonError);
          }
         
          if (response != null) {
            Message reply = new Message();
            String sender = senderUrl.replaceFirst("xmpps?:", "");
            reply.setTo(sender);
            reply.setBody(response.toString());
            conn.sendPacket(reply);
          } else {
            LOG.severe("XMPP response is null? This shouldn't happen...");
          }
        }
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

   */
  @Override
  public JSONResponse send(final String senderUrl, final String receiverUrl,
      final JSONRequest request) throws JSONRPCException {
    try {
      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;
    } catch (Exception e) {
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

            + pushParams);
      }
      String method = pushParams.get("method").textValue();
      ObjectNode params = (ObjectNode) JOM.getInstance().readTree(
          pushParams.get("params").textValue());
      JSONResponse res = JSONRPC.invoke(myAgent, new JSONRequest(method,
          params), myAgent);
     
      JsonNode result = res.getResult();
      if (pushParams.has("onChange")
          && pushParams.get("onChange").asBoolean()) {
        if (lastRes != null && lastRes.equals(result)) {
          return;
        }
View Full Code Here

          // TODO: cleanup logger info
          logger.info("request agentUrl =" + agentUrl + ", agentId=" + 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

        } 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

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.