private void readRpcError(String data) { try { JsonRpcResponse json = JsonRpcResponse.fromString(data); this.errorMessage = json.getError().toString(); } catch (Exception ex) { logger.warn("Caught exception parsing response"); } }
public JsonRpcResponse(JsonElement id, JsonElement result, JsonElement error, int statusCode) { setId(id); setResult(result); setError(error); setStatusCode(statusCode); }
@Test public void verifyParseResponse() { String input = "{\"error\":\"errror\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getError().getAsString()).isEqualTo("errror"); assertThat(response.getResult().toString()).isEqualTo("{\"foo\":\"bar\"}"); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
@Test public void testJsonResponseWithRpcException() { String jsonRpcException = "{\"category\":400,\"message\":\"You fool!\"," + "\"source\":\"com.sixt.service.foobar\",\"code\":\"SERVICE_PROTOBUF_ENUM\"," + "\"data\":\"my data\",\"retriable\":true}"; String input = "{\"error\":" + jsonRpcException + ",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getError()).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void verifyToString() { JsonRpcResponse response = new JsonRpcResponse(new JsonPrimitive(42), JsonNull.INSTANCE, new JsonPrimitive("none"), 200); assertThat(response.getId()).isEqualTo(new JsonPrimitive(42)); assertThat(response.toString()).contains("[id=42,result=null,error=\"none\"]"); }
@Test public void testJsonArrayError() { String input = "{\"error\":\"[{},{}]\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }
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(); }
writeResponse(resp, finalResponse.getStatusCode(), finalResponse.toJson().toString());
public static JsonRpcResponse fromString(String rawResponse) { JsonParser parser = new JsonParser(); JsonObject response = parser.parse(rawResponse).getAsJsonObject(); JsonElement id = response.get("id"); JsonElement errorElement = response.get("error"); int responseStatus = HttpServletResponse.SC_OK; String error; if (! (errorElement instanceof JsonNull)) { if (errorElement instanceof JsonObject) { error = errorElement.toString(); // try parsing it into RpcCallException to get the HttpStatus from there RpcCallException rpcEx = RpcCallException.fromJson(error); if (rpcEx != null) { responseStatus = rpcEx.getCategory().getHttpStatus(); JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); } } error = errorElement.getAsString(); if (StringUtils.isNotBlank(error)) { responseStatus = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; } } JsonElement resultElement = response.get("result"); return new JsonRpcResponse(id, resultElement == null ? JsonNull.INSTANCE : resultElement, errorElement, responseStatus); }
@Test public void testJsonResponseWithSuccessCode() { String input = "{\"error\":\"\",\"result\":{\"foo\":\"bar\"}}"; JsonRpcResponse response = JsonRpcResponse.fromString(input); assertThat(response.getStatusCode()).isEqualTo(HttpServletResponse.SC_OK); }
@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; }