2. Code Example

Example 1. Content Exporter

The example shows calling JcrContentExporter which is defined in the Spring configuration. It just needs a reference to the JcrContentRecurser, which is automatically loaded by the context:component-scan since it has an @Component annotation, and also a file path where to export all content nodes in the repository.

This is an excerpt from JcrContentExporterIT.

                
@Autowired
private JcrContentExporter processor = null;

...

public void testJcrContentProcessor() {
...
        
    List<String> lResults = processor.process();

...
}
                
            

Example 2. Using JcrContentRecurser

This example is from JcrContentExporter. It processes each content node and writes the content to an export directory, but obviously any type of processing can be done on the content node inside the callback.

For every content node found while recursing the repository JcrNodeCallback.doInJcrNode(Session session, Node node) is called. The file name and relative path from the root node is derived and then the content is retrieved and written to the export directory.

                
public List<String> process() {
    final List<String> lResults = new ArrayList<String>();
    
    contentRecurser.recurse(new JcrNodeCallback() {
        public void doInJcrNode(Session session, Node node)
                throws IOException, RepositoryException {
            // file name node is above content
            String fileName = node.getParent().getName();
            // file path is two above content
            String path = node.getParent().getParent().getPath();
            
            logger.debug("In content recurser callback." + 
                         "  fileName='{}' path='{}'", fileName, path);

            File fileDir = new File(rootFolder + path);
            File file = new File(fileDir, fileName);
    
            Property dataProperty = node.getProperty(JcrConstants.JCR_DATA);
    
            InputStream in = null;
            OutputStream out = null;
            
            try {
                FileUtils.forceMkdir(fileDir);
                
                in = dataProperty.getStream();
                out = new FileOutputStream(file);
                
                IOUtils.copy(in, out);
                
                lResults.add(path + File.pathSeparator + fileName);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }
    });
    
    return lResults;
}
                
            

Example 3. Using JcrRecurser

The previous example of JcrContentRecurser is a very simple subclass of JcrRecurser. If it no specific node matching is set on it, it's callback will be called for every node in the repository. But a Setof specific node names to match can be passed in either through the constructor or methods.

Below is JcrContentRecurser which just sets the content node as the only node to have callbacks performed.

                
public class JcrContentRecurser extends JcrRecurser {

    final Logger logger = LoggerFactory.getLogger(JcrContentRecurser.class);

    /**
     * Constructor
     */
    public JcrContentRecurser() {
        super(JcrConstants.JCR_CONTENT); 
    }

}