Throw custom error message from behaviour

cancel
Showing results for 
Search instead for 
Did you mean: 
manav
Active Member II

Throw custom error message from behaviour

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...

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.

2 Replies
afaust
Master

Re: Throw custom error message from behaviour

You cannot make this work with TRANSACTION_COMMIT - it is just as simple as that. When using EVERY_EVENT, you have to consider the sequence of behaviour events and pick the right behaviour / policy to implement. Before content can be written to a node, the node first has to be created - so it is natural that the OnCreateNodePolicy will not have access to the content reader yet, and you might want to choose OnContentUpdatePolicy instead - though you need to be careful as this is also triggered on any subsequent update. One way to deal with this would be to combine OnCreateNodePolicy and OnContentUpdatePolicy, i.e. use OnCreateNodePolicy to record the node currently being created in a transactional resource (use TransactionalResourceHelper for this) and then check when OnContentUpdatePolicy fires.

manav
Active Member II

Re: Throw custom error message from behaviour

Thanks for your valuable response @afaust 

As suggested, I have updated my code like below


public void registerEventHandlers() {
eventManager.bindClassBehaviour(ContentServicePolicies.OnContentUpdatePolicy.QNAME, ContentModel.TYPE_CONTENT,
new JavaBehaviour(this, "onUpdateDocument", Behaviour.NotificationFrequency.EVERY_EVENT));
}

public void onUpdateDocument(NodeRef nodeRef, boolean newContent) throws NoSuchAlgorithmException, IOException {
NodeRef docRef = nodeRef;

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");
}
}
}

But still, I am getting the generic response

{
"status": {
"code": 500,
"name": "Internal Error",
"description": "An error inside the HTTP server which prevented it from fulfilling the request."
},
"message": "Server error (06140025). Details can be found in the server logs.",
"exception": "",
"callstack": [],
"server": "Community v5.2.0 (r135134-b14) schema 10,005",
"time": "14-Jul-2020 18:57:00"
}


Would you please review the above and suggest the changes.