+ final ObjectMapper objectMapper, final String method, ContainerNode<?> params
+ ) throws JsonRpcException {
+ var command = getCommand(method);
+ // TODO implement register, verify, link
+ if (c != null && command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) {
+ return runCommand(objectMapper, params, new MultiCommandRunnerImpl<>(c, jsonRpcCommand));
+ }
+ if (command instanceof JsonRpcSingleCommand<?> jsonRpcCommand) {
+ if (m != null) {
+ return runCommand(objectMapper, params, new CommandRunnerImpl<>(m, jsonRpcCommand));
+ }
+
+ if (params.has("account")) {
+ Manager manager = c.getManager(params.get("account").asText());
+ if (manager != null) {
+ return runCommand(objectMapper, params, new CommandRunnerImpl<>(manager, jsonRpcCommand));
+ }
+ } else {
+ throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_PARAMS,
+ "Method requires valid account parameter",
+ null));
+ }
+ }
+
+ throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.METHOD_NOT_FOUND,
+ "Method not implemented",
+ null));
+ }
+
+ private Command getCommand(final String method) {
+ if ("subscribeReceive".equals(method)) {
+ return new SubscribeReceiveCommand();
+ }
+ if ("unsubscribeReceive".equals(method)) {
+ return new UnsubscribeReceiveCommand();
+ }
+ return Commands.getCommand(method);
+ }
+
+ private record CommandRunnerImpl<T>(Manager m, JsonRpcSingleCommand<T> command) implements CommandRunner<T> {
+
+ @Override
+ public void handleCommand(final T request, final JsonWriter jsonWriter) throws CommandException {
+ command.handleCommand(request, m, jsonWriter);
+ }
+
+ @Override
+ public TypeReference<T> getRequestType() {
+ return command.getRequestType();
+ }
+ }
+
+ private record MultiCommandRunnerImpl<T>(
+ SignalCreator c, JsonRpcMultiCommand<T> command
+ ) implements CommandRunner<T> {
+
+ @Override
+ public void handleCommand(final T request, final JsonWriter jsonWriter) throws CommandException {
+ command.handleCommand(request, c, jsonWriter);
+ }
+
+ @Override
+ public TypeReference<T> getRequestType() {
+ return command.getRequestType();
+ }
+ }
+
+ interface CommandRunner<T> {
+
+ void handleCommand(T request, JsonWriter jsonWriter) throws CommandException;
+
+ TypeReference<T> getRequestType();
+ }
+
+ private JsonNode runCommand(
+ final ObjectMapper objectMapper, final ContainerNode<?> params, final CommandRunner<?> command