Skip to content

Commit

Permalink
Extract static methods from Main
Browse files Browse the repository at this point in the history
  • Loading branch information
AsamK committed Nov 19, 2018
1 parent 184354f commit 860ec6f
Show file tree
Hide file tree
Showing 8 changed files with 520 additions and 440 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/asamk/signal/DbusReceiveMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.asamk.signal;

import org.asamk.signal.manager.Manager;
import org.freedesktop.dbus.DBusConnection;
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;

class DbusReceiveMessageHandler extends ReceiveMessageHandler {

private final DBusConnection conn;
private final String objectPath;

DbusReceiveMessageHandler(Manager m, DBusConnection conn, final String objectPath) {
super(m);
this.conn = conn;
this.objectPath = objectPath;
}

@Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
super.handleMessage(envelope, content, exception);

JsonDbusReceiveMessageHandler.sendReceivedMessageToDbus(envelope, content, conn, objectPath, m);
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/asamk/signal/GroupIdFormatException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.asamk.signal;

import java.io.IOException;

public class GroupIdFormatException extends Exception {

public GroupIdFormatException(String groupId, IOException e) {
super("Failed to decode groupId (must be base64) \"" + groupId + "\": " + e.getMessage());
}
}
71 changes: 71 additions & 0 deletions src/main/java/org/asamk/signal/JsonDbusReceiveMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.asamk.signal;

import org.asamk.Signal;
import org.asamk.signal.manager.Manager;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
import org.whispersystems.signalservice.api.messages.*;

import java.util.ArrayList;
import java.util.List;

class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {

private final DBusConnection conn;

private final String objectPath;

JsonDbusReceiveMessageHandler(Manager m, DBusConnection conn, final String objectPath) {
super(m);
this.conn = conn;
this.objectPath = objectPath;
}

static void sendReceivedMessageToDbus(SignalServiceEnvelope envelope, SignalServiceContent content, DBusConnection conn, final String objectPath, Manager m) {
if (envelope.isReceipt()) {
try {
conn.sendSignal(new Signal.ReceiptReceived(
objectPath,
envelope.getTimestamp(),
envelope.getSource()
));
} catch (DBusException e) {
e.printStackTrace();
}
} else if (content != null && content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();

if (!message.isEndSession() &&
!(message.getGroupInfo().isPresent() &&
message.getGroupInfo().get().getType() != SignalServiceGroup.Type.DELIVER)) {
List<String> attachments = new ArrayList<>();
if (message.getAttachments().isPresent()) {
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
if (attachment.isPointer()) {
attachments.add(m.getAttachmentFile(attachment.asPointer().getId()).getAbsolutePath());
}
}
}

try {
conn.sendSignal(new Signal.MessageReceived(
objectPath,
message.getTimestamp(),
envelope.getSource(),
message.getGroupInfo().isPresent() ? message.getGroupInfo().get().getGroupId() : new byte[0],
message.getBody().isPresent() ? message.getBody().get() : "",
attachments));
} catch (DBusException e) {
e.printStackTrace();
}
}
}
}

@Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
super.handleMessage(envelope, content, exception);

sendReceivedMessageToDbus(envelope, content, conn, objectPath, m);
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/asamk/signal/JsonReceiveMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.asamk.signal.manager.Manager;
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;

import java.io.IOException;

class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler {

final Manager m;
private final ObjectMapper jsonProcessor;

JsonReceiveMessageHandler(Manager m) {
this.m = m;
this.jsonProcessor = new ObjectMapper();
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
jsonProcessor.enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
}

@Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
ObjectNode result = jsonProcessor.createObjectNode();
if (exception != null) {
result.putPOJO("error", new JsonError(exception));
}
if (envelope != null) {
result.putPOJO("envelope", new JsonMessageEnvelope(envelope, content));
}
try {
jsonProcessor.writeValue(System.out, result);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 860ec6f

Please sign in to comment.