I have a requirement to scan the input stream of the file (through some business logic). If the file is valid then only it should be allowed to be added in alfresco. If the file is invalid I want to give a custom message so the caller of upload API would get to know about file invalidation. To achieve this I have created a custom behavior below is its code.
public void registerEventHandlers() {
eventManager.bindClassBehaviour(NodeServicePolicies.OnCreateNodePolicy.QNAME, ContentModel.TYPE_CONTENT,
new JavaBehaviour(this, "onAddDocument", Behaviour.NotificationFrequency.TRANSACTION_COMMIT));
}
public void onAddDocument(ChildAssociationRef parentChildAssocRef) throws NoSuchAlgorithmException, IOException {
NodeRef docRef = parentChildAssocRef.getChildRef();
ContentReader inputReader = serviceRegistry.getContentService().getReader(docRef, ContentModel.PROP_CONTENT);
if (inputReader != null) {
InputStream inputStream = inputReader.getContentInputStream();
if (!scanStream.isValid(inputStream)) {
throw new AlfrescoRuntimeException("ss.invalidFile");
}
}
}
The problem is I am not able to see the message thrown by me in the response to upload API.
https://hub.alfresco.com/t5/alfresco-content-services-forum/error-custom-messages-throwing-exception/td-p/295444
I have also referred the above link but if I change frequency to EVERY_EVENT getting null value in inputReader object.
I would be appreciable if anyone could help me to achieve this.