Example #1
0
  /**
   * Handles the given {@link ArrayNode} and writes the responses to the given {@link OutputStream}.
   *
   * @param node the {@link JsonNode}
   * @param ops the {@link OutputStream}
   * @throws JsonGenerationException
   * @throws JsonMappingException
   * @throws IOException
   */
  private void handleArray(ArrayNode node, OutputStream ops)
      throws JsonGenerationException, JsonMappingException, IOException {

    // loop through each array element
    for (int i = 0; i < node.size(); i++) {
      handleNode(node.get(i), ops);
    }
  }
Example #2
0
  /**
   * Handles a single request from the given {@link InputStream}, that is to say that a single
   * {@link JsonNode} is read from the stream and treated as a JSON-RPC request. All responses are
   * written to the given {@link OutputStream}.
   *
   * @param ips the {@link InputStream}
   * @param ops the {@link OutputStream}
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   */
  public void handle(HttpServletRequest request, HttpServletResponse response)
      throws JsonParseException, JsonMappingException, IOException {

    // set response type
    response.setContentType(JSONRPC_RESPONSE_CONTENT_TYPE);

    // setup streams
    InputStream input = null;
    OutputStream output = response.getOutputStream();

    // POST
    if (request.getMethod().equals("POST")) {
      input = request.getInputStream();

      // GET
    } else if (request.getMethod().equals("GET")) {

      // get parameters
      String method = request.getParameter("method");
      String id = request.getParameter("id");
      String params =
          URLDecoder.decode(new String(Base64.decode(request.getParameter("params"))), "UTF-8");

      // create full RPC request
      StringBuilder buff = new StringBuilder();
      buff.append("{ ")
          .append("\"id\": \"")
          .append(id)
          .append("\", ")
          .append("\"method\": \"")
          .append(method)
          .append("\", ")
          .append("\"params\": ")
          .append(params)
          .append(" ")
          .append("}");

      // setup stream to byte array
      input = new ByteArrayInputStream(buff.toString().getBytes());

      // invalid request
    } else {
      throw new IOException("Invalid request method, only POST and GET is supported");
    }

    // service the request
    handleNode(mapper.readValue(input, JsonNode.class), output);
  }
Example #3
0
 /**
  * Handles a single request from the given {@link InputStream}, that is to say that a single
  * {@link JsonNode} is read from the stream and treated as a JSON-RPC request. All responses are
  * written to the given {@link OutputStream}.
  *
  * @param ips the {@link InputStream}
  * @param ops the {@link OutputStream}
  * @throws IOException on error
  */
 public void handle(InputStream ips, OutputStream ops) throws IOException {
   handleNode(
       mapper.readTree(new NoCloseInputStream(ips)),
       new com.googlecode.jsonrpc4j.OutputStreamWrapper.BasicOutputStreamWrapper(mapper, ops));
 }
Example #4
0
 /**
  * Handles a single request from the given {@link InputStream}, that is to say that a single
  * {@link JsonNode} is read from the stream and treated as a JSON-RPC request. All responses are
  * written to the given {@link OutputStreamWrapper}.
  *
  * @param ips the {@link InputStream}
  * @param opsw the {@link OutputStreamWrapper}
  * @throws IOException on error
  */
 public void handle(InputStream ips, OutputStreamWrapper opsw) throws IOException {
   handleNode(mapper.readTree(new NoCloseInputStream(ips)), opsw);
 }
Example #5
0
 /**
  * Handles a single request from the given {@link InputStream}, that is to say that a single
  * {@link JsonNode} is read from the stream and treated as a JSON-RPC request. All responses are
  * written to the given {@link OutputStream}.
  *
  * @param ips the {@link InputStream}
  * @param ops the {@link OutputStream}
  * @throws JsonParseException
  * @throws JsonMappingException
  * @throws IOException
  */
 public void handle(InputStream ips, OutputStream ops)
     throws JsonParseException, JsonMappingException, IOException {
   handleNode(mapper.readValue(ips, JsonNode.class), ops);
 }