mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Add JsonRpcMultiCommand
This commit is contained in:
parent
64af4914ee
commit
9c67aabebb
8 changed files with 90 additions and 26 deletions
|
@ -38,7 +38,7 @@ import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DaemonCommand implements MultiLocalCommand {
|
public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class);
|
private final static Logger logger = LoggerFactory.getLogger(DaemonCommand.class);
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
import net.sourceforge.argparse4j.inf.Namespace;
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
|
|
||||||
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.OutputType;
|
import org.asamk.signal.OutputType;
|
||||||
import org.asamk.signal.OutputWriter;
|
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
|
|
||||||
|
@ -19,10 +19,10 @@ public interface JsonRpcLocalCommand extends JsonRpcSingleCommand<Map<String, Ob
|
||||||
}
|
}
|
||||||
|
|
||||||
default void handleCommand(
|
default void handleCommand(
|
||||||
Map<String, Object> request, Manager m, OutputWriter outputWriter
|
Map<String, Object> request, Manager m, JsonWriter jsonWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
Namespace commandNamespace = new JsonRpcNamespace(request == null ? Map.of() : request);
|
Namespace commandNamespace = new JsonRpcNamespace(request == null ? Map.of() : request);
|
||||||
handleCommand(commandNamespace, m, outputWriter);
|
handleCommand(commandNamespace, m, jsonWriter);
|
||||||
}
|
}
|
||||||
|
|
||||||
default List<OutputType> getSupportedOutputTypes() {
|
default List<OutputType> getSupportedOutputTypes() {
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.asamk.signal.commands;
|
||||||
|
|
||||||
|
import org.asamk.signal.JsonWriter;
|
||||||
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
|
||||||
|
public interface JsonRpcMultiCommand<T> extends JsonRpcCommand<T> {
|
||||||
|
|
||||||
|
void handleCommand(T request, SignalCreator c, JsonWriter jsonWriter) throws CommandException;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package org.asamk.signal.commands;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
|
||||||
|
import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
|
|
||||||
|
import org.asamk.signal.JsonWriter;
|
||||||
|
import org.asamk.signal.OutputType;
|
||||||
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface JsonRpcMultiLocalCommand extends JsonRpcMultiCommand<Map<String, Object>>, MultiLocalCommand {
|
||||||
|
|
||||||
|
default TypeReference<Map<String, Object>> getRequestType() {
|
||||||
|
return new TypeReference<>() {};
|
||||||
|
}
|
||||||
|
|
||||||
|
default void handleCommand(
|
||||||
|
Map<String, Object> request, SignalCreator c, JsonWriter jsonWriter
|
||||||
|
) throws CommandException {
|
||||||
|
Namespace commandNamespace = new JsonRpcNamespace(request == null ? Map.of() : request);
|
||||||
|
handleCommand(commandNamespace, c, jsonWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
default List<OutputType> getSupportedOutputTypes() {
|
||||||
|
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package org.asamk.signal.commands;
|
package org.asamk.signal.commands;
|
||||||
|
|
||||||
import org.asamk.signal.OutputWriter;
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
|
|
||||||
public interface JsonRpcSingleCommand<T> extends JsonRpcCommand<T> {
|
public interface JsonRpcSingleCommand<T> extends JsonRpcCommand<T> {
|
||||||
|
|
||||||
void handleCommand(T request, Manager m, OutputWriter outputWriter) throws CommandException;
|
void handleCommand(T request, Manager m, JsonWriter jsonWriter) throws CommandException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
||||||
import org.asamk.signal.OutputWriter;
|
import org.asamk.signal.OutputWriter;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
|
||||||
public interface MultiLocalCommand extends LocalCommand {
|
public interface MultiLocalCommand extends CliCommand {
|
||||||
|
|
||||||
void handleCommand(
|
void handleCommand(Namespace ns, SignalCreator c, OutputWriter outputWriter) throws CommandException;
|
||||||
Namespace ns, SignalCreator c, OutputWriter outputWriter
|
|
||||||
) throws CommandException;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,12 @@ package org.asamk.signal.commands;
|
||||||
|
|
||||||
import org.asamk.signal.BaseConfig;
|
import org.asamk.signal.BaseConfig;
|
||||||
import org.asamk.signal.JsonWriter;
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.OutputWriter;
|
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class VersionCommand implements JsonRpcSingleCommand<Void> {
|
public class VersionCommand implements JsonRpcSingleCommand<Void>, JsonRpcMultiCommand<Void> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -17,9 +16,19 @@ public class VersionCommand implements JsonRpcSingleCommand<Void> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Void request, final Manager m, final OutputWriter outputWriter
|
final Void request, final Manager m, final JsonWriter jsonWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
final var jsonWriter = (JsonWriter) outputWriter;
|
outputVersion(jsonWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleCommand(
|
||||||
|
final Void request, final SignalCreator c, final JsonWriter jsonWriter
|
||||||
|
) throws CommandException {
|
||||||
|
outputVersion(jsonWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void outputVersion(final JsonWriter jsonWriter) {
|
||||||
jsonWriter.write(Map.of("version", BaseConfig.PROJECT_VERSION));
|
jsonWriter.write(Map.of("version", BaseConfig.PROJECT_VERSION));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@ import com.fasterxml.jackson.databind.node.ContainerNode;
|
||||||
|
|
||||||
import org.asamk.signal.JsonReceiveMessageHandler;
|
import org.asamk.signal.JsonReceiveMessageHandler;
|
||||||
import org.asamk.signal.JsonWriter;
|
import org.asamk.signal.JsonWriter;
|
||||||
import org.asamk.signal.OutputWriter;
|
|
||||||
import org.asamk.signal.commands.Command;
|
import org.asamk.signal.commands.Command;
|
||||||
import org.asamk.signal.commands.Commands;
|
import org.asamk.signal.commands.Commands;
|
||||||
|
import org.asamk.signal.commands.JsonRpcMultiCommand;
|
||||||
import org.asamk.signal.commands.JsonRpcSingleCommand;
|
import org.asamk.signal.commands.JsonRpcSingleCommand;
|
||||||
import org.asamk.signal.commands.SignalCreator;
|
import org.asamk.signal.commands.SignalCreator;
|
||||||
import org.asamk.signal.commands.exceptions.CommandException;
|
import org.asamk.signal.commands.exceptions.CommandException;
|
||||||
|
@ -48,11 +48,11 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
private Manager m;
|
private Manager m;
|
||||||
|
|
||||||
public SignalJsonRpcDispatcherHandler(
|
public SignalJsonRpcDispatcherHandler(
|
||||||
final JsonWriter outputWriter, final Supplier<String> lineSupplier, final boolean noReceiveOnStart
|
final JsonWriter jsonWriter, final Supplier<String> lineSupplier, final boolean noReceiveOnStart
|
||||||
) {
|
) {
|
||||||
this.noReceiveOnStart = noReceiveOnStart;
|
this.noReceiveOnStart = noReceiveOnStart;
|
||||||
this.objectMapper = Util.createJsonObjectMapper();
|
this.objectMapper = Util.createJsonObjectMapper();
|
||||||
this.jsonRpcSender = new JsonRpcSender(outputWriter);
|
this.jsonRpcSender = new JsonRpcSender(jsonWriter);
|
||||||
this.jsonRpcReader = new JsonRpcReader(jsonRpcSender, lineSupplier);
|
this.jsonRpcReader = new JsonRpcReader(jsonRpcSender, lineSupplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +120,9 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
) throws JsonRpcException {
|
) throws JsonRpcException {
|
||||||
var command = getCommand(method);
|
var command = getCommand(method);
|
||||||
// TODO implement listAccounts, register, verify, link
|
// TODO implement listAccounts, register, verify, link
|
||||||
|
if (c != null && command instanceof JsonRpcMultiCommand<?> jsonRpcCommand) {
|
||||||
|
return runCommand(objectMapper, params, new MultiCommandRunnerImpl<>(c, jsonRpcCommand));
|
||||||
|
}
|
||||||
if (command instanceof JsonRpcSingleCommand<?> jsonRpcCommand) {
|
if (command instanceof JsonRpcSingleCommand<?> jsonRpcCommand) {
|
||||||
if (m != null) {
|
if (m != null) {
|
||||||
return runCommand(objectMapper, params, new CommandRunnerImpl<>(m, jsonRpcCommand));
|
return runCommand(objectMapper, params, new CommandRunnerImpl<>(m, jsonRpcCommand));
|
||||||
|
@ -155,8 +158,23 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
private record CommandRunnerImpl<T>(Manager m, JsonRpcSingleCommand<T> command) implements CommandRunner<T> {
|
private record CommandRunnerImpl<T>(Manager m, JsonRpcSingleCommand<T> command) implements CommandRunner<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(final T request, final OutputWriter outputWriter) throws CommandException {
|
public void handleCommand(final T request, final JsonWriter jsonWriter) throws CommandException {
|
||||||
command.handleCommand(request, m, outputWriter);
|
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
|
@Override
|
||||||
|
@ -167,7 +185,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
|
|
||||||
interface CommandRunner<T> {
|
interface CommandRunner<T> {
|
||||||
|
|
||||||
void handleCommand(T request, OutputWriter outputWriter) throws CommandException;
|
void handleCommand(T request, JsonWriter jsonWriter) throws CommandException;
|
||||||
|
|
||||||
TypeReference<T> getRequestType();
|
TypeReference<T> getRequestType();
|
||||||
}
|
}
|
||||||
|
@ -176,7 +194,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
final ObjectMapper objectMapper, final ContainerNode<?> params, final CommandRunner<?> command
|
final ObjectMapper objectMapper, final ContainerNode<?> params, final CommandRunner<?> command
|
||||||
) throws JsonRpcException {
|
) throws JsonRpcException {
|
||||||
final Object[] result = {null};
|
final Object[] result = {null};
|
||||||
final JsonWriter commandOutputWriter = s -> {
|
final JsonWriter commandJsonWriter = s -> {
|
||||||
if (result[0] != null) {
|
if (result[0] != null) {
|
||||||
throw new AssertionError("Command may only write one json result");
|
throw new AssertionError("Command may only write one json result");
|
||||||
}
|
}
|
||||||
|
@ -185,7 +203,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parseParamsAndRunCommand(objectMapper, params, commandOutputWriter, command);
|
parseParamsAndRunCommand(objectMapper, params, commandJsonWriter, command);
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
|
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
|
||||||
e.getMessage(),
|
e.getMessage(),
|
||||||
|
@ -210,7 +228,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
private <T> void parseParamsAndRunCommand(
|
private <T> void parseParamsAndRunCommand(
|
||||||
final ObjectMapper objectMapper,
|
final ObjectMapper objectMapper,
|
||||||
final TreeNode params,
|
final TreeNode params,
|
||||||
final OutputWriter outputWriter,
|
final JsonWriter jsonWriter,
|
||||||
final CommandRunner<T> command
|
final CommandRunner<T> command
|
||||||
) throws CommandException, JsonMappingException {
|
) throws CommandException, JsonMappingException {
|
||||||
T requestParams = null;
|
T requestParams = null;
|
||||||
|
@ -224,7 +242,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
throw new AssertionError(e);
|
throw new AssertionError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
command.handleCommand(requestParams, outputWriter);
|
command.handleCommand(requestParams, jsonWriter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SubscribeReceiveCommand implements JsonRpcSingleCommand<Void> {
|
private class SubscribeReceiveCommand implements JsonRpcSingleCommand<Void> {
|
||||||
|
@ -236,7 +254,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Void request, final Manager m, final OutputWriter outputWriter
|
final Void request, final Manager m, final JsonWriter jsonWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
subscribeReceive(m);
|
subscribeReceive(m);
|
||||||
}
|
}
|
||||||
|
@ -251,7 +269,7 @@ public class SignalJsonRpcDispatcherHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Void request, final Manager m, final OutputWriter outputWriter
|
final Void request, final Manager m, final JsonWriter jsonWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
unsubscribeReceive(m);
|
unsubscribeReceive(m);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue