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 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); } } }
/** * 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; }
/** * 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; }
out.value(notificationMessage.getMethod()); out.name("params"); Object params = notificationMessage.getParams(); if (params == null) writeNullValue(out);
out.value(notificationMessage.getMethod()); out.name("params"); Object params = notificationMessage.getParams(); if (params == null) writeNullValue(out);
@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 testNotification() { TestEndpoint endp = new TestEndpoint(); TestMessageConsumer consumer = new TestMessageConsumer(); RemoteEndpoint endpoint = new DebugRemoteEndpoint(consumer, endp); endpoint.consume(new NotificationMessage() {{ setMethod("foo"); 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())); 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); }); } }
@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()); }