Unverified Commit ab7cc130 authored by akwizgran's avatar akwizgran
Browse files

Added QueueMessage support to BdfMessageValidator.

parent a4d6de03
......@@ -2,6 +2,8 @@ package org.briarproject.clients;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.QueueMessage;
import org.briarproject.api.clients.QueueMessageValidator;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
......@@ -14,10 +16,12 @@ import org.briarproject.util.StringUtils;
import java.util.logging.Logger;
import static org.briarproject.api.clients.QueueMessage.QUEUE_MESSAGE_HEADER_LENGTH;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
public abstract class BdfMessageValidator implements MessageValidator {
public abstract class BdfMessageValidator implements MessageValidator,
QueueMessageValidator {
protected static final Logger LOG =
Logger.getLogger(BdfMessageValidator.class.getName());
......@@ -38,6 +42,15 @@ public abstract class BdfMessageValidator implements MessageValidator {
@Override
public Metadata validateMessage(Message m, Group g) {
return validateMessage(m, g, MESSAGE_HEADER_LENGTH);
}
@Override
public Metadata validateMessage(QueueMessage q, Group g) {
return validateMessage(q, g, QUEUE_MESSAGE_HEADER_LENGTH);
}
private Metadata validateMessage(Message m, Group g, int headerLength) {
// Reject the message if it's too far in the future
long now = clock.currentTimeMillis();
if (m.getTimestamp() - now > MAX_CLOCK_DIFFERENCE) {
......@@ -45,9 +58,13 @@ public abstract class BdfMessageValidator implements MessageValidator {
return null;
}
byte[] raw = m.getRaw();
if (raw.length <= headerLength) {
LOG.info("Message is too short");
return null;
}
try {
BdfList message = clientHelper.toList(raw, MESSAGE_HEADER_LENGTH,
raw.length - MESSAGE_HEADER_LENGTH);
BdfList message = clientHelper.toList(raw, headerLength,
raw.length - headerLength);
BdfDictionary meta = validateMessage(message, g, m.getTimestamp());
if (meta == null) {
LOG.info("Invalid message");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment