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 responseTest() { Data data = new Data(); data.data1 = "Value1"; data.data2 = "Value2"; Response<Data> request = new Response<Data>(1, data); String requestJson = JsonUtils.toJsonResponse(request); log.info(requestJson); Response<Data> newRequest = JsonUtils.fromJsonResponse(requestJson, Data.class); Assert.assertEquals(data.data1, newRequest.getResult().data1); Assert.assertEquals(data.data2, newRequest.getResult().data2); }
@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 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 noResultResponseTest() { Response<Void> response = new Response<Void>(1); response.setSessionId("xxxxxxx"); String responseJson = response.toString(); JsonParser parser = new JsonParser(); JsonObject expected = (JsonObject) parser.parse("{\"id\":1,\"result\":{\"sessionId\":\"xxxxxxx\"},\"jsonrpc\":\"2.0\"}"); JsonObject result = (JsonObject) parser.parse(responseJson); Assert.assertEquals(expected, result); log.info(responseJson); Response<Void> newResponse = JsonUtils.fromJsonResponse(responseJson, Void.class); // Assert.assertEquals(null, newResponse.getResult()); Assert.assertEquals(newResponse.getSessionId(), "xxxxxxx"); }
@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"); }
@Test public void responseTest() { Data data = new Data(); data.data1 = "Value1"; Response<Data> response = new Response<Data>(1, data); response.setSessionId("xxxxxxx"); String responseJson = response.toString(); Assert.assertEquals( "{\"id\":1,\"result\":{\"data1\":\"Value1\",\"sessionId\":\"xxxxxxx\"},\"jsonrpc\":\"2.0\"}", responseJson); log.info(responseJson); Response<Data> newResponse = JsonUtils.fromJsonResponse(responseJson, Data.class); Assert.assertEquals(data.data1, newResponse.getResult().data1); Assert.assertEquals(newResponse.getSessionId(), "xxxxxxx"); }