Support for retrieving messages via REST.

Support for retrieving messages via HTTP rather than websockets.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-04-15 16:46:03 -07:00
parent 0e3ca18588
commit 807f13ddc4
4 changed files with 114 additions and 0 deletions

View File

@ -19,15 +19,19 @@ package org.whispersystems.textsecure.api;
import org.whispersystems.libaxolotl.InvalidMessageException;
import org.whispersystems.textsecure.api.crypto.AttachmentCipherInputStream;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import org.whispersystems.textsecure.api.push.TrustStore;
import org.whispersystems.textsecure.api.util.CredentialsProvider;
import org.whispersystems.textsecure.internal.push.PushServiceSocket;
import org.whispersystems.textsecure.internal.push.TextSecureEnvelopeEntity;
import org.whispersystems.textsecure.internal.util.StaticCredentialsProvider;
import org.whispersystems.textsecure.internal.websocket.WebSocketConnection;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
/**
* The primary interface for receiving TextSecure messages.
@ -102,4 +106,38 @@ public class TextSecureMessageReceiver {
return new TextSecureMessagePipe(webSocket, credentialsProvider);
}
public List<TextSecureEnvelope> retrieveMessages() throws IOException {
return retrieveMessages(new NullMessageReceivedCallback());
}
public List<TextSecureEnvelope> retrieveMessages(MessageReceivedCallback callback)
throws IOException
{
List<TextSecureEnvelope> results = new LinkedList<>();
List<TextSecureEnvelopeEntity> entities = socket.getMessages();
for (TextSecureEnvelopeEntity entity : entities) {
TextSecureEnvelope envelope = new TextSecureEnvelope(entity.getType(), entity.getSource(),
entity.getSourceDevice(), entity.getRelay(),
entity.getTimestamp(), entity.getMessage());
callback.onMessage(envelope);
results.add(envelope);
socket.acknowledgeMessage(entity.getSource(), entity.getTimestamp());
}
return results;
}
public interface MessageReceivedCallback {
public void onMessage(TextSecureEnvelope envelope);
}
public static class NullMessageReceivedCallback implements MessageReceivedCallback {
@Override
public void onMessage(TextSecureEnvelope envelope) {}
}
}

View File

@ -90,6 +90,7 @@ public class PushServiceSocket {
private static final String DIRECTORY_TOKENS_PATH = "/v1/directory/tokens";
private static final String DIRECTORY_VERIFY_PATH = "/v1/directory/%s";
private static final String MESSAGE_PATH = "/v1/messages/%s";
private static final String ACKNOWLEDGE_MESSAGE_PATH = "/v1/messages/%s/%d";
private static final String RECEIPT_PATH = "/v1/receipt/%s/%d";
private static final String ATTACHMENT_PATH = "/v1/attachments/%s";
@ -162,6 +163,15 @@ public class PushServiceSocket {
}
}
public List<TextSecureEnvelopeEntity> getMessages() throws IOException {
String responseText = makeRequest(String.format(MESSAGE_PATH, ""), "GET", null);
return JsonUtil.fromJson(responseText, TextSecureEnvelopeEntityList.class).getMessages();
}
public void acknowledgeMessage(String sender, long timestamp) throws IOException {
makeRequest(String.format(ACKNOWLEDGE_MESSAGE_PATH, sender, timestamp), "DELETE", null);
}
public void registerPreKeys(IdentityKey identityKey,
PreKeyRecord lastResortKey,
SignedPreKeyRecord signedPreKey,

View File

@ -0,0 +1,50 @@
package org.whispersystems.textsecure.internal.push;
import com.fasterxml.jackson.annotation.JsonProperty;
public class TextSecureEnvelopeEntity {
@JsonProperty
private int type;
@JsonProperty
private String relay;
@JsonProperty
private long timestamp;
@JsonProperty
private String source;
@JsonProperty
private int sourceDevice;
@JsonProperty
private byte[] message;
public TextSecureEnvelopeEntity() {}
public int getType() {
return type;
}
public String getRelay() {
return relay;
}
public long getTimestamp() {
return timestamp;
}
public String getSource() {
return source;
}
public int getSourceDevice() {
return sourceDevice;
}
public byte[] getMessage() {
return message;
}
}

View File

@ -0,0 +1,16 @@
package org.whispersystems.textsecure.internal.push;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import java.util.List;
public class TextSecureEnvelopeEntityList {
private List<TextSecureEnvelopeEntity> messages;
public TextSecureEnvelopeEntityList() {}
public List<TextSecureEnvelopeEntity> getMessages() {
return messages;
}
}