protected NotificationMessage createNotificationMessage(String method, Object parameter) { NotificationMessage notificationMessage = new NotificationMessage(); notificationMessage.setJsonrpc(MessageConstants.JSONRPC_VERSION); notificationMessage.setMethod(method); notificationMessage.setParams(parameter); return notificationMessage; }
protected void handleNotification(NotificationMessage notificationMessage) { if (!handleCancellation(notificationMessage)) { // Forward the notification to the local endpoint try { localEndpoint.notify(notificationMessage.getMethod(), notificationMessage.getParams()); } catch (Exception exception) { LOG.log(Level.WARNING, "Notification threw an exception: " + notificationMessage, exception); } } }
@Override public boolean equals(final Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; if (!super.equals(obj)) return false; DebugNotificationMessage other = (DebugNotificationMessage) obj; if (this.id == null) { if (other.id != null) return false; } else if (!this.id.equals(other.id)) return false; return true; }
public void notify(String method, Object parameter) { notifications.add(init(new NotificationMessage(), it -> { it.setMethod(method); it.setParams(parameter); })); }
@Test public void testNotification() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new RemoteEndpoint(consumer, endp); endpoint.consume(init(new NotificationMessage(), it -> { it.setMethod("foo"); it.setParams("myparam"); })); NotificationMessage notificationMessage = endp.notifications.get(0); assertEquals("foo", notificationMessage.getMethod()); assertEquals("myparam", notificationMessage.getParams()); assertTrue(consumer.messages.isEmpty()); }
@Test public void testNotification_AllOrders() { 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[] properties = new String[] { "\"seq\":2", "\"type\":\"event\"", "\"event\":\"foo\"", "\"body\": {\"uri\": \"dummy://mymodel.mydsl\"}" }; testAllPermutations(properties, json -> { NotificationMessage message = (NotificationMessage) handler.parseMessage(json); Object params = message.getParams(); Class<? extends Object> class1 = params.getClass(); Assert.assertEquals(Location.class, class1); Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri); }); }
@Test public void testSerializeImmutableList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); List<Object> list = new ArrayList<>(); list.add("a"); list.add("b"); message.setParams(Collections.unmodifiableList(list)); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[\"a\",\"b\"]}", json); }
@Test public void testNotification_AllOrders() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<Void>() {}.getType(), new TypeToken<Location>() { }.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id) -> "foo"); String[] properties = new String[] { "\"jsonrpc\":\"2.0\"", "\"method\":\"foo\"", "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}" }; testAllPermutations(properties, json -> { NotificationMessage message = (NotificationMessage) handler.parseMessage(json); Object params = message.getParams(); Class<? extends Object> class1 = params.getClass(); Assert.assertEquals(Location.class, class1); Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri); }); } }
protected NotificationMessage createNotificationMessage(String method, Object parameter) { NotificationMessage notificationMessage = new NotificationMessage(); notificationMessage.setJsonrpc(MessageConstants.JSONRPC_VERSION); notificationMessage.setMethod(method); notificationMessage.setParams(parameter); return notificationMessage; }
@Test public void testSkipJsonElement() { final AtomicBoolean result = new AtomicBoolean(false); ReflectiveMessageValidator validator = new ReflectiveMessageValidator((message) -> { result.set(true); }); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); message.setParams(new JsonObject()); validator.consume(message); Assert.assertTrue(result.get()); }
protected void handleNotification(NotificationMessage notificationMessage) { if (!handleCancellation(notificationMessage)) { // Forward the notification to the local endpoint try { localEndpoint.notify(notificationMessage.getMethod(), notificationMessage.getParams()); } catch (Exception exception) { LOG.log(Level.WARNING, "Notification threw an exception: " + notificationMessage, exception); } } }
protected Message createMessage(String jsonrpc, Either<String, Number> id, String method, Object params, Object responseResult, ResponseError responseError) throws JsonParseException { if (id != null && method != null) { RequestMessage message = new RequestMessage(); message.setJsonrpc(jsonrpc); message.setRawId(id); message.setMethod(method); message.setParams(params); return message; } else if (id != null) { ResponseMessage message = new ResponseMessage(); message.setJsonrpc(jsonrpc); message.setRawId(id); if (responseError != null) message.setError(responseError); else message.setResult(responseResult); return message; } else if (method != null) { NotificationMessage message = new NotificationMessage(); message.setJsonrpc(jsonrpc); message.setMethod(method); message.setParams(params); return message; } else { throw new JsonParseException("Unable to identify the input message."); } }
@Test public void testSerializeEmptyList() { MessageJsonHandler handler = new MessageJsonHandler(Collections.emptyMap()); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); message.setParams(Collections.EMPTY_LIST); String json = handler.serialize(message); Assert.assertEquals("{\"jsonrpc\":\"2.0\",\"method\":\"foo\",\"params\":[]}", json); }
/** * Cancellation is handled inside this class and not forwarded to the local endpoint. * * @return {@code true} if the given message is a cancellation notification, * {@code false} if it can be handled by the local endpoint */ protected boolean handleCancellation(NotificationMessage notificationMessage) { if (MessageJsonHandler.CANCEL_METHOD.getMethodName().equals(notificationMessage.getMethod())) { Object cancelParams = notificationMessage.getParams(); if (cancelParams != null) { if (cancelParams instanceof CancelParams) { synchronized (receivedRequestMap) { String id = ((CancelParams) cancelParams).getId(); CompletableFuture<?> future = receivedRequestMap.get(id); if (future != null) future.cancel(true); else LOG.warning("Unmatched cancel notification for request id " + id); } return true; } else { LOG.warning("Cancellation support is disabled, since the '" + MessageJsonHandler.CANCEL_METHOD.getMethodName() + "' method has been registered explicitly."); } } else { LOG.warning("Missing 'params' attribute of cancel notification."); } } return false; }
protected Message createMessage(String jsonrpc, Either<String, Number> id, String method, Object params, Object responseResult, ResponseError responseError) throws JsonParseException { if (id != null && method != null) { RequestMessage message = new RequestMessage(); message.setJsonrpc(jsonrpc); message.setRawId(id); message.setMethod(method); message.setParams(params); return message; } else if (id != null) { ResponseMessage message = new ResponseMessage(); message.setJsonrpc(jsonrpc); message.setRawId(id); if (responseError != null) message.setError(responseError); else message.setResult(responseResult); return message; } else if (method != null) { NotificationMessage message = new NotificationMessage(); message.setJsonrpc(jsonrpc); message.setMethod(method); message.setParams(params); return message; } else { throw new JsonParseException("Unable to identify the input message."); } }
@Test public void testNoViolation() { ReflectiveMessageValidator validator = new ReflectiveMessageValidator(); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); Foo foo = new Foo(); foo.nonNullString = "test"; foo.nested = new Foo() {{ nonNullString = "something"; }}; message.setParams(foo); validator.consume(message); }
/** * Cancellation is handled inside this class and not forwarded to the local endpoint. * * @return {@code true} if the given message is a cancellation notification, * {@code false} if it can be handled by the local endpoint */ protected boolean handleCancellation(NotificationMessage notificationMessage) { if (MessageJsonHandler.CANCEL_METHOD.getMethodName().equals(notificationMessage.getMethod())) { Object cancelParams = notificationMessage.getParams(); if (cancelParams != null) { if (cancelParams instanceof CancelParams) { synchronized (receivedRequestMap) { String id = ((CancelParams) cancelParams).getId(); CompletableFuture<?> future = receivedRequestMap.get(id); if (future != null) future.cancel(true); else LOG.warning("Unmatched cancel notification for request id " + id); } return true; } else { LOG.warning("Cancellation support is disabled, since the '" + MessageJsonHandler.CANCEL_METHOD.getMethodName() + "' method has been registered explicitly."); } } else { LOG.warning("Missing 'params' attribute of cancel notification."); } } return false; }
_builder.append("}"); _builder.newLine(); NotificationMessage _notificationMessage = new NotificationMessage(); final Procedure1<NotificationMessage> _function = (NotificationMessage it) -> { it.setJsonrpc("2.0"); it.setMethod(MessageMethods.TELEMETRY_EVENT); Pair<String, Double> _mappedTo = Pair.<String, Double>of("foo", Double.valueOf(12.3)); Pair<String, String> _mappedTo_1 = Pair.<String, String>of("bar", "qwertz"); it.setParams(CollectionLiterals.<String, Object>newLinkedHashMap(_mappedTo, _mappedTo_1)); }; NotificationMessage _doubleArrow = ObjectExtensions.<NotificationMessage>operator_doubleArrow(_notificationMessage, _function);
@Test public void testNonNullViolated() { ReflectiveMessageValidator validator = new ReflectiveMessageValidator(); NotificationMessage message = new NotificationMessage(); message.setMethod("foo"); message.setParams(new Foo()); try { validator.consume(message); Assert.fail(); } catch (MessageIssueException e) { Assert.assertEquals("The accessor 'Foo.getNonNullString()' must return a non-null value. Path: $.params.nonNullString", e.getMessage()); } }