Add JsonRpcMultiCommand

This commit is contained in:
AsamK 2021-11-10 19:21:51 +01:00
parent 64af4914ee
commit 9c67aabebb
8 changed files with 90 additions and 26 deletions

View file

@ -38,7 +38,7 @@ import java.util.Objects;
import java.util.function.Consumer;
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);

View file

@ -4,8 +4,8 @@ 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.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager;
@ -19,10 +19,10 @@ public interface JsonRpcLocalCommand extends JsonRpcSingleCommand<Map<String, Ob
}
default void handleCommand(
Map<String, Object> request, Manager m, OutputWriter outputWriter
Map<String, Object> request, Manager m, JsonWriter jsonWriter
) throws CommandException {
Namespace commandNamespace = new JsonRpcNamespace(request == null ? Map.of() : request);
handleCommand(commandNamespace, m, outputWriter);
handleCommand(commandNamespace, m, jsonWriter);
}
default List<OutputType> getSupportedOutputTypes() {

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -1,10 +1,10 @@
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.manager.Manager;
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;
}

View file

@ -5,9 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException;
public interface MultiLocalCommand extends LocalCommand {
public interface MultiLocalCommand extends CliCommand {
void handleCommand(
Namespace ns, SignalCreator c, OutputWriter outputWriter
) throws CommandException;
void handleCommand(Namespace ns, SignalCreator c, OutputWriter outputWriter) throws CommandException;
}

View file

@ -2,13 +2,12 @@ package org.asamk.signal.commands;
import org.asamk.signal.BaseConfig;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager;
import java.util.Map;
public class VersionCommand implements JsonRpcSingleCommand<Void> {
public class VersionCommand implements JsonRpcSingleCommand<Void>, JsonRpcMultiCommand<Void> {
@Override
public String getName() {
@ -17,9 +16,19 @@ public class VersionCommand implements JsonRpcSingleCommand<Void> {
@Override
public void handleCommand(
final Void request, final Manager m, final OutputWriter outputWriter
final Void request, final Manager m, final JsonWriter jsonWriter
) 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));
}
}