public static ClassifyDocumentResult create(ConditionEngineResult result, ConditionEngineMetadata conditionEngineMetadata) { ClassifyDocumentResult classifyDocumentResult = new ClassifyDocumentResult(result); try { classifyDocumentResult.signature = conditionEngineMetadata.compressAndEncode(result); } catch (IOException e) { logger.warn("Unable to compress and encode ConditionEngineResult to signature, continuing without.", e); return new ClassifyDocumentResult(result); } return classifyDocumentResult; }
/** * Verifies that a hash matches the supplied classify result. */ public boolean isValidHash(ConditionEngineResult conditionEngineResult, String hash){ String generatedHash = generateSecurityHash(conditionEngineResult); return generatedHash.equals(hash); }
public ConditionEngineResult createResult(Multimap<String, String> metadata) { //if the eval info and hash are not present, disregard them if (!metadata.containsKey(DocumentFields.EvaluationInformationBlob) || !metadata.containsKey(DocumentFields.MetadataHash)) { logger.debug("No previous evaluation info or corresponding hash supplied - starting evaluation from scratch."); return new ConditionEngineResult(); } //if there are more than 1 of the has fields, disregard them Collection<String> allBlobFields = metadata.get(DocumentFields.EvaluationInformationBlob); Collection<String> allHashFields = metadata.get(DocumentFields.MetadataHash); if (allBlobFields.size() != 1 || allHashFields.size() != 1) { logger.debug(String.format("Incorrect number of blob fields %d or condition fields %d found, ignoring.", allBlobFields.size(), allHashFields.size())); return new ConditionEngineResult(); } String blob = allBlobFields.stream().findFirst().get(); String hash = allHashFields.stream().findFirst().get(); return validateConditionEngineResult(blob, hash); }
private ConditionEngineResult validateConditionEngineResult(String blob, String hash) { //deserialize the blob and validate with the hash, if all checks out then we're good. try { ConditionEngineResult conditionEngineResult = mapper.readValue(ZipUtils.decompressEncodedString(blob), ConditionEngineResult.class); if (!isValidHash(conditionEngineResult, hash)) { logger.info("Invalid hash for deserialized classify result, ignoring."); return new ConditionEngineResult(); } return conditionEngineResult; } catch (IOException e) { logger.debug("There was a problem de-serializing ClassifyDocumentResult, ignoring.", e); return new ConditionEngineResult(); } } /**
private void setupPreevaluatedInformation(Document document) { // Check if we have any supplied metadata which has been evaluated before. ConditionEngineResult result = conditionEngineMetadata.createResult(document.getMetadata()); applyEvaluationInfo(result, this); }
ConditionEngineResult previousResult = conditionEngineMetadata.createResultFromMetadata(document.getMetadata());
/** * This method will REMOVE any existing temp (e.g. validation hash) metadata fields, and add new ones that represent * this metadata. * @param document The document who's metadata should be updated. */ public void applyTemporaryMetadataToDocument( ConditionEngineResult conditionEngineResult, Document document ){ try { //Lets remove all the field values document.getMetadata().removeAll(DocumentFields.EvaluationInformationBlob); document.getMetadata().removeAll(DocumentFields.MetadataHash); //now lets get new values and add them to the document String sourceJson = mapper.writeValueAsString(conditionEngineResult); String compressedBlob = ZipUtils.compressStringAndEncode(sourceJson); String securityHash = generateSecurityHash(conditionEngineResult); document.getMetadata().put(DocumentFields.EvaluationInformationBlob, compressedBlob); document.getMetadata().put(DocumentFields.MetadataHash, securityHash); } catch (IOException e) { CpeException cpeException = new BackEndRequestFailedCpeException(BackEndRequestFailedErrors.UnableToAddTemporaryMetadata,e ); logger.error("Error adding temporary evaluation metadata to a document.", cpeException); throw cpeException; } }
public ConditionEngineResult createResultFromMetadata(Multimap<String, MetadataValue> metadata){ //if the eval info and hash are not present, disregard them if(!metadata.containsKey(DocumentFields.EvaluationInformationBlob) || !metadata.containsKey(DocumentFields.MetadataHash)){ logger.debug("No previous evaluation info or corresponding hash supplied - starting evaluation from scratch."); return new ConditionEngineResult(); } //if there are more than 1 of the has fields, disregard them Collection<MetadataValue> allBlobFields = metadata.get(DocumentFields.EvaluationInformationBlob); Collection<MetadataValue> allHashFields = metadata.get(DocumentFields.MetadataHash); if(allBlobFields.size() != 1 || allHashFields.size() != 1) { logger.debug(String.format("Incorrect number of blob fields %d or condition fields %d found, ignoring.", allBlobFields.size(), allHashFields.size())); return new ConditionEngineResult(); } MetadataValue blob = allBlobFields.stream().findFirst().get(); MetadataValue hash = allHashFields.stream().findFirst().get(); //deserialize the blob and validate with the hash, if all checks out then we're good. return validateConditionEngineResult(blob.getAsString(), hash.getAsString()); }