mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-24 07:16:43 +00:00
Add java API for restful meshmb send and list
This commit is contained in:
parent
4b1f64c0e3
commit
86d4d87ff2
@ -24,6 +24,8 @@ import org.servalproject.codec.Base64;
|
||||
import org.servalproject.servaldna.keyring.KeyringCommon;
|
||||
import org.servalproject.servaldna.keyring.KeyringIdentity;
|
||||
import org.servalproject.servaldna.keyring.KeyringIdentityList;
|
||||
import org.servalproject.servaldna.meshmb.MeshMBCommon;
|
||||
import org.servalproject.servaldna.meshmb.MessagePlyList;
|
||||
import org.servalproject.servaldna.meshms.MeshMSCommon;
|
||||
import org.servalproject.servaldna.meshms.MeshMSConversationList;
|
||||
import org.servalproject.servaldna.meshms.MeshMSException;
|
||||
@ -183,6 +185,20 @@ public class ServalDClient implements ServalDHttpConnectionFactory {
|
||||
return MeshMSCommon.advanceReadOffset(this, sid1, sid2, offset);
|
||||
}
|
||||
|
||||
public int meshmbSendMessage(SigningKey id, String text) throws IOException, ServalDInterfaceException {
|
||||
return MeshMBCommon.sendMessage(this, id, text);
|
||||
}
|
||||
|
||||
public MessagePlyList meshmbListMessages(SigningKey id) throws IOException, ServalDInterfaceException {
|
||||
return meshmbListMessagesSince(id, null);
|
||||
}
|
||||
|
||||
public MessagePlyList meshmbListMessagesSince(SigningKey id, String token) throws IOException, ServalDInterfaceException {
|
||||
MessagePlyList list = new MessagePlyList(this, id, token);
|
||||
list.connect();
|
||||
return list;
|
||||
}
|
||||
|
||||
// interface ServalDHttpConnectionFactory
|
||||
public HttpURLConnection newServalDHttpConnection(String path) throws ServalDInterfaceException, IOException
|
||||
{
|
||||
|
29
java/org/servalproject/servaldna/meshmb/MeshMBCommon.java
Normal file
29
java/org/servalproject/servaldna/meshmb/MeshMBCommon.java
Normal file
@ -0,0 +1,29 @@
|
||||
package org.servalproject.servaldna.meshmb;
|
||||
|
||||
import org.servalproject.servaldna.PostHelper;
|
||||
import org.servalproject.servaldna.ServalDHttpConnectionFactory;
|
||||
import org.servalproject.servaldna.ServalDInterfaceException;
|
||||
import org.servalproject.servaldna.SigningKey;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
/**
|
||||
* Created by jeremy on 5/10/16.
|
||||
*/
|
||||
public class MeshMBCommon {
|
||||
|
||||
public static int sendMessage(ServalDHttpConnectionFactory connector, SigningKey id, String text) throws IOException, ServalDInterfaceException
|
||||
{
|
||||
HttpURLConnection conn = connector.newServalDHttpConnection("/restful/meshmb/" + id.toHex() + "/sendmessage");
|
||||
PostHelper helper = new PostHelper(conn);
|
||||
helper.connect();
|
||||
helper.writeField("message", text);
|
||||
helper.close();
|
||||
|
||||
// TODO handle specific errors
|
||||
return conn.getResponseCode();
|
||||
}
|
||||
|
||||
|
||||
}
|
56
java/org/servalproject/servaldna/meshmb/MessagePlyList.java
Normal file
56
java/org/servalproject/servaldna/meshmb/MessagePlyList.java
Normal file
@ -0,0 +1,56 @@
|
||||
package org.servalproject.servaldna.meshmb;
|
||||
|
||||
import org.servalproject.json.JSONTableScanner;
|
||||
import org.servalproject.json.JSONTokeniser;
|
||||
import org.servalproject.servaldna.AbstractJsonList;
|
||||
import org.servalproject.servaldna.ServalDHttpConnectionFactory;
|
||||
import org.servalproject.servaldna.ServalDInterfaceException;
|
||||
import org.servalproject.servaldna.SigningKey;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by jeremy on 10/10/16.
|
||||
*/
|
||||
public class MessagePlyList extends AbstractJsonList<PlyMessage, IOException> {
|
||||
private final SigningKey bundleId;
|
||||
private final String sinceToken;
|
||||
|
||||
public MessagePlyList(ServalDHttpConnectionFactory httpConnector, SigningKey bundleId, String sinceToken){
|
||||
super(httpConnector, new JSONTableScanner()
|
||||
.addColumn("offset", Long.class)
|
||||
.addColumn("token", String.class)
|
||||
.addColumn("text", String.class)
|
||||
.addColumn("timestamp", Long.class, JSONTokeniser.Narrow.ALLOW_NULL));
|
||||
this.bundleId = bundleId;
|
||||
this.sinceToken = sinceToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleResponseError() throws IOException, ServalDInterfaceException {
|
||||
// TODO handle specific errors
|
||||
super.handleResponseError();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getUrl() {
|
||||
if (this.sinceToken == null)
|
||||
return "/restful/meshmb/" + bundleId.toHex() + "/messagelist.json";
|
||||
else if(this.sinceToken.equals(""))
|
||||
return "/restful/meshmb/" + bundleId.toHex() + "/newsince/messagelist.json";
|
||||
else
|
||||
return "/restful/meshmb/" + bundleId.toHex() + "/newsince/" + sinceToken + "/messagelist.json";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PlyMessage factory(Map<String, Object> row, long rowCount) {
|
||||
return new PlyMessage(
|
||||
rowCount,
|
||||
(Long)row.get("offset"),
|
||||
(String)row.get("token"),
|
||||
(Long)row.get("timestamp"),
|
||||
(String)row.get("text")
|
||||
);
|
||||
}
|
||||
}
|
20
java/org/servalproject/servaldna/meshmb/PlyMessage.java
Normal file
20
java/org/servalproject/servaldna/meshmb/PlyMessage.java
Normal file
@ -0,0 +1,20 @@
|
||||
package org.servalproject.servaldna.meshmb;
|
||||
|
||||
/**
|
||||
* Created by jeremy on 10/10/16.
|
||||
*/
|
||||
public class PlyMessage {
|
||||
|
||||
public final long _row;
|
||||
public final long offset;
|
||||
public final String token;
|
||||
public final long timestamp;
|
||||
public final String text;
|
||||
public PlyMessage(long _row, long offset, String token, long timestamp, String text){
|
||||
this._row = _row;
|
||||
this.offset = offset;
|
||||
this.token = token;
|
||||
this.timestamp = timestamp;
|
||||
this.text = text;
|
||||
}
|
||||
}
|
@ -130,11 +130,13 @@ static int strn_to_position_token(const char *str, uint64_t *position, const cha
|
||||
size_t token_len = base64url_decode(token, sizeof token, str, 0, afterp, 0, NULL);
|
||||
|
||||
int unpacked;
|
||||
if ((unpacked = unpack_uint(token, token_len, position))==-1){
|
||||
if ((unpacked = unpack_uint(token, token_len, position))!=-1
|
||||
&& **afterp=='/'){
|
||||
(*afterp)++;
|
||||
} else {
|
||||
*position = 0;
|
||||
*afterp=str;
|
||||
}else
|
||||
(*afterp)++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user