meerkat-java/voter-registry/src/main/java/meerkat/Registry/RelevantDataCallBack.java

146 lines
5.1 KiB
Java

package meerkat.Registry;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.MessageValidator;
import meerkat.VoterRegistry.RegistryCallBack;
import meerkat.VoterRegistryMessage;
import meerkat.bulletinboard.BulletinBoardClient.ClientCallback;
import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.Crypto;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static meerkat.Registry.CollectionMessagesUtils.*;
/**
* TODO : add logging
*/
/**
* Created by Vladimir Eliezer Tokarev on 2/19/2016.
* Object that handles the GetGroups or the GetPersonIDDetails of the simpleRegistry
*/
public class RelevantDataCallBack implements ClientCallback<List<BulletinBoardMessage>>, MessageValidator<List<BulletinBoardMessage>> {
public RegistryCallBack callback;
protected DigitalSignature validator;
protected Crypto.Signature signature;
protected InputStream certificateStream;
/**
* Init BooleanCallBack
* @param callback voter registry callback object
* @param validator DigitalSignature object
* @param signature Crypto.Signature object
*/
public RelevantDataCallBack(RegistryCallBack callback,
DigitalSignature validator,
Crypto.Signature signature,
InputStream certificateStream) {
this.callback = callback;
this.validator = validator;
this.signature = signature;
this.certificateStream = certificateStream;
}
/**
* Checks if the given list have tags of type GROUP_ID_TAG or else
* @param msg List<BulletinBoardAPI.BulletinBoardMessage>
* @return true if the messages are with GROUP_ID_TAG tags
*/
private boolean isAddToGroupsList(List<BulletinBoardMessage> msg) {
List<String> tags = msg.get(0).getMsg().getTagList();
for (int i = 0 ;i < tags.size(); i++) {
String tag = tags.get(i);
if(tag.contains(RegistryTags.GROUP_ID_TAG))
{
return true;
}
}
return false;
}
/**
* Checks if the list of messages is of type GROUP_ID_TAG if it is then calls
* HandleResult with mapping of the latest groups, else calls to HandleResult with
* the latest tag from this list (in case of personal data)
*
* @param msg List<BulletinBoardAPI.BulletinBoardMessage>
*/
@Override
public void handleCallback(List<BulletinBoardMessage> msg) {
try {
List<VoterRegistryMessage> messages = ConvertToVoterRegistryMessages(validate(msg));
if(isAddToGroupsList(msg)) {
Map<String, VoterRegistryMessage> map = GetLatestGroupsActions(messages);
List<String> groupsOfUser = GetListOfGroupIds(map);
callback.HandleResult(groupsOfUser);
}
else {
callback.HandleResult(GetLatestMessage(messages));
}
} catch (ValidationError | ParseException | EmptyListException
| InvalidProtocolBufferException validationError) {
callback.HandleResult(null);
validationError.printStackTrace();
}
}
/**
* Calls the callback HandleResult method with false because the post method failed
* @param t the exception data that have been thrown during the failure of the post method
*/
@Override
public void handleFailure(Throwable t) {
callback.HandleResult(null);
}
/**
* verify that the message is valid
* @param message BulletinBoardMessage instance
* @return true if the message is valid else false
*/
private boolean VerifyMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message)
{
try {
validator.loadVerificationCertificates(certificateStream);
validator.initVerify(signature);
validator.updateContent(message);
return validator.verify();
} catch (CertificateException | InvalidKeyException | SignatureException e) {
e.printStackTrace();
return false;
}
}
/**
* Gets all the valid messages from the list of bulletin board messages
* @param object object which will be checked
* @return List<BulletinBoardMessage>
* @throws ValidationError
*/
@Override
public List<BulletinBoardMessage> validate(List<BulletinBoardMessage> object) throws ValidationError {
List<BulletinBoardMessage> verifiedMessages = new ArrayList<>();
for (int i = 0 ; i < object.size() ; i++)
{
BulletinBoardMessage message = object.get(i);
if(VerifyMessage(message.getMsg()))
{
verifiedMessages.add(message);
}
}
return verifiedMessages;
}
}