Extract JsonWriter for json output

This commit is contained in:
AsamK 2021-01-16 20:11:32 +01:00
parent d4b4bd8603
commit 03bf043959
6 changed files with 94 additions and 89 deletions

View file

@ -1,45 +1,42 @@
package org.asamk.signal; 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.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.asamk.signal.json.JsonError; import org.asamk.signal.json.JsonError;
import org.asamk.signal.json.JsonMessageEnvelope; import org.asamk.signal.json.JsonMessageEnvelope;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.messages.SignalServiceContent; import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler { public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler {
final Manager m; private final static Logger logger = LoggerFactory.getLogger(JsonReceiveMessageHandler.class);
private final ObjectMapper jsonProcessor;
protected final Manager m;
private final JsonWriter jsonWriter;
public JsonReceiveMessageHandler(Manager m) { public JsonReceiveMessageHandler(Manager m) {
this.m = m; this.m = m;
this.jsonProcessor = new ObjectMapper(); jsonWriter = new JsonWriter(System.out);
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} }
@Override @Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
ObjectNode result = jsonProcessor.createObjectNode(); final Map<String, Object> object = new HashMap<>();
if (exception != null) { if (exception != null) {
result.putPOJO("error", new JsonError(exception)); object.put("error", new JsonError(exception));
} }
if (envelope != null) { if (envelope != null) {
result.putPOJO("envelope", new JsonMessageEnvelope(envelope, content, m)); object.put("envelope", new JsonMessageEnvelope(envelope, content, m));
} }
try { try {
jsonProcessor.writeValue(System.out, result); jsonWriter.write(object);
System.out.println();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("Failed to write json object: {}", e.getMessage());
} }
} }
} }

View file

@ -0,0 +1,37 @@
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.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
public class JsonWriter {
private final OutputStreamWriter writer;
private final ObjectMapper objectMapper;
public JsonWriter(final OutputStream writer) {
this.writer = new OutputStreamWriter(writer, StandardCharsets.UTF_8);
objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}
public void write(final Object object) throws IOException {
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();
}
}

View file

