Examples of JSONRequest


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

   *            the params
   * @return the JSON request
   */
  public <T> JSONRequest buildMsg(final String method,
      final ObjectNode params) {
    return new JSONRequest(method, params);
  }
View Full Code Here

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

   *            the callback
   * @return the JSON request
   */
  public <T> JSONRequest buildMsg(final String method,
      final ObjectNode params, final AsyncCallback<T> callback) {
    final JSONRequest request = new JSONRequest(method, params);
    addCallback(request, callback);
    return request;
  }
View Full Code Here

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

   *            the callback
   * @return the JSON request
   */
  public <T> JSONRequest buildMsg(final Method method, final Object[] params,
      final AsyncCallback<T> callback) {
    final JSONRequest request = JSONRPC.createRequest(method, params);
    addCallback(request, callback);
    return request;
  }
View Full Code Here

Examples of org.butor.json.JsonRequest

      handler.addMessage(CommonMessageID.SERVICE_FAILURE.getMessage());
    }
  }

  protected JsonRequest findJsonRequest(final HttpServletRequest req_) throws IOException {
    JsonRequest jr = null;
    InputStream is = null;
    String qs;
    if (req_.getMethod().equalsIgnoreCase("post")) {
      is = req_.getInputStream();
      StringWriter sw = new StringWriter();
      while (true) {
        int bb = is.read();
        if (bb == -1)
          break;
        sw.write(bb);
      }
      qs = sw.toString();
    } else {
      qs = req_.getQueryString();
    }

    Map<String,String> argsMap = new HashMap<String, String>();
    String[] toks = qs.split("&");
    for (String tok : toks) {
      tok = URLDecoder.decode(tok, "utf-8");
      String[] pair = tok.split("=");
      argsMap.put(pair[0], pair.length > 1 ? pair[1] : null);
    }
    jr = new JsonRequest();
    String userId = null;
    if (req_.getUserPrincipal() != null)
      userId = req_.getUserPrincipal().getName();
    jr.setUserId(userId);
    jr.setSessionId(req_.getSession().getId());
    jr.setReqId(argsMap.get("reqId"));
    jr.setService(argsMap.get("service"));
    jr.setLang(argsMap.get("lang"));
    String val = argsMap.get("streaming");
    jr.setStreaming(StringUtil.isEmpty(val) ? false : val.equalsIgnoreCase("yes") || val.equalsIgnoreCase("true"));

    jr.setServiceArgsJson(URLDecoder.decode(argsMap.get("args"), "UTF-8"));

    return jr;
  }
View Full Code Here

