How to retrieve data from Alfresco WCM repository using Java backed web-scripts?
Retrieveing contents from Alfresco WCM repository constitutes of two things:-
1) Retrieving the contents of a particular file.
2) Retrieving a list of files from a particular location.
We were to achieve this by using Java-backed web-scripts and the example script at http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#SimpleWebScript.java was very useful.
The abstract class “AbstractRepositoryWebScript.java” used in this example provides a base for how to work with Alfresco Java API. The ServiceRegistry instance that it gets through springs provides handle to various other services that are very useful. Lets see how we can use this bean to retrieve contents from alfresco.
1) Nodes in alfresco are identified by their alfresco node reference. This reference for nodes in alfresco wcm are very similar to their actual content path. For example: Our web projects name is “test”. In our project, the path to a file that we want to retrieve is “media/releases/content/press-release1.xml”. The alfresco node reference for this file will be
“avm://test/-1;www;avm_webapps;ROOT;media;releases;content;press-release1.xml”. The point is, that “avm://test/-1;www;avm_webapps;ROOT” is the reference to the staging sandbox of the “test” web project. It can be seen very clearly that the rest of the reference is actually the path to the file separated by ‘;’ instead of ‘/’. So when we want a reference to this file, we can create a reference by:
NodeRef nodeRef = new NodeRef("avm://test/-1;www;avm_webapps;ROOT;media;releases;content;press-release1.xml");
Now to stream back the contents of this file we can use the output function defined in the AbstractRepositorywebScript in the above mentioned example.
2) The second issue that we faced was to retrieve the list of files in a particular folder/space in our WCM repository. Lets say we want to get a list of files in the folder “media/releases.contents”. ServiceRegistry provides a lot of services which include the Nodeservice, AVMNodeService, FileFolderService, etc. The one which i found useful for retrieving a list of files from a particular folder in our WCM repository was the FileFolderService. It has a method “lookup”, which searches for files matching a particular name pattern mentioned. The parameters that this function takes are:
• Noderef to the parent node from which it will start searching.
• The string name pattern to be searched. e.g. “*.xml” or “*.*”.
• Boolean value to search in sub-folders or not.
Using these parameters, this function returns a List of FileInfo objects matching the criteria.
So if we want to get a list of all xml files in our media/releases folder but do not want the searching to be done its sub-folders, then our script will look like:
NodeRef parentNode = new NodeRef("avm://test/-1;www;avm_webapps;ROOT;media;releases");
List<FileInfo> files = this.services.getFileFolderService.lookup(parentNode,"*.xml",false);
Your feedback and suggestions are welcome.
Thanks for this. searching for this kind of AVM related web script for the past 6 months. very helpful.