Make the group field of messages optional.

Private messages have no group.
This commit is contained in:
akwizgran
2011-09-13 14:17:22 +01:00
parent 1d25b5a92e
commit 2858c139fa
4 changed files with 29 additions and 12 deletions

View File

@@ -6,7 +6,11 @@ import java.security.PrivateKey;
public interface MessageEncoder { public interface MessageEncoder {
/** Encodes an anonymous to an unrestricted group. */ /** Encodes a private message. */
Message encodeMessage(MessageId parent, byte[] body) throws IOException,
GeneralSecurityException;
/** Encodes an anonymous message to an unrestricted group. */
Message encodeMessage(MessageId parent, Group group, byte[] body) Message encodeMessage(MessageId parent, Group group, byte[] body)
throws IOException, GeneralSecurityException; throws IOException, GeneralSecurityException;

View File

@@ -53,7 +53,7 @@ abstract class JdbcDatabase implements Database<Connection> {
"CREATE TABLE messages" "CREATE TABLE messages"
+ " (messageId HASH NOT NULL," + " (messageId HASH NOT NULL,"
+ " parentId HASH," + " parentId HASH,"
+ " groupId HASH NOT NULL," + " groupId HASH,"
+ " authorId HASH," + " authorId HASH,"
+ " timestamp BIGINT NOT NULL," + " timestamp BIGINT NOT NULL,"
+ " size INT NOT NULL," + " size INT NOT NULL,"

View File

@@ -33,6 +33,11 @@ class MessageEncoderImpl implements MessageEncoder {
this.writerFactory = writerFactory; this.writerFactory = writerFactory;
} }
public Message encodeMessage(MessageId parent, byte[] body)
throws IOException, GeneralSecurityException {
return encodeMessage(parent, null, null, null, null, body);
}
public Message encodeMessage(MessageId parent, Group group, byte[] body) public Message encodeMessage(MessageId parent, Group group, byte[] body)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
return encodeMessage(parent, group, null, null, null, body); return encodeMessage(parent, group, null, null, null, body);
@@ -56,7 +61,8 @@ class MessageEncoderImpl implements MessageEncoder {
if((author == null) != (authorKey == null)) if((author == null) != (authorKey == null))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if((group.getPublicKey() == null) != (groupKey == null)) if((group == null || group.getPublicKey() == null) !=
(groupKey == null))
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if(body.length > Message.MAX_BODY_LENGTH) if(body.length > Message.MAX_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@@ -68,7 +74,8 @@ class MessageEncoderImpl implements MessageEncoder {
w.writeUserDefinedTag(Types.MESSAGE); w.writeUserDefinedTag(Types.MESSAGE);
if(parent == null) w.writeNull(); if(parent == null) w.writeNull();
else parent.writeTo(w); else parent.writeTo(w);
group.writeTo(w); if(group == null) w.writeNull();
else group.writeTo(w);
if(author == null) w.writeNull(); if(author == null) w.writeNull();
else author.writeTo(w); else author.writeTo(w);
w.writeInt64(timestamp); w.writeInt64(timestamp);

View File

@@ -12,6 +12,7 @@ import net.sf.briar.api.crypto.KeyParser;
import net.sf.briar.api.protocol.Author; import net.sf.briar.api.protocol.Author;
import net.sf.briar.api.protocol.AuthorId; import net.sf.briar.api.protocol.AuthorId;
import net.sf.briar.api.protocol.Group; import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message; import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageId; import net.sf.briar.api.protocol.MessageId;
import net.sf.briar.api.protocol.ProtocolConstants; import net.sf.briar.api.protocol.ProtocolConstants;
@@ -57,10 +58,15 @@ class MessageReader implements ObjectReader<Message> {
parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class); parent = r.readUserDefined(Types.MESSAGE_ID, MessageId.class);
r.removeObjectReader(Types.MESSAGE_ID); r.removeObjectReader(Types.MESSAGE_ID);
} }
// Read the group // Read the group, if there is one
r.addObjectReader(Types.GROUP, groupReader); Group group = null;
Group group = r.readUserDefined(Types.GROUP, Group.class); if(r.hasNull()) {
r.removeObjectReader(Types.GROUP); r.readNull();
} else {
r.addObjectReader(Types.GROUP, groupReader);
group = r.readUserDefined(Types.GROUP, Group.class);
r.removeObjectReader(Types.GROUP);
}
// Read the author, if there is one // Read the author, if there is one
Author author = null; Author author = null;
if(r.hasNull()) { if(r.hasNull()) {
@@ -85,7 +91,7 @@ class MessageReader implements ObjectReader<Message> {
int signedByGroup = (int) counting.getCount(); int signedByGroup = (int) counting.getCount();
// Read the group's signature, if there is one // Read the group's signature, if there is one
byte[] groupSig = null; byte[] groupSig = null;
if(group.getPublicKey() == null) r.readNull(); if(group == null || group.getPublicKey() == null) r.readNull();
else groupSig = r.readBytes(Message.MAX_SIGNATURE_LENGTH); else groupSig = r.readBytes(Message.MAX_SIGNATURE_LENGTH);
// That's all, folks // That's all, folks
r.removeConsumer(counting); r.removeConsumer(counting);
@@ -103,7 +109,7 @@ class MessageReader implements ObjectReader<Message> {
} }
} }
// Verify the group's signature, if there is one // Verify the group's signature, if there is one
if(group.getPublicKey() != null) { if(group != null && group.getPublicKey() != null) {
try { try {
PublicKey k = keyParser.parsePublicKey(group.getPublicKey()); PublicKey k = keyParser.parsePublicKey(group.getPublicKey());
signature.initVerify(k); signature.initVerify(k);
@@ -118,7 +124,7 @@ class MessageReader implements ObjectReader<Message> {
messageDigest.update(raw); messageDigest.update(raw);
MessageId id = new MessageId(messageDigest.digest()); MessageId id = new MessageId(messageDigest.digest());
AuthorId authorId = author == null ? null : author.getId(); AuthorId authorId = author == null ? null : author.getId();
return new MessageImpl(id, parent, group.getId(), authorId, timestamp, GroupId groupId = group == null ? null : group.getId();
raw); return new MessageImpl(id, parent, groupId, authorId, timestamp, raw);
} }
} }