Use a checked exception instead of a boolean to indicate an invalid readable msg

This commit is contained in:
str4d
2016-07-12 11:43:19 +00:00
parent db52b2c29f
commit 2698e4c181
4 changed files with 24 additions and 23 deletions

View File

@@ -13,6 +13,7 @@ import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction; import org.briarproject.api.db.Transaction;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message; import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
@@ -35,16 +36,17 @@ public abstract class ReadableMessageManagerImpl
protected abstract Group getContactGroup(Contact c); protected abstract Group getContactGroup(Contact c);
protected abstract boolean incomingReadableMessage(Transaction txn, protected abstract void incomingReadableMessage(Transaction txn,
Message m, BdfList body, BdfDictionary meta) Message m, BdfList body, BdfDictionary meta)
throws DbException, FormatException; throws DbException, FormatException, InvalidMessageException;
@Override @Override
protected void incomingMessage(Transaction txn, Message m, BdfList body, protected void incomingMessage(Transaction txn, Message m, BdfList body,
BdfDictionary meta) throws DbException, FormatException { BdfDictionary meta) throws DbException, FormatException {
// Check if we accept this message // Check if we accept this message
if (incomingReadableMessage(txn, m, body, meta)) { try {
incomingReadableMessage(txn, m, body, meta);
// Update the group timestamp and unread count // Update the group timestamp and unread count
GroupId groupId = m.getGroupId(); GroupId groupId = m.getGroupId();
@@ -52,6 +54,7 @@ public abstract class ReadableMessageManagerImpl
boolean local = meta.getBoolean(LOCAL); boolean local = meta.getBoolean(LOCAL);
boolean read = meta.getBoolean(READ); boolean read = meta.getBoolean(READ);
updateGroupMetadata(txn, groupId, timestamp, local, read, read); updateGroupMetadata(txn, groupId, timestamp, local, read, read);
} catch (InvalidMessageException ignored) {
} }
} }

View File

@@ -26,6 +26,7 @@ import org.briarproject.api.introduction.IntroductionResponse;
import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message; import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus; import org.briarproject.api.sync.MessageStatus;
@@ -213,8 +214,9 @@ class IntroductionManagerImpl extends ReadableMessageManagerImpl
* in the introduction protocol and which engine we need to start. * in the introduction protocol and which engine we need to start.
*/ */
@Override @Override
protected boolean incomingReadableMessage(Transaction txn, Message m, BdfList body, protected void incomingReadableMessage(Transaction txn, Message m,
BdfDictionary message) throws DbException, FormatException { BdfList body, BdfDictionary message)
throws DbException, FormatException, InvalidMessageException {
// Get message data and type // Get message data and type
GroupId groupId = m.getGroupId(); GroupId groupId = m.getGroupId();
@@ -240,7 +242,7 @@ class IntroductionManagerImpl extends ReadableMessageManagerImpl
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
} }
deleteMessage(txn, m.getId()); deleteMessage(txn, m.getId());
return false; throw new InvalidMessageException();
} }
try { try {
introduceeManager.incomingMessage(txn, state, message); introduceeManager.incomingMessage(txn, state, message);
@@ -262,7 +264,7 @@ class IntroductionManagerImpl extends ReadableMessageManagerImpl
} catch (FormatException e) { } catch (FormatException e) {
LOG.warning("Could not find state for message, deleting..."); LOG.warning("Could not find state for message, deleting...");
deleteMessage(txn, m.getId()); deleteMessage(txn, m.getId());
return false; throw new InvalidMessageException();
} }
long role = state.getLong(ROLE, -1L); long role = state.getLong(ROLE, -1L);
@@ -277,7 +279,7 @@ class IntroductionManagerImpl extends ReadableMessageManagerImpl
"'. Deleting message..."); "'. Deleting message...");
} }
deleteMessage(txn, m.getId()); deleteMessage(txn, m.getId());
return false; throw new InvalidMessageException();
} }
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
@@ -296,11 +298,8 @@ class IntroductionManagerImpl extends ReadableMessageManagerImpl
LOG.warning("Unknown message type '" + type + "', deleting..."); LOG.warning("Unknown message type '" + type + "', deleting...");
} }
deleteMessage(txn, m.getId()); deleteMessage(txn, m.getId());
return false; throw new InvalidMessageException();
} }
// The message is valid
return true;
} }
@Override @Override

View File

@@ -21,6 +21,7 @@ import org.briarproject.api.messaging.PrivateMessageHeader;
import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message; import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus; import org.briarproject.api.sync.MessageStatus;
@@ -96,8 +97,9 @@ class MessagingManagerImpl extends ReadableMessageManagerImpl
} }
@Override @Override
protected boolean incomingReadableMessage(Transaction txn, Message m, BdfList body, protected void incomingReadableMessage(Transaction txn, Message m,
BdfDictionary meta) throws DbException, FormatException { BdfList body, BdfDictionary meta)
throws DbException, FormatException, InvalidMessageException {
// Broadcast event // Broadcast event
GroupId groupId = m.getGroupId(); GroupId groupId = m.getGroupId();
@@ -110,8 +112,6 @@ class MessagingManagerImpl extends ReadableMessageManagerImpl
PrivateMessageReceivedEvent event = new PrivateMessageReceivedEvent( PrivateMessageReceivedEvent event = new PrivateMessageReceivedEvent(
header, groupId); header, groupId);
txn.attach(event); txn.attach(event);
return true;
} }
@Override @Override

View File

@@ -31,6 +31,7 @@ import org.briarproject.api.sharing.SharingManager;
import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.Group; import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message; import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus; import org.briarproject.api.sync.MessageStatus;
@@ -182,8 +183,9 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
} }
@Override @Override
protected boolean incomingReadableMessage(Transaction txn, Message m, protected void incomingReadableMessage(Transaction txn, Message m,
BdfList body, BdfDictionary d) throws DbException, FormatException { BdfList body, BdfDictionary d)
throws DbException, FormatException, InvalidMessageException {
BaseMessage msg = BaseMessage.from(getIFactory(), m.getGroupId(), d); BaseMessage msg = BaseMessage.from(getIFactory(), m.getGroupId(), d);
SessionId sessionId = msg.getSessionId(); SessionId sessionId = msg.getSessionId();
@@ -219,7 +221,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
} catch (FormatException e) { } catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
deleteMessage(txn, m.getId()); deleteMessage(txn, m.getId());
return false; throw new InvalidMessageException();
} }
} else if (msg.getType() == SHARE_MSG_TYPE_ACCEPT || } else if (msg.getType() == SHARE_MSG_TYPE_ACCEPT ||
msg.getType() == SHARE_MSG_TYPE_DECLINE) { msg.getType() == SHARE_MSG_TYPE_DECLINE) {
@@ -254,9 +256,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
// message has passed validator, so that should never happen // message has passed validator, so that should never happen
throw new RuntimeException("Illegal Sharing Message"); throw new RuntimeException("Illegal Sharing Message");
} }
// The message is valid
return true;
} }
@Override @Override
@@ -362,7 +361,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
@Override @Override
public IM getInvitationMessage(ContactId contactId, MessageId messageId) public IM getInvitationMessage(ContactId contactId, MessageId messageId)
throws DbException { throws DbException {
Transaction txn = db.startTransaction(false); Transaction txn = db.startTransaction(true);
try { try {
Contact contact = db.getContact(txn, contactId); Contact contact = db.getContact(txn, contactId);
Group group = getContactGroup(contact); Group group = getContactGroup(contact);