@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_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 testParseRpcRequestStringNumericId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":\"234234\"}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("\"234234\""); }
@SuppressWarnings("unchecked") private JsonRpcResponse dispatchJsonRpcRequest(JsonRpcRequest rpcRequest, OrangeContext cxt) { JsonRpcResponse jsonResponse = new JsonRpcResponse(rpcRequest.getId(), JsonNull.INSTANCE, JsonNull.INSTANCE, HttpServletResponse.SC_OK); try { ServiceMethodHandler handler = handlers.getMethodHandler(rpcRequest.getMethod()); Message innerRequest = convertJsonToProtobuf(handler, rpcRequest); JsonElement idElement = rpcRequest.getId(); if (idElement == null) { jsonResponse.setId(new JsonPrimitive(-1)); } Message innerResponse = invokeHandlerChain(rpcRequest.getMethod(), handler, innerRequest, cxt); jsonResponse.setResult(ProtobufUtil.protobufToJson(innerResponse)); } catch (RpcCallException rpcEx) { logger.debug("Error processing request", rpcEx); jsonResponse.setError(rpcEx.toJson()); jsonResponse.setStatusCode(rpcEx.getCategory().getHttpStatus()); } catch (Exception ex) { logger.warn("Error processing request", ex); if (ex.getMessage() != null) { jsonResponse.setError(new JsonPrimitive(ex.getMessage())); } jsonResponse.setStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return jsonResponse; }
methodName = rpcRequest.getMethod(); context.getRpcOriginMethod()); startTime = methodTimer.start(); context.setCorrelationId(rpcRequest.getIdAsString()); JsonRpcResponse finalResponse = dispatchJsonRpcRequest(rpcRequest, context);
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(); }
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); }
@SuppressWarnings("unchecked") private Message convertJsonToProtobuf(ServiceMethodHandler handler, JsonRpcRequest rpcRequest) throws RpcCallException { try { Class<?> requestKlass = findSubClassParameterType(handler, 0); return ProtobufUtil.jsonToProtobuf(rpcRequest.getParams(), (Class<? extends Message>) requestKlass); } catch (ClassNotFoundException ex) { throw new IllegalStateException("Reflection for handler " + handler.getClass() + " failed"); } catch (RuntimeException ex) { throw new RpcCallException(RpcCallException.Category.BadRequest, "Invalid request"); } }
@Test public void getIdAsString_NullId_Null() { // given JsonRpcRequest req = new JsonRpcRequest(null, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); }
@Test public void testParseRpcRequestStringId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":\"dead-beef\"}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("\"dead-beef\""); }
@Test public void getIdAsString_NullJson_Null() { // given JsonRpcRequest req = new JsonRpcRequest(JsonNull.INSTANCE, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); } }
@Test public void testParseRpcRequestNumericId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":123456789}"; JsonRpcRequest request = servlet.parseRpcRequest(input); JsonElement idElement = request.getId(); assertThat(idElement.toString()).isEqualTo("123456789"); }
@Test public void testParseRpcRequestNoId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}]}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isNull(); }
@Test public void testParseRpcRequestNullId() { String input = "{\"service\":\"x\",\"method\":\"a\",\"params\":[{\"data\":\"\"}],\"id\":null}"; JsonRpcRequest request = servlet.parseRpcRequest(input); assertThat(request.getId()).isInstanceOf(JsonNull.class); }