- 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 RegistrationCommandRunnerImpl<T>(
- RegistrationManager m, MultiAccountManager c, JsonRpcRegistrationCommand<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>(
- MultiAccountManager 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
- ) throws JsonRpcException {
- final Object[] result = {null};
- final JsonWriter commandJsonWriter = s -> {
- if (result[0] != null) {
- throw new AssertionError("Command may only write one json result");
- }
-
- result[0] = s;
- };
-
- try {
- parseParamsAndRunCommand(objectMapper, params, commandJsonWriter, command);
- } catch (JsonMappingException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
- e.getMessage(),
- null));
- } catch (UserErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR,
- e.getMessage(),
- getErrorDataNode(objectMapper, result)));
- } catch (IOErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR,
- e.getMessage(),
- getErrorDataNode(objectMapper, result)));
- } catch (UntrustedKeyErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_ERROR,
- e.getMessage(),
- getErrorDataNode(objectMapper, result)));
- } catch (Throwable e) {
- logger.error("Command execution failed", e);
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
- e.getMessage(),
- getErrorDataNode(objectMapper, result)));
- }
-
- Object output = result[0] == null ? Map.of() : result[0];
- return objectMapper.valueToTree(output);
- }
-
- private JsonNode getErrorDataNode(final ObjectMapper objectMapper, final Object[] result) {
- if (result[0] == null) {
- return null;
- }
- return objectMapper.valueToTree(Map.of("response", result[0]));
- }
-
- private <T> void parseParamsAndRunCommand(
- final ObjectMapper objectMapper,
- final TreeNode params,
- final JsonWriter jsonWriter,
- final CommandRunner<T> command
- ) throws CommandException, JsonMappingException {
- T requestParams = null;
- final var requestType = command.getRequestType();
- if (params != null && requestType != null) {
- try {
- requestParams = objectMapper.readValue(objectMapper.treeAsTokens(params), requestType);
- } catch (JsonMappingException e) {
- throw e;
- } catch (IOException e) {
- throw new AssertionError(e);
- }
- }
- command.handleCommand(requestParams, jsonWriter);
- }
-