From c3ca00fe56fa18f9d265f843e4f1272433dc36a5 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Tue, 26 Jul 2016 09:43:25 +0930 Subject: [PATCH] Add signing key (not populated yet) to meshms java api --- .../servaldna/meshms/MeshMSConversation.java | 13 ++- .../meshms/MeshMSConversationList.java | 91 +++++-------------- .../servaldna/meshms/MeshMSMessage.java | 23 +++-- .../servaldna/meshms/MeshMSMessageList.java | 5 +- 4 files changed, 52 insertions(+), 80 deletions(-) diff --git a/java/org/servalproject/servaldna/meshms/MeshMSConversation.java b/java/org/servalproject/servaldna/meshms/MeshMSConversation.java index eba45f71..54fcbedc 100644 --- a/java/org/servalproject/servaldna/meshms/MeshMSConversation.java +++ b/java/org/servalproject/servaldna/meshms/MeshMSConversation.java @@ -20,24 +20,31 @@ package org.servalproject.servaldna.meshms; +import org.servalproject.servaldna.Subscriber; import org.servalproject.servaldna.SubscriberId; public class MeshMSConversation { public final int _rowNumber; public final int _id; + public final Subscriber me; + @Deprecated public final SubscriberId mySid; + public final Subscriber them; + @Deprecated public final SubscriberId theirSid; public final boolean isRead; public final long lastMessageOffset; public final long readOffset; - protected MeshMSConversation(int rowNumber, int _id, SubscriberId my_sid, SubscriberId their_sid, boolean read, long last_message_offset, long read_offset) + protected MeshMSConversation(int rowNumber, int _id, Subscriber me, Subscriber them, boolean read, long last_message_offset, long read_offset) { this._rowNumber = rowNumber; this._id = _id; - this.mySid = my_sid; - this.theirSid = their_sid; + this.me = me; + this.mySid = me.sid; + this.them = them; + this.theirSid = them.sid; this.isRead = read; this.lastMessageOffset = last_message_offset; this.readOffset = read_offset; diff --git a/java/org/servalproject/servaldna/meshms/MeshMSConversationList.java b/java/org/servalproject/servaldna/meshms/MeshMSConversationList.java index 41dafaa4..b122499e 100644 --- a/java/org/servalproject/servaldna/meshms/MeshMSConversationList.java +++ b/java/org/servalproject/servaldna/meshms/MeshMSConversationList.java @@ -21,16 +21,18 @@ package org.servalproject.servaldna.meshms; import org.servalproject.json.JSONInputException; +import org.servalproject.json.JSONTableScanner; import org.servalproject.json.JSONTokeniser; import org.servalproject.servaldna.ServalDHttpConnectionFactory; import org.servalproject.servaldna.ServalDInterfaceException; +import org.servalproject.servaldna.Subscriber; import org.servalproject.servaldna.SubscriberId; import java.io.IOException; import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; -import java.util.Vector; +import java.util.Map; public class MeshMSConversationList { @@ -38,19 +40,21 @@ public class MeshMSConversationList { private SubscriberId sid; private HttpURLConnection httpConnection; private JSONTokeniser json; - private Vector headers; - int columnIndex__id; - int columnIndex_my_sid; - int columnIndex_their_sid; - int columnIndex_read; - int columnIndex_last_message; - int columnIndex_read_offset; + private JSONTableScanner table; int rowCount; public MeshMSConversationList(ServalDHttpConnectionFactory connector, SubscriberId sid) { this.httpConnector = connector; this.sid = sid; + this.table = new JSONTableScanner() + .addColumn("_id", Integer.class) + .addColumn("my_sid", SubscriberId.class) + .addColumn("their_sid", SubscriberId.class) + .addColumn("read", Boolean.class) + .addColumn("last_message", Long.class) + .addColumn("read_offset", Long.class) + ; } public boolean isConnected() @@ -61,12 +65,6 @@ public class MeshMSConversationList { public void connect() throws IOException, ServalDInterfaceException, MeshMSException { try { - columnIndex__id = -1; - columnIndex_my_sid = -1; - columnIndex_their_sid = -1; - columnIndex_read = -1; - columnIndex_last_message = -1; - columnIndex_read_offset = -1; rowCount = 0; httpConnection = httpConnector.newServalDHttpConnection("/restful/meshms/" + sid.toHex() + "/conversationlist.json"); httpConnection.connect(); @@ -74,37 +72,7 @@ public class MeshMSConversationList { json.consume(JSONTokeniser.Token.START_OBJECT); json.consume("header"); json.consume(JSONTokeniser.Token.COLON); - headers = new Vector(); - json.consumeArray(headers, String.class); - if (headers.size() < 1) - throw new ServalDInterfaceException("empty JSON headers array"); - for (int i = 0; i < headers.size(); ++i) { - String header = headers.get(i); - if (header.equals("_id")) - columnIndex__id = i; - else if (header.equals("my_sid")) - columnIndex_my_sid = i; - else if (header.equals("their_sid")) - columnIndex_their_sid = i; - else if (header.equals("read")) - columnIndex_read = i; - else if (header.equals("last_message")) - columnIndex_last_message = i; - else if (header.equals("read_offset")) - columnIndex_read_offset = i; - } - if (columnIndex__id == -1) - throw new ServalDInterfaceException("missing JSON column: _id"); - if (columnIndex_my_sid == -1) - throw new ServalDInterfaceException("missing JSON column: my_sid"); - if (columnIndex_their_sid == -1) - throw new ServalDInterfaceException("missing JSON column: their_sid"); - if (columnIndex_read == -1) - throw new ServalDInterfaceException("missing JSON column: read"); - if (columnIndex_last_message == -1) - throw new ServalDInterfaceException("missing JSON column: last_message"); - if (columnIndex_read_offset == -1) - throw new ServalDInterfaceException("missing JSON column: read_offset"); + table.consumeHeaderArray(json); json.consume(JSONTokeniser.Token.COMMA); json.consume("rows"); json.consume(JSONTokeniser.Token.COLON); @@ -128,27 +96,17 @@ public class MeshMSConversationList { JSONTokeniser.match(tok, JSONTokeniser.Token.COMMA); else json.pushToken(tok); - Object[] row = new Object[headers.size()]; - json.consumeArray(row); - int _id = JSONTokeniser.narrow(row[columnIndex__id], Integer.class); - SubscriberId my_sid; - try { - my_sid = new SubscriberId(JSONTokeniser.narrow(row[columnIndex_my_sid], String.class)); - } - catch (SubscriberId.InvalidHexException e) { - throw new ServalDInterfaceException("invalid column value: my_sid", e); - } - SubscriberId their_sid; - try { - their_sid = new SubscriberId(JSONTokeniser.narrow(row[columnIndex_their_sid], String.class)); - } - catch (SubscriberId.InvalidHexException e) { - throw new ServalDInterfaceException("invalid column value: their_sid", e); - } - boolean is_read = JSONTokeniser.narrow(row[columnIndex_read], Boolean.class); - long last_message = JSONTokeniser.narrow(row[columnIndex_last_message], Long.class); - long read_offset = JSONTokeniser.narrow(row[columnIndex_read_offset], Long.class); - return new MeshMSConversation(rowCount++, _id, my_sid, their_sid, is_read, last_message, read_offset); + + Map row = table.consumeRowArray(json); + + return new MeshMSConversation( + rowCount++, + (Integer)row.get("_id"), + new Subscriber((SubscriberId)row.get("my_sid")), + new Subscriber((SubscriberId)row.get("their_sid")), + (Boolean)row.get("read"), + (Long)row.get("last_message"), + (Long)row.get("read_offset")); } catch (JSONInputException e) { throw new ServalDInterfaceException(e); @@ -162,7 +120,6 @@ public class MeshMSConversationList { json.close(); json = null; } - headers = null; } public List toList() throws ServalDInterfaceException, IOException { diff --git a/java/org/servalproject/servaldna/meshms/MeshMSMessage.java b/java/org/servalproject/servaldna/meshms/MeshMSMessage.java index ea175b6c..6e50e93b 100644 --- a/java/org/servalproject/servaldna/meshms/MeshMSMessage.java +++ b/java/org/servalproject/servaldna/meshms/MeshMSMessage.java @@ -20,6 +20,7 @@ package org.servalproject.servaldna.meshms; +import org.servalproject.servaldna.Subscriber; import org.servalproject.servaldna.SubscriberId; import org.servalproject.servaldna.ServalDInterfaceException; @@ -33,7 +34,11 @@ public class MeshMSMessage { public final int _rowNumber; public final Type type; + public final Subscriber me; + @Deprecated public final SubscriberId mySid; + public final Subscriber them; + @Deprecated public final SubscriberId theirSid; public final long offset; public final String token; @@ -45,8 +50,8 @@ public class MeshMSMessage { protected MeshMSMessage(int rowNumber, Type type, - SubscriberId my_sid, - SubscriberId their_sid, + Subscriber me, + Subscriber them, long offset, String token, String text, @@ -55,10 +60,10 @@ public class MeshMSMessage { Long timestamp, Long ack_offset) throws ServalDInterfaceException { - if (my_sid == null) - throw new ServalDInterfaceException("my_sid is null"); - if (their_sid == null) - throw new ServalDInterfaceException("their_sid is null"); + if (me == null) + throw new ServalDInterfaceException("me is null"); + if (them == null) + throw new ServalDInterfaceException("them is null"); if (type != Type.ACK_RECEIVED && text == null) throw new ServalDInterfaceException("text is null"); if (token == null) @@ -69,8 +74,10 @@ public class MeshMSMessage { throw new ServalDInterfaceException("timestamp is null"); this._rowNumber = rowNumber; this.type = type; - this.mySid = my_sid; - this.theirSid = their_sid; + this.me = me; + this.mySid = me.sid; + this.them = them; + this.theirSid = them.sid; this.offset = offset; this.token = token; this.text = text; diff --git a/java/org/servalproject/servaldna/meshms/MeshMSMessageList.java b/java/org/servalproject/servaldna/meshms/MeshMSMessageList.java index f9894a18..e4afef16 100644 --- a/java/org/servalproject/servaldna/meshms/MeshMSMessageList.java +++ b/java/org/servalproject/servaldna/meshms/MeshMSMessageList.java @@ -25,6 +25,7 @@ import org.servalproject.json.JSONTableScanner; import org.servalproject.json.JSONTokeniser; import org.servalproject.servaldna.ServalDHttpConnectionFactory; import org.servalproject.servaldna.ServalDInterfaceException; +import org.servalproject.servaldna.Subscriber; import org.servalproject.servaldna.SubscriberId; import java.io.IOException; @@ -150,8 +151,8 @@ public class MeshMSMessageList { return new MeshMSMessage( rowCount++, type, - (SubscriberId)row.get("my_sid"), - (SubscriberId)row.get("their_sid"), + new Subscriber((SubscriberId)row.get("my_sid")), + new Subscriber((SubscriberId)row.get("their_sid")), (Long)row.get("offset"), (String)row.get("token"), (String)row.get("text"),