parse json data

This commit is contained in:
Vincent Toms 2019-02-20 11:10:25 -05:00
parent 6d5cfa32e2
commit 711d59967f
5 changed files with 49 additions and 30 deletions

View file

@ -1,19 +0,0 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="LINE_SEPARATOR" value="&#10;" />
<JavaCodeStyleSettings>
<option name="GENERATE_FINAL_LOCALS" value="true" />
<option name="GENERATE_FINAL_PARAMETERS" value="true" />
<option name="JD_P_AT_EMPTY_LINES" value="false" />
</JavaCodeStyleSettings>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="JAVA">
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
<option name="BLANK_LINES_AFTER_CLASS_HEADER" value="1" />
</codeStyleSettings>
</code_scheme>
</component>

View file

@ -1,5 +0,0 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

View file

@ -2,8 +2,8 @@ apply plugin: 'java'
apply plugin: 'application' apply plugin: 'application'
apply plugin: 'eclipse' apply plugin: 'eclipse'
sourceCompatibility = JavaVersion.VERSION_1_7 sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_8
mainClassName = 'org.asamk.signal.Main' mainClassName = 'org.asamk.signal.Main'

View file

@ -0,0 +1,30 @@
package org.asamk.signal;
import java.util.Base64;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class JsonSendableMessage {
public String getMessage() {
return message;
}
public List<String> getAttachments() {
return attachments;
}
String message;
public void setAttachments(List<String> attachments) {
if ( attachments == null ) {
return;
}
this.attachments = attachments.stream()
.map(i -> new String(Base64.getDecoder().decode(i)))
.collect(Collectors.toList());
}
List<String> attachments;
}

View file

@ -1,13 +1,12 @@
package org.asamk.signal.commands; package org.asamk.signal.commands;
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 netscape.javascript.JSException;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.AttachmentInvalidException; import org.asamk.signal.*;
import org.asamk.signal.GroupIdFormatException;
import org.asamk.signal.GroupNotFoundException;
import org.asamk.signal.NotAGroupMemberException;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
@ -16,6 +15,7 @@ import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptio
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import static org.asamk.signal.util.ErrorUtils.*; import static org.asamk.signal.util.ErrorUtils.*;
@ -87,6 +87,15 @@ public class SendCommand implements DbusCommand {
if (attachments == null) { if (attachments == null) {
attachments = new ArrayList<>(); attachments = new ArrayList<>();
} }
// check to see if message is a JSON
// { "messageText": "string", "attachments": [ "base64" ] }
if (messageText.startsWith("{")) {
JsonSendableMessage result = new ObjectMapper().readValue(messageText, JsonSendableMessage.class);
messageText = result.getMessage();
attachments = result.getAttachments();
}
if (ns.getString("group") != null) { if (ns.getString("group") != null) {
byte[] groupId = Util.decodeGroupId(ns.getString("group")); byte[] groupId = Util.decodeGroupId(ns.getString("group"));
signal.sendGroupMessage(messageText, attachments, groupId); signal.sendGroupMessage(messageText, attachments, groupId);
@ -119,6 +128,10 @@ public class SendCommand implements DbusCommand {
} catch (GroupIdFormatException e) { } catch (GroupIdFormatException e) {
handleGroupIdFormatException(e); handleGroupIdFormatException(e);
return 1; return 1;
} catch (JSException e) {
// not sure how to deal with this.
e.printStackTrace();
return 3;
} }
} }
} }