-
- var command = Commands.getCommand(method, commandOutputWriter);
- if (!(command instanceof JsonRpcCommand)) {
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.METHOD_NOT_FOUND,
- "Method not implemented",
- null));
- }
-
- try {
- parseParamsAndRunCommand(m, objectMapper, params, (JsonRpcCommand<?>) 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(), null));
- } catch (IOErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR, e.getMessage(), null));
- } catch (UntrustedKeyErrorException e) {
- throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_ERROR, e.getMessage(), null));
- } catch (Throwable e) {
- logger.error("Command execution failed", e);
- throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
- e.getMessage(),
- null));
- }
-
- Object output = result[0] == null ? Map.of() : result[0];
- return objectMapper.valueToTree(output);
- }
-
- private <T> void parseParamsAndRunCommand(
- final Manager m, final ObjectMapper objectMapper, final TreeNode params, final JsonRpcCommand<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, m);
- }
-
- private Thread receiveMessages(
- JsonWriter jsonWriter, Manager m, boolean ignoreAttachments
- ) {
- final var thread = new Thread(() -> {
- while (!Thread.interrupted()) {
- try {
- final var receiveMessageHandler = new JsonReceiveMessageHandler(m, jsonWriter);
- m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, receiveMessageHandler);
- break;
- } catch (IOException e) {
- logger.warn("Receiving messages failed, retrying", e);
- } catch (InterruptedException e) {
- break;
- }
- }
- });
-
- thread.start();
-
- return thread;