Examples of org.butor.json.JsonRequest

      throws ServletException, IOException {

    OutputStream os = null;
    BinResponseHandler streamer = null;
    String serviceName = null;
    JsonRequest jr = null;
    try {
      jr = findJsonRequest(req_);
      List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
      int nbArgs = params.size();
      final JsonRequest fjr = jr;

      serviceName = jr.getService();
      if (StringUtil.isEmpty(serviceName))
        throw new IllegalArgumentException("Missing serviceName");
View Full Code Here

Examples of org.butor.json.JsonRequest

    ResponseHandler<Object> streamer = null;
    String serviceName = null;
    final List<Message> fmsgs = new ArrayList<Message>();
    final List<Object> frows = new ArrayList<Object>();
    ScriptStreamer notif = null;
    JsonRequest jr = null;
    try {
      jr = findJsonRequest(req_);
      List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
      int nbArgs = params.size();
      final JsonRequest fjr = jr;

      serviceName = jr.getService();
      if (StringUtil.isEmpty(serviceName))
        throw new IllegalArgumentException("Missing serviceName");

      Method m = findService(serviceName, nbArgs);
      if (m == null)
        throw new UnsupportedOperationException(
          String.format("Service=%s with %d args doesn't exists",
            serviceName, nbArgs +1));

      if (!fjr.isStreaming()) {
        resp_.setContentType("application/json");
      } else {
        resp_.setContentType("text/html");
      }

      os = resp_.getOutputStream();
      final OutputStream fos = os;

      notif = fjr.isStreaming() ? new ScriptStreamer() : null;
      final ScriptStreamer fnotif = notif;

      final String reqId = jr.getReqId();

      streamer = new ResponseHandler<Object>() {
        int rowsPerNotif = 2;
        long rowCount = 0;
        boolean startedResp = false;
        @Override
        public void end() {
          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to write beginnig of response", e1);
            }
        }
        @Override
        public boolean addRow(Object row_) {
          if (row_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          rowCount++;
          try {
            if (fjr.isStreaming()) {
              // send small size notif first, then grows slowly
              // to reach a max. This way, results will hit the browser
              // very soon.
              if (rowsPerNotif < MAX_ROWS_PER_NOTIF)
                rowsPerNotif+=2;
              frows.add(row_);
              //if (frows.size() >= rowsPerNotif) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              //}
            } else {
              if (rowCount>1)
                fos.write(',');
              String json = _jsh.serialize(row_);
              fos.write(json.getBytes());
            }
            fos.flush();
            return true;
          } catch (IOException e) {
            _logger.warn("Failed to addRow", e);
          }
          return false;
        }

        @Override
        public boolean addMessage(Message message_) {
          if (message_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          fmsgs.add(message_);
          try {
            if (fjr.isStreaming()) {
              if (frows.size() > 0) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              }

              String json = _jsh.serialize(fmsgs);
              fmsgs.clear();
              fos.write(fnotif.buildNotif(fjr.getReqId(), "msg", json, false));
              fos.flush();
            }
            return true;
          } catch (IOException e) {
            _logger.warn("Failed to write row or message", e);
View Full Code Here

Examples of org.butor.json.JsonRequest

    ResponseHandler<Object> streamer = null;
    String serviceName = null;
    final List<Message> fmsgs = new ArrayList<Message>();
    final List<Object> frows = new ArrayList<Object>();
    ScriptStreamer notif = null;
    JsonRequest jr = null;
    try {
      String qs;
      if (req_.getMethod().equalsIgnoreCase("post")) {
        is = req_.getInputStream();
        StringWriter sw = new StringWriter();
        while (true) {
          int bb = is.read();
          if (bb == -1)
            break;
          sw.write(bb);
        }
        qs = sw.toString();
      } else {
        qs = req_.getQueryString();
      }

      Map<String,String> argsMap = new HashMap<String, String>();
      String[] toks = qs.split("&");
      for (String tok : toks) {
        tok = URLDecoder.decode(tok, "utf-8");
        String[] pair = tok.split("=");
        argsMap.put(pair[0], pair.length > 1 ? pair[1] : null);
      }
      jr = new JsonRequest();
      String userId = null;
      if (req_.getUserPrincipal() != null)
        userId = req_.getUserPrincipal().getName();
      jr.setUserId(userId);
      jr.setSessionId(req_.getSession().getId());
      jr.setReqId(argsMap.get("reqId"));
      jr.setService(argsMap.get("service"));
      jr.setLang(argsMap.get("lang"));
      String val = argsMap.get("streaming");
      jr.setStreaming(StringUtil.isEmpty(val) ? false : val.equalsIgnoreCase("yes") || val.equalsIgnoreCase("true"));

      jr.setServiceArgsJson(URLDecoder.decode(argsMap.get("args"), "UTF-8"));

      List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
      int nbArgs = params.size();
      final JsonRequest fjr = jr;

      serviceName = jr.getService();
      if (StringUtil.isEmpty(serviceName))
        throw new IllegalArgumentException("Missing serviceName");

      String key = buildKey(serviceName, nbArgs +1);//+1 for context as arg[0]
      Method m = _servicesMap.get(key);
      if (m == null)
        throw new UnsupportedOperationException(
          String.format("Service=%s with %d args doesn't exists",
            serviceName, nbArgs +1));

      if (!fjr.isStreaming()) {
        resp_.setContentType("application/json");
      } else {
        resp_.setContentType("text/html");
      }

      os = resp_.getOutputStream();
      final OutputStream fos = os;

      notif = fjr.isStreaming() ? new ScriptStreamer() : null;
      final ScriptStreamer fnotif = notif;

      final String reqId = jr.getReqId();

      streamer = new ResponseHandler<Object>() {
        int rowsPerNotif = 2;
        long rowCount = 0;
        boolean startedResp = false;
        @Override
        public void end() {
          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to write beginnig of response", e1);
            }
        }
        @Override
        public boolean addRow(Object row_) {
          if (row_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          rowCount++;
          try {
            if (fjr.isStreaming()) {
              // send small size notif first, then grows slowly
              // to reach a max. This way, results will hit the browser
              // very soon.
              if (rowsPerNotif < MAX_ROWS_PER_NOTIF)
                rowsPerNotif+=2;
              frows.add(row_);
              if (frows.size() >= rowsPerNotif) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              }
            } else {
              if (rowCount>1)
                fos.write(',');
              String json = _jsh.serialize(row_);
              fos.write(json.getBytes());
            }
            fos.flush();
            return true;
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          return false;
        }

        @Override
        public boolean addMessage(Message message_) {
          if (message_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          fmsgs.add(message_);
          try {
            if (fjr.isStreaming()) {
              if (frows.size() > 0) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              }

              String json = _jsh.serialize(fmsgs);
              fmsgs.clear();
              fos.write(fnotif.buildNotif(fjr.getReqId(), "msg", json, false));
              fos.flush();
            }
            return true;
          } catch (IOException e) {
            _logger.warn("Failed to write row or message", e);
View Full Code Here

Examples of org.butor.json.JsonRequest

      handler.addMessage(CommonMessageID.SERVICE_FAILURE.getMessage());
    }
  }

  protected JsonRequest findJsonRequest(final HttpServletRequest req_) throws IOException {
    JsonRequest jr = null;
    InputStream is = null;
    String qs;
    if (req_.getMethod().equalsIgnoreCase("post")) {
      is = req_.getInputStream();
      StringWriter sw = new StringWriter();
      while (true) {
        int bb = is.read();
        if (bb == -1)
          break;
        sw.write(bb);
      }
      qs = sw.toString();
    } else {
      qs = req_.getQueryString();
    }

    Map<String,String> argsMap = new HashMap<String, String>();
    String[] toks = qs.split("&");
    for (String tok : toks) {
      tok = URLDecoder.decode(tok, "utf-8");
      String[] pair = tok.split("=");
      argsMap.put(pair[0], pair.length > 1 ? pair[1] : null);
    }
    jr = new JsonRequest();
    String userId = null;
    if (req_.getUserPrincipal() != null)
      userId = req_.getUserPrincipal().getName();
    jr.setUserId(userId);
    String sessionId = argsMap.get("sessionId");
    if (Strings.isNullOrEmpty(sessionId)) {
      sessionId = req_.getSession().getId();
    }
    jr.setSessionId(sessionId);
    jr.setReqId(argsMap.get("reqId"));
    jr.setService(argsMap.get("service"));
    jr.setLang(argsMap.get("lang"));
    String val = argsMap.get("streaming");
    jr.setStreaming(StringUtil.isEmpty(val) ? false : val.equalsIgnoreCase("yes") || val.equalsIgnoreCase("true"));

    jr.setServiceArgsJson(URLDecoder.decode(argsMap.get("args"), "UTF-8"));

    return jr;
  }
View Full Code Here

Examples of org.butor.json.JsonRequest

      throws ServletException, IOException {

    OutputStream os = null;
    BinResponseHandler streamer = null;
    String serviceName = null;
    JsonRequest jr = null;
    try {
      jr = findJsonRequest(req_);
      List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
      int nbArgs = params.size();
      final JsonRequest fjr = jr;

      serviceName = jr.getService();
      if (StringUtil.isEmpty(serviceName))
        throw new IllegalArgumentException("Missing serviceName");
View Full Code Here

Examples of org.butor.json.JsonRequest

    ResponseHandler<Object> streamer = null;
    String serviceName = null;
    final List<Message> fmsgs = new ArrayList<Message>();
    final List<Object> frows = new ArrayList<Object>();
    ScriptStreamer notif = null;
    JsonRequest jr = null;
    try {
      jr = findJsonRequest(req_);
      List<?> params = _jsh.deserialize(jr.getServiceArgsJson(), List.class);
      int nbArgs = params.size();
      final JsonRequest fjr = jr;

      serviceName = jr.getService();
      if (StringUtil.isEmpty(serviceName))
        throw new IllegalArgumentException("Missing serviceName");

      Method m = findService(serviceName, nbArgs);
      if (m == null) {
        String msg = String.format("Service=%s with %d args doesn't exists",
          serviceName, nbArgs +1);
        resp_.sendError(HttpServletResponse.SC_BAD_REQUEST,msg);
        throw new UnsupportedOperationException(
            msg);
      }
      if (!fjr.isStreaming()) {
        resp_.setContentType("application/json");
      } else {
        resp_.setContentType("text/html");
      }

      os = resp_.getOutputStream();
      final OutputStream fos = os;

      notif = fjr.isStreaming() ? new ScriptStreamer() : null;
      final ScriptStreamer fnotif = notif;

      final String reqId = jr.getReqId();

      streamer = new ResponseHandler<Object>() {
        int rowsPerNotif = 2;
        long rowCount = 0;
        boolean startedResp = false;
        @Override
        public void end() {
          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to write beginnig of response", e1);
            }
        }
        @Override
        public boolean addRow(Object row_) {
          if (row_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          rowCount++;
          try {
            if (fjr.isStreaming()) {
              // send small size notif first, then grows slowly
              // to reach a max. This way, results will hit the browser
              // very soon.
              if (rowsPerNotif < MAX_ROWS_PER_NOTIF)
                rowsPerNotif+=2;
              frows.add(row_);
              //if (frows.size() >= rowsPerNotif) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              //}
            } else {
              if (rowCount>1)
                fos.write(',');
              String json = _jsh.serialize(row_);
              fos.write(json.getBytes());
            }
            fos.flush();
            return true;
          } catch (IOException e) {
            _logger.warn("Failed to addRow", e);
          }
          return false;
        }

        @Override
        public boolean addMessage(Message message_) {
          if (message_ == null)
            return false;

          if (!startedResp)
            try {
              //startResp(os, fjr.isStreaming(), reqId, notif.funcDef());
              startedResp = true;
              if (fjr.isStreaming()) {
                fos.write(fnotif.funcDef());
                fos.flush();
              } else {
                fos.write(String.format("{\"reqId\":\"%s\",\"data\":[", reqId).getBytes());
                fos.flush();
              }
            } catch (IOException e1) {
              _logger.warn("Failed to addRow", e1);
              return false;
            }

          fmsgs.add(message_);
          try {
            if (fjr.isStreaming()) {
              if (frows.size() > 0) {
                String json = _jsh.serialize(frows);
                frows.clear();
                fos.write(fnotif.buildNotif(fjr.getReqId(), "row", json, false));
              }

              String json = _jsh.serialize(fmsgs);
              fmsgs.clear();
              fos.write(fnotif.buildNotif(fjr.getReqId(), "msg", json, false));
              fos.flush();
            }
            return true;
          } catch (IOException e) {
            _logger.warn("Failed to write row or message", e);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.