JsonRpcMethod meth; if (methodInfo.isNotification) { meth = JsonRpcMethod.notification(methodInfo.name, methodInfo.parameterTypes); } else { Type genericReturnType = methodInfo.method.getGenericReturnType(); meth = JsonRpcMethod.request(methodInfo.name, returnType, responseTypeAdapter, methodInfo.parameterTypes); } else { throw new IllegalStateException("Expecting return type of CompletableFuture but was : " + genericReturnType); Map<String, JsonRpcMethod> supportedDelegateMethods = getSupportedMethods(method.getReturnType(), visitedTypes); for (JsonRpcMethod meth : supportedDelegateMethods.values()) { if (result.put(meth.getMethodName(), meth) != null) { throw new IllegalStateException("Duplicate RPC method "+meth.getMethodName()+"."); };
@Test public void testRpcMethods_01() { Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(Foo.class); assertEquals("foo/doStuff", methods.get("foo/doStuff").getMethodName()); assertEquals(String.class, methods.get("foo/doStuff").getParameterTypes()[0]); assertFalse(methods.get("foo/doStuff").isNotification()); assertEquals("foo/myNotification", methods.get("foo/myNotification").getMethodName()); assertEquals(String.class, methods.get("foo/myNotification").getParameterTypes()[0]); assertTrue(methods.get("foo/myNotification").isNotification()); assertEquals("hubba", methods.get("hubba").getMethodName()); assertEquals(String.class, methods.get("hubba").getParameterTypes()[0]); assertTrue(methods.get("hubba").isNotification()); }
JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) { type = jsonRpcMethod.getReturnType(); if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) { TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(type)); if (typeAdapter != null) return typeAdapter.fromJsonTree((JsonElement) result);
@SuppressWarnings({ "unchecked" }) @Test public void testParseEmptyList() { Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>(); supportedMethods.put("foo", JsonRpcMethod.request("foo", new TypeToken<List<? extends Entry>>() {}.getType(), new TypeToken<List<? extends Entry>>() {}.getType())); MessageJsonHandler handler = new MessageJsonHandler(supportedMethods); handler.setMethodProvider((id)->"foo"); Message message = handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + " \"result\": []}"); List<Entry> result = (List<Entry>) ((ResponseMessage)message).getResult(); Assert.assertEquals(0, result.size()); }
/** * Resolve an RPC method by name. */ public JsonRpcMethod getJsonRpcMethod(String name) { JsonRpcMethod result = supportedMethods.get(name); if (result != null) return result; else if (CANCEL_METHOD.getMethodName().equals(name)) return CANCEL_METHOD; return null; }
public static JsonRpcMethod request(String name, Type returnType, TypeAdapterFactory returnTypeAdapterFactory, Type... parameterTypes) { return new JsonRpcMethod(name, parameterTypes, returnType, returnTypeAdapterFactory, false); }
protected Type[] getParameterTypes(String method) { if (method != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(method); if (jsonRpcMethod != null) return jsonRpcMethod.getParameterTypes(); } return EMPTY_TYPE_ARRAY; }
@Test public void testParamsParsing_03() { 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"); RequestMessage message = (RequestMessage) handler.parseMessage("{" + "\"seq\":2,\n" + "\"type\":\"request\",\n" + "\"command\":\"bar\",\n" + "\"arguments\": {\"uri\": \"dummy://mymodel.mydsl\"}\n" + "}"); Assert.assertEquals(JsonObject.class, message.getParams().getClass()); }
/** * Resolve an RPC method by name. */ public JsonRpcMethod getJsonRpcMethod(String name) { JsonRpcMethod result = supportedMethods.get(name); if (result != null) return result; else if (CANCEL_METHOD.getMethodName().equals(name)) return CANCEL_METHOD; return null; }
public static JsonRpcMethod request(String name, Type returnType, Type... parameterTypes) { return new JsonRpcMethod(name, parameterTypes, returnType, null, false); }
protected Type[] getParameterTypes(String method) { if (method != null) { JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(method); if (jsonRpcMethod != null) return jsonRpcMethod.getParameterTypes(); } return EMPTY_TYPE_ARRAY; }
@Test public void testParamsParsing_03() { 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"); RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"method\":\"bar\",\n" + "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}\n" + "}"); Assert.assertEquals(JsonObject.class, message.getParams().getClass()); }
JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) { type = jsonRpcMethod.getReturnType(); if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) { TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(type)); if (typeAdapter != null) return typeAdapter.fromJsonTree((JsonElement) result);
JsonRpcMethod meth; if (methodInfo.isNotification) { meth = JsonRpcMethod.notification(methodInfo.name, methodInfo.parameterTypes); } else { Type genericReturnType = methodInfo.method.getGenericReturnType(); meth = JsonRpcMethod.request(methodInfo.name, returnType, responseTypeAdapter, methodInfo.parameterTypes); } else { throw new IllegalStateException("Expecting return type of CompletableFuture but was : " + genericReturnType); Map<String, JsonRpcMethod> supportedDelegateMethods = getSupportedMethods(method.getReturnType(), visitedTypes); for (JsonRpcMethod meth : supportedDelegateMethods.values()) { if (result.put(meth.getMethodName(), meth) != null) { throw new IllegalStateException("Duplicate RPC method "+meth.getMethodName()+"."); };
@Test public void testIssue107() { Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(StringConsumer.class); final JsonRpcMethod method = methods.get("consumer/accept"); assertEquals("consumer/accept", method.getMethodName()); assertEquals(1, method.getParameterTypes().length); assertEquals(String.class, method.getParameterTypes()[0]); assertTrue(method.isNotification()); }
/** * 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; }
public static JsonRpcMethod request(String name, Type returnType, Type... parameterTypes) { return new JsonRpcMethod(name, parameterTypes, returnType, null, false); }
@Test public void testParamsParsing_02() { 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"); RequestMessage message = (RequestMessage) handler.parseMessage("{\"jsonrpc\":\"2.0\"," + "\"id\":\"2\",\n" + "\"method\":\"foo\",\n" + "\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}\n" + "}"); Assert.assertEquals(Location.class, message.getParams().getClass()); }
JsonRpcMethod jsonRpcMethod = handler.getJsonRpcMethod(resolvedMethod); if (jsonRpcMethod != null) { type = jsonRpcMethod.getReturnType(); if (jsonRpcMethod.getReturnTypeAdapterFactory() != null) { TypeAdapter<?> typeAdapter = jsonRpcMethod.getReturnTypeAdapterFactory().create(gson, TypeToken.get(type)); try { if (typeAdapter != null)
@Test public void testRpcMethods_02() { Map<String, JsonRpcMethod> methods = ServiceEndpoints.getSupportedMethods(Bar.class); final JsonRpcMethod requestMethod = methods.get("bar/doStuff2"); assertEquals("bar/doStuff2", requestMethod.getMethodName()); assertEquals(2, requestMethod.getParameterTypes().length); assertEquals(String.class, requestMethod.getParameterTypes()[0]); assertEquals(Integer.class, requestMethod.getParameterTypes()[1]); assertFalse(requestMethod.isNotification()); final JsonRpcMethod notificationMethod = methods.get("bar/myNotification2"); assertEquals("bar/myNotification2", notificationMethod.getMethodName()); assertEquals(2, notificationMethod.getParameterTypes().length); assertEquals(String.class, notificationMethod.getParameterTypes()[0]); assertEquals(Integer.class, notificationMethod.getParameterTypes()[1]); assertTrue(notificationMethod.isNotification()); final JsonRpcMethod delegateMethod = methods.get("hubba"); assertEquals("hubba", delegateMethod.getMethodName()); assertEquals(2, delegateMethod.getParameterTypes().length); assertEquals(String.class, delegateMethod.getParameterTypes()[0]); assertEquals(Integer.class, delegateMethod.getParameterTypes()[1]); assertTrue(delegateMethod.isNotification()); }