Add PlainTextWriter interface

This commit is contained in:
AsamK 2021-08-09 19:02:24 +02:00
parent 641dc7577c
commit 15e8029715
16 changed files with 69 additions and 66 deletions

View file

@ -92,7 +92,7 @@ public class App {
public void init() throws CommandException { public void init() throws CommandException {
var outputType = ns.<OutputType>get("output"); var outputType = ns.<OutputType>get("output");
var outputWriter = outputType == OutputType.JSON var outputWriter = outputType == OutputType.JSON
? new JsonWriter(System.out) ? new JsonWriterImpl(System.out)
: new PlainTextWriterImpl(System.out); : new PlainTextWriterImpl(System.out);
var commandKey = ns.getString("command"); var commandKey = ns.getString("command");

View file

@ -1,43 +1,6 @@
package org.asamk.signal; package org.asamk.signal;
import com.fasterxml.jackson.annotation.JsonAutoDetect; public interface JsonWriter extends OutputWriter {
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter; void write(final Object object);
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonWriter implements OutputWriter {
private final Writer writer;
private final ObjectMapper objectMapper;
public JsonWriter(final OutputStream outputStream) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
public void write(final Object object) {
try {
try {
objectMapper.writeValue(writer, object);
} catch (JsonProcessingException e) {
// Some issue with json serialization, probably caused by a bug
throw new AssertionError(e);
}
writer.write(System.lineSeparator());
writer.flush();
} catch (IOException e) {
throw new AssertionError(e);
}
}
} }

View file

@ -0,0 +1,43 @@
package org.asamk.signal;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
public class JsonWriterImpl implements JsonWriter {
private final Writer writer;
private final ObjectMapper objectMapper;
public JsonWriterImpl(final OutputStream outputStream) {
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.PUBLIC_ONLY);
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
public synchronized void write(final Object object) {
try {
try {
objectMapper.writeValue(writer, object);
} catch (JsonProcessingException e) {
// Some issue with json serialization, probably caused by a bug
throw new AssertionError(e);
}
writer.write(System.lineSeparator());
writer.flush();
} catch (IOException e) {
throw new AssertionError(e);
}
}
}

View file

@ -6,7 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter; import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
@ -60,7 +60,7 @@ public class GetUserStatusCommand implements LocalCommand {
jsonWriter.write(jsonUserStatuses); jsonWriter.write(jsonUserStatuses);
} else { } else {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
for (var entry : registered.entrySet()) { for (var entry : registered.entrySet()) {
writer.println("{}: {}", entry.getKey(), entry.getValue()); writer.println("{}: {}", entry.getKey(), entry.getValue());

View file

@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
@ -49,7 +49,7 @@ public class JoinGroupCommand implements LocalCommand {
} }
try { try {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
final var results = m.joinGroup(linkUrl); final var results = m.joinGroup(linkUrl);
var newGroupId = results.first(); var newGroupId = results.first();

View file

@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -32,7 +32,7 @@ public class LinkCommand implements ProvisioningCommand {
@Override @Override
public void handleCommand(final Namespace ns, final ProvisioningManager m) throws CommandException { public void handleCommand(final Namespace ns, final ProvisioningManager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
var deviceName = ns.getString("name"); var deviceName = ns.getString("name");
if (deviceName == null) { if (deviceName == null) {

View file

@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import static org.asamk.signal.util.Util.getLegacyIdentifier; import static org.asamk.signal.util.Util.getLegacyIdentifier;
@ -23,7 +23,7 @@ public class ListContactsCommand implements LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) { public void handleCommand(final Namespace ns, final Manager m) {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
var contacts = m.getContacts(); var contacts = m.getContacts();
for (var c : contacts) { for (var c : contacts) {

View file

@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
@ -31,7 +31,7 @@ public class ListDevicesCommand implements LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
List<Device> devices; List<Device> devices;
try { try {

View file

@ -5,7 +5,6 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
@ -46,7 +45,7 @@ public class ListIdentitiesCommand implements LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
var number = ns.getString("number"); var number = ns.getString("number");

View file

@ -5,7 +5,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -48,7 +48,7 @@ public class QuitGroupCommand implements LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
final GroupId groupId; final GroupId groupId;
try { try {

View file

@ -10,7 +10,6 @@ import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.ReceiveMessageHandler; import org.asamk.signal.ReceiveMessageHandler;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
@ -79,7 +78,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
jsonWriter.write(object); jsonWriter.write(object);
}); });
} else { } else {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> { dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
writer.println("Envelope from: {}", messageReceived.getSender()); writer.println("Envelope from: {}", messageReceived.getSender());

View file

@ -5,7 +5,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -49,7 +49,7 @@ public class RemoteDeleteCommand implements DbusCommand {
final long targetTimestamp = ns.getLong("target-timestamp"); final long targetTimestamp = ns.getLong("target-timestamp");
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (groupIdString != null) { if (groupIdString != null) {

View file

@ -6,7 +6,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -60,7 +60,7 @@ public class SendReactionCommand implements DbusCommand {
final var targetAuthor = ns.getString("target-author"); final var targetAuthor = ns.getString("target-author");
final long targetTimestamp = ns.getLong("target-timestamp"); final long targetTimestamp = ns.getLong("target-timestamp");
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (groupIdString != null) { if (groupIdString != null) {

View file

@ -32,7 +32,6 @@ public class UnregisterCommand implements LocalCommand {
m.unregister(); m.unregister();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
throw new IOErrorException("Unregister error: " + e.getMessage()); throw new IOErrorException("Unregister error: " + e.getMessage());
} }
} }

View file

@ -8,7 +8,7 @@ import org.asamk.Signal;
import org.asamk.signal.GroupLinkState; import org.asamk.signal.GroupLinkState;
import org.asamk.signal.GroupPermission; import org.asamk.signal.GroupPermission;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -74,7 +74,7 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
GroupId groupId = null; GroupId groupId = null;
final var groupIdString = ns.getString("group"); final var groupIdString = ns.getString("group");
if (groupIdString != null) { if (groupIdString != null) {
@ -142,7 +142,7 @@ public class UpdateGroupCommand implements DbusCommand, LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Signal signal) throws CommandException { public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (ns.getString("group") != null) { if (ns.getString("group") != null) {
try { try {

View file

@ -4,7 +4,7 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
@ -33,7 +33,7 @@ public class UploadStickerPackCommand implements LocalCommand {
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = (PlainTextWriterImpl) outputWriter; final var writer = (PlainTextWriter) outputWriter;
var path = new File(ns.getString("path")); var path = new File(ns.getString("path"));
try { try {