@ -1,12 +1,10 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -20,7 +18,6 @@ import java.util.stream.Collectors;
public class GetUserStatusCommand implements LocalCommand { public class GetUserStatusCommand implements LocalCommand {
// TODO delete later when "json" variable is removed
private final static Logger logger = LoggerFactory.getLogger(GetUserStatusCommand.class); private final static Logger logger = LoggerFactory.getLogger(GetUserStatusCommand.class);
@Override @Override
@ -35,9 +32,6 @@ public class GetUserStatusCommand implements LocalCommand {
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public int handleCommand(final Namespace ns, final Manager m) {
// Setup the json object mapper // Setup the json object mapper
ObjectMapper jsonProcessor = new ObjectMapper();
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -56,16 +50,18 @@ public class GetUserStatusCommand implements LocalCommand {
// Output // Output
if (inJson) { if (inJson) {
List<JsonIsRegistered> objects = registered.entrySet() final JsonWriter jsonWriter = new JsonWriter(System.out);
List<JsonUserStatus> jsonUserStatuses = registered.entrySet()
.stream() .stream()
.map(entry -> new JsonIsRegistered(entry.getKey(), entry.getValue())) .map(entry -> new JsonUserStatus(entry.getKey(), entry.getValue()))
.collect(Collectors.toList()); .collect(Collectors.toList());
try { try {
jsonProcessor.writeValue(System.out, objects); jsonWriter.write(jsonUserStatuses);
System.out.println();
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.getMessage()); logger.error("Failed to write json object: {}", e.getMessage());
return 3;
} }
} else { } else {
for (Map.Entry<String, Boolean> entry : registered.entrySet()) { for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
@ -76,13 +72,13 @@ public class GetUserStatusCommand implements LocalCommand {
return 0; return 0;
} }
private static final class JsonIsRegistered { private static final class JsonUserStatus {
public String name; public String name;
public boolean isRegistered; public boolean isRegistered;
public JsonIsRegistered(String name, boolean isRegistered) { public JsonUserStatus(String name, boolean isRegistered) {
this.name = name; this.name = name;
this.isRegistered = isRegistered; this.isRegistered = isRegistered;
} }

View file

@ -1,18 +1,16 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl; import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.storage.groups.GroupInfo; import org.asamk.signal.manager.storage.groups.GroupInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import java.io.IOException; import java.io.IOException;
@ -23,6 +21,8 @@ import java.util.stream.Collectors;
public class ListGroupsCommand implements LocalCommand { public class ListGroupsCommand implements LocalCommand {
private final static Logger logger = LoggerFactory.getLogger(ListGroupsCommand.class);
private static Set<String> resolveMembers(Manager m, Set<SignalServiceAddress> addresses) { private static Set<String> resolveMembers(Manager m, Set<SignalServiceAddress> addresses) {
return addresses.stream() return addresses.stream()
.map(m::resolveSignalServiceAddress) .map(m::resolveSignalServiceAddress)
@ -30,18 +30,6 @@ public class ListGroupsCommand implements LocalCommand {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
private static int printGroupsJson(ObjectMapper jsonProcessor, List<?> objects) {
try {
jsonProcessor.writeValue(System.out, objects);
System.out.println();
} catch (IOException e) {
System.err.println(e.getMessage());
return 3;
}
return 0;
}
private static void printGroupPlainText(Manager m, GroupInfo group, boolean detailed) { private static void printGroupPlainText(Manager m, GroupInfo group, boolean detailed) {
if (detailed) { if (detailed) {
final GroupInviteLinkUrl groupInviteLink = group.getGroupInviteLink(); final GroupInviteLinkUrl groupInviteLink = group.getGroupInviteLink();
@ -77,15 +65,13 @@ public class ListGroupsCommand implements LocalCommand {
@Override @Override
public int handleCommand(final Namespace ns, final Manager m) { public int handleCommand(final Namespace ns, final Manager m) {
if (ns.get("output") == OutputType.JSON) { if (ns.get("output") == OutputType.JSON) {
final ObjectMapper jsonProcessor = new ObjectMapper(); final JsonWriter jsonWriter = new JsonWriter(System.out);
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
List<JsonGroup> objects = new ArrayList<>(); List<JsonGroup> jsonGroups = new ArrayList<>();
for (GroupInfo group : m.getGroups()) { for (GroupInfo group : m.getGroups()) {
final GroupInviteLinkUrl groupInviteLink = group.getGroupInviteLink(); final GroupInviteLinkUrl groupInviteLink = group.getGroupInviteLink();
objects.add(new JsonGroup(group.getGroupId().toBase64(), jsonGroups.add(new JsonGroup(group.getGroupId().toBase64(),
group.getTitle(), group.getTitle(),
group.isMember(m.getSelfAddress()), group.isMember(m.getSelfAddress()),
group.isBlocked(), group.isBlocked(),
@ -94,7 +80,15 @@ public class ListGroupsCommand implements LocalCommand {
resolveMembers(m, group.getRequestingMembers()), resolveMembers(m, group.getRequestingMembers()),
groupInviteLink == null ? null : groupInviteLink.getUrl())); groupInviteLink == null ? null : groupInviteLink.getUrl()));
} }
return printGroupsJson(jsonProcessor, objects);
try {
jsonWriter.write(jsonGroups);
} catch (IOException e) {
logger.error("Failed to write json object: {}", e.getMessage());
return 3;
}
return 0;
} else { } else {
boolean detailed = ns.getBoolean("detailed"); boolean detailed = ns.getBoolean("detailed");
for (GroupInfo group : m.getGroups()) { for (GroupInfo group : m.getGroups()) {

View file

@ -1,17 +1,12 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.JsonReceiveMessageHandler; import org.asamk.signal.JsonReceiveMessageHandler;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType; import org.asamk.signal.OutputType;
import org.asamk.signal.ReceiveMessageHandler; import org.asamk.signal.ReceiveMessageHandler;
import org.asamk.signal.json.JsonMessageEnvelope; import org.asamk.signal.json.JsonMessageEnvelope;
@ -24,13 +19,13 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand { public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
// TODO delete later when "json" variable is removed
private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class); private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
@Override @Override
@ -47,8 +42,6 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
} }
public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) { public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
final ObjectMapper jsonProcessor;
boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json"); boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed // TODO delete later when "json" variable is removed
@ -56,24 +49,16 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead."); logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
} }
if (inJson) { final JsonWriter jsonWriter = inJson ? new JsonWriter(System.out) : null;
jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
} else {
jsonProcessor = null;
}
try { try {
dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> { dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
if (jsonProcessor != null) { if (jsonWriter != null) {
JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived); JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
ObjectNode result = jsonProcessor.createObjectNode(); final Map<String, JsonMessageEnvelope> object = Map.of("envelope", envelope);
result.putPOJO("envelope", envelope);
try { try {
jsonProcessor.writeValue(System.out, result); jsonWriter.write(object);
System.out.println();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("Failed to write json object: {}", e.getMessage());
} }
} else { } else {
System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n", System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
@ -95,15 +80,13 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
}); });
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> { dbusconnection.addSigHandler(Signal.ReceiptReceived.class, receiptReceived -> {
if (jsonProcessor != null) { if (jsonWriter != null) {
JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived); JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived);
ObjectNode result = jsonProcessor.createObjectNode(); final Map<String, JsonMessageEnvelope> object = Map.of("envelope", envelope);
result.putPOJO("envelope", envelope);
try { try {
jsonProcessor.writeValue(System.out, result); jsonWriter.write(object);
System.out.println();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("Failed to write json object: {}", e.getMessage());
} }
} else { } else {
System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n", System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
@ -113,15 +96,13 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
}); });
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> { dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
if (jsonProcessor != null) { if (jsonWriter != null) {
JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived); JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived);
ObjectNode result = jsonProcessor.createObjectNode(); final Map<String, JsonMessageEnvelope> object = Map.of("envelope", envelope);
result.putPOJO("envelope", envelope);
try { try {
jsonProcessor.writeValue(System.out, result); jsonWriter.write(object);
System.out.println();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("Failed to write json object: {}", e.getMessage());
} }
} else { } else {
System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n", System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",

View file

@ -91,7 +91,7 @@ public class SignalAccount implements Closeable {
this.fileChannel = fileChannel; this.fileChannel = fileChannel;
this.lock = lock; this.lock = lock;
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); // disable autodetect jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); // disable autodetect
jsonProcessor.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print, you can disable it. jsonProcessor.enable(SerializationFeature.INDENT_OUTPUT); // for pretty print
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); jsonProcessor.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);