@Override public void consume(Message message) throws MessageIssueException, JsonRpcException { List<MessageIssue> result = new ArrayList<>(); try { validate(message, result, new LinkedList<>(), new LinkedList<>()); } catch (Exception e) { LOG.log(Level.SEVERE, "Error during message validation: " + e.getMessage(), e); result.add(new MessageIssue("Message validation failed, please check the logs of the remote endpoint.", ResponseErrorCode.InvalidParams.getValue())); } if (!result.isEmpty()) { // Sort the messages in order to get a stable order (otherwise it depends on the JVM's reflection implementation) Collections.sort(result, (issue1, issue2) -> issue1.getText().compareTo(issue2.getText())); throw new MessageIssueException(message, result); } else if (delegate != null) { delegate.consume(message); } }
protected void handleRequestIssues(RequestMessage requestMessage, List<MessageIssue> issues) { ResponseError errorObject = new ResponseError(); if (issues.size() == 1) { MessageIssue issue = issues.get(0); errorObject.setMessage(issue.getText()); errorObject.setCode(issue.getIssueCode()); errorObject.setData(issue.getCause()); } else { if (requestMessage.getMethod() != null) errorObject.setMessage("Multiple issues were found in '" + requestMessage.getMethod() + "' request."); else errorObject.setMessage("Multiple issues were found in request."); errorObject.setCode(ResponseErrorCode.InvalidRequest); errorObject.setData(issues); } out.consume(createErrorResponseMessage(requestMessage, errorObject)); }
public Message parseMessage(Reader input) throws JsonParseException { JsonReader jsonReader = new JsonReader(input); Message message = gson.fromJson(jsonReader, Message.class); if (message != null) { // Check whether the input has been fully consumed try { if (jsonReader.peek() != JsonToken.END_DOCUMENT) { MessageIssue issue = new MessageIssue("JSON document was not fully consumed.", ResponseErrorCode.ParseError.getValue()); throw new MessageIssueException(message, issue); } } catch (MalformedJsonException e) { MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), e); throw new MessageIssueException(message, issue); } catch (IOException e) { throw new JsonIOException(e); } } return message; }
@Override public String toString() { return getText(); }
@Test public void testParseErrorRequest() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Void>() {}.getType(), new TypeToken<Location>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String input = "{" + "\"seq\":2,\n" + "\"type\":\"request\",\n" + "\"command\":\"foo\"\n" + "\"arguments\": \"ERROR HERE - a string where an object is expected\",\n" + "}"; try { handler.parseMessage(input); fail("Should have had a parse error"); } catch (MessageIssueException e) { // Make sure the message parsed ok up until the parse error DebugRequestMessage rpcMessage = (DebugRequestMessage)e.getRpcMessage(); Assert.assertEquals("2", rpcMessage.getId()); Assert.assertEquals("foo", rpcMessage.getMethod()); // check there is an underlying error MessageIssue messageIssue = e.getIssues().get(0); Assert.assertNotNull(messageIssue.getCause()); } }
protected void handleRequestIssues(RequestMessage requestMessage, List<MessageIssue> issues) { ResponseError errorObject = new ResponseError(); if (issues.size() == 1) { MessageIssue issue = issues.get(0); errorObject.setMessage(issue.getText()); errorObject.setCode(issue.getIssueCode()); errorObject.setData(issue.getCause()); } else { if (requestMessage.getMethod() != null) errorObject.setMessage("Multiple issues were found in '" + requestMessage.getMethod() + "' request."); else errorObject.setMessage("Multiple issues were found in request."); errorObject.setCode(ResponseErrorCode.InvalidRequest); errorObject.setData(issues); } out.consume(createErrorResponseMessage(requestMessage, errorObject)); }
public Message parseMessage(Reader input) throws JsonParseException { JsonReader jsonReader = new JsonReader(input); Message message = gson.fromJson(jsonReader, Message.class); if (message != null) { // Check whether the input has been fully consumed try { if (jsonReader.peek() != JsonToken.END_DOCUMENT) { MessageIssue issue = new MessageIssue("JSON document was not fully consumed.", ResponseErrorCode.ParseError.getValue()); throw new MessageIssueException(message, issue); } } catch (MalformedJsonException e) { MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), e); throw new MessageIssueException(message, issue); } catch (IOException e) { throw new JsonIOException(e); } } return message; }
@Override public String toString() { return getText(); }
@Test public void testParseSyntaxErrorRequest() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Void>() {}.getType(), new TypeToken<Location>() {}.getType())); DebugMessageJsonHandler handler = new DebugMessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String input = "{" + "\"seq\":2,\n" + "\"type\":\"request\",\n" + "\"command\":\"foo\"\n" + "\"arguments\": \"ERROR HERE - an unterminated string,\n" + "}"; try { handler.parseMessage(input); fail("Should have had a parse error"); } catch (MessageIssueException e) { // Make sure the message parsed ok up until the parse error DebugRequestMessage rpcMessage = (DebugRequestMessage)e.getRpcMessage(); Assert.assertEquals("2", rpcMessage.getId()); Assert.assertEquals("foo", rpcMessage.getMethod()); // check there is an underlying error MessageIssue messageIssue = e.getIssues().get(0); Assert.assertNotNull(messageIssue.getCause()); } } }
_builder.newLine(); final MessageIssue issue = _gson.<MessageIssue>fromJson(_builder.toString(), MessageIssue.class); Assert.assertEquals("Howdy!", issue.getText()); Assert.assertEquals(1234, issue.getIssueCode()); Assert.assertEquals("Foo", issue.getCause().getMessage()); Assert.assertEquals("Bar", issue.getCause().getCause().getMessage());
issues.add(new MessageIssue("An element of the message has a direct or indirect reference to itself." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); Object element = iter.next(); if (element == null) { issues.add(new MessageIssue("Lists must not contain null references." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); validate(either.getRight(), issues, objectStack, accessorStack); } else { issues.add(new MessageIssue("An Either instance must not be empty." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); Object value = method.invoke(object); if (value == null && method.getAnnotation(NonNull.class) != null) { issues.add(new MessageIssue("The accessor '" + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()' must return a non-null value." + " Path: " + createPathString(accessorStack),
@Override public void consume(Message message) throws MessageIssueException, JsonRpcException { List<MessageIssue> result = new ArrayList<>(); try { validate(message, result, new LinkedList<>(), new LinkedList<>()); } catch (Exception e) { LOG.log(Level.SEVERE, "Error during message validation: " + e.getMessage(), e); result.add(new MessageIssue("Message validation failed, please check the logs of the remote endpoint.", ResponseErrorCode.InvalidParams.getValue())); } if (!result.isEmpty()) { // Sort the messages in order to get a stable order (otherwise it depends on the JVM's reflection implementation) Collections.sort(result, (issue1, issue2) -> issue1.getText().compareTo(issue2.getText())); throw new MessageIssueException(message, result); } else if (delegate != null) { delegate.consume(message); } }
@Override public String getMessage() { return issues.stream().map(issue -> issue.getText()).collect(Collectors.joining("\n")); }
issues.add(new MessageIssue("An element of the message has a direct or indirect reference to itself." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); Object element = iter.next(); if (element == null) { issues.add(new MessageIssue("Lists must not contain null references." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); validate(either.getRight(), issues, objectStack, accessorStack); } else { issues.add(new MessageIssue("An Either instance must not be empty." + " Path: " + createPathString(accessorStack), ResponseErrorCode.InvalidParams.getValue())); Object value = method.invoke(object); if (value == null && method.getAnnotation(NonNull.class) != null) { issues.add(new MessageIssue("The accessor '" + method.getDeclaringClass().getSimpleName() + "." + method.getName() + "()' must return a non-null value." + " Path: " + createPathString(accessorStack),
@Override public String getMessage() { return issues.stream().map(issue -> issue.getText()).collect(Collectors.joining("\n")); }
MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception); throw new MessageIssueException(message, issue); } else {
MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception); throw new MessageIssueException(resultMessage, issue); } else {
MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception); throw new MessageIssueException(message, issue); } else {