Exemplo n.º 1
0
  @Override
  public void handleRequest(Transaction transaction, Request<JsonObject> request) throws Exception {

    if (demoBean == null) {
      throw new RuntimeException("Not autowired dependencies");
    }
    log.debug("Request id:" + request.getId());
    log.debug("Request method:" + request.getMethod());
    log.debug("Request params:" + request.getParams());

    transaction.sendResponse(request.getParams());
  }
  private <P, R> Response<R> sendRequestWebSocket(Request<P> request, Class<R> resultClass) {

    log.info("Req-> {}", request.toString());

    Future<Response<JsonElement>> responseFuture = null;

    if (request.getId() != null) {
      responseFuture = pendingRequests.prepareResponse(request.getId());
    }

    try {
      synchronized (wsSession) {
        wsSession.sendMessage(new TextMessage(JsonUtils.toJson(request)));
      }
    } catch (Exception e) {
      throw new KurentoException(
          "Exception while sending message '"
              + JsonUtils.toJson(request)
              + "' to websocket with native sessionId '"
              + wsSession.getId()
              + "'",
          e);
    }

    if (responseFuture == null) {
      return null;
    }

    Response<JsonElement> responseJsonObject;
    try {
      responseJsonObject = responseFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);

      log.info("<-Res {}", responseJsonObject.toString());

    } catch (InterruptedException e) {
      // TODO What to do in this case?
      throw new JsonRpcException("Interrupted while waiting for a response", e);
    } catch (ExecutionException e) {
      // TODO Is there a better way to handle this?
      throw new JsonRpcException("This exception shouldn't be thrown", e);
    } catch (TimeoutException e) {
      throw new TransportException(
          "Timeout of "
              + TIMEOUT
              + " milliseconds waiting from response to request with id:"
              + request.getId(),
          e);
    }

    return MessageUtils.convertResponse(responseJsonObject, resultClass);
  }
  @Test
  public void noParamsRequestTest() {

    Request<Void> request = new Request<Void>(1, "method", null);
    request.setSessionId("xxxxxxx");

    String requestJson = request.toString();
    Assert.assertEquals(
        "{\"id\":1,\"method\":\"method\",\"jsonrpc\":\"2.0\",\"params\":{\"sessionId\":\"xxxxxxx\"}}",
        requestJson);

    log.info(requestJson);

    Request<Void> newRequest = JsonUtils.fromJsonRequest(requestJson, Void.class);

    // Assert.assertEquals(null, newRequest.getParams());
    Assert.assertEquals(newRequest.getSessionId(), "xxxxxxx");
  }
  @Test
  public void requestTest() {

    Params params = new Params();
    params.param1 = "Value1";
    params.param2 = "Value2";
    params.data = new Data();
    params.data.data1 = "XX";
    params.data.data2 = "YY";

    Request<Params> request = new Request<Params>(1, "method", params);

    String requestJson = JsonUtils.toJsonRequest(request);

    log.info(requestJson);

    Request<Params> newRequest = JsonUtils.fromJsonRequest(requestJson, Params.class);

    Assert.assertEquals(params.param1, newRequest.getParams().param1);
    Assert.assertEquals(params.param2, newRequest.getParams().param2);
    Assert.assertEquals(params.data.data1, newRequest.getParams().data.data1);
    Assert.assertEquals(params.data.data2, newRequest.getParams().data.data2);
  }
  @Test
  public void requestTest() {

    Params params = new Params();
    params.param1 = "Value1";

    Request<Params> request = new Request<Params>(1, "method", params);
    request.setSessionId("xxxxxxx");

    String requestJson = request.toString();
    Assert.assertEquals(
        "{\"id\":1,\"method\":\"method\",\"params\":{\"param1\":\"Value1\",\"sessionId\":\"xxxxxxx\"},\"jsonrpc\":\"2.0\"}",
        requestJson);

    log.info(requestJson);

    Request<Params> newRequest = JsonUtils.fromJsonRequest(requestJson, Params.class);

    Assert.assertEquals(params.param1, newRequest.getParams().param1);
    Assert.assertEquals(newRequest.getSessionId(), "xxxxxxx");
  }