protected JsonRpcRequest parseRpcRequest(String jsonRequestString) throws IllegalArgumentException { JsonObject jsonRpcRequest = null; try { jsonRpcRequest = new JsonParser().parse(jsonRequestString).getAsJsonObject(); } catch (Exception ex) { throw new IllegalArgumentException(ex.getMessage(), ex); } JsonElement methodElement = jsonRpcRequest.get(METHOD_FIELD); if (methodElement == null || methodElement.getAsString().isEmpty()) { throw new IllegalArgumentException("Missing method name"); } if (! handlers.hasMethodHandler(methodElement.getAsString())) { throw new IllegalArgumentException("No handler registered for method '" + methodElement.getAsString() + "'"); } JsonArray paramsArray = jsonRpcRequest.getAsJsonArray(PARAMS_FIELD); JsonElement idElement = jsonRpcRequest.get(JsonRpcRequest.ID_FIELD); return new JsonRpcRequest(idElement, methodElement.getAsString(), paramsArray); }
@Test public void getIdAsString_SomeIdAsNumber_TheNumberAsString() { // given JsonElement jsonElement = new JsonPrimitive(20); JsonRpcRequest req = new JsonRpcRequest(jsonElement, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNotNull(); assertThat(result).isEqualTo("20"); }
@Test public void gettersAndSetters() { JsonArray array = new JsonArray(); JsonRpcRequest req = new JsonRpcRequest(null, "mefod", array); assertThat(req.getMethod()).isEqualTo("mefod"); assertThat(req.getParams()).isEqualTo(array); }
@Test public void getIdAsString_NullJson_Null() { // given JsonRpcRequest req = new JsonRpcRequest(JsonNull.INSTANCE, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); } }
public String callSynchronous(JsonArray params, OrangeContext orangeContext) throws RpcCallException { HttpClientWrapper clientWrapper = loadBalancer.getHttpClientWrapper(); HttpRequestWrapper balancedPost = clientWrapper.createHttpPost(this); //set custom headers if (orangeContext != null) { orangeContext.getProperties().forEach(balancedPost::setHeader); } balancedPost.setHeader("Content-type", TYPE_JSON); //TODO: fix: Temporary workaround below until go services are more http compliant balancedPost.setHeader("Connection", "close"); JsonRpcRequest jsonRequest = new JsonRpcRequest(null, methodName, params); String json = jsonRequest.toString(); balancedPost.setContentProvider(new StringContentProvider(json)); logger.debug("Sending request of size {}", json.length()); ContentResponse rpcResponse = clientWrapper.execute(balancedPost, new JsonRpcCallExceptionDecoder(), orangeContext); String rawResponse = rpcResponse.getContentAsString(); logger.debug("Json response from the service: {}", rawResponse); return JsonRpcResponse.fromString(rawResponse).getResult().getAsString(); }
@Test public void getIdAsString_NullId_Null() { // given JsonRpcRequest req = new JsonRpcRequest(null, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); }