2. Code Example

Example 1. Node Creation (Excerpt from JcrNodeCreationTest)

This code using JcrTemplate creates a node if one doesn't already exist in the repository.

                
private String nodeName = "fileFolder";

...

public void testAddNodeIfDoesntExist() {
    assertNotNull("JCR Template is null.", template);

    template.execute(new JcrCallback() {
        public Object doInJcr(Session session) throws RepositoryException {
            Node root = session.getRootNode();

            logger.debug("Starting from root node.  node={}", root);

            Node node = null;

            if (root.hasNode(nodeName)) {
                node = root.getNode(nodeName);

                logger.debug("Node exists.  node={}", node);
            } else {
                node = root.addNode(nodeName);

                session.save();

                logger.info("Saved node.  node={}", node);
            }

            assertNotNull("Node is null.", node);

            return node;
        }
    });
}
                
            

Example 2. File Node Creation (Excerpt from JcrNodeCreationTest)

This code using JcrTemplate creates a file node by attaching one if one doesn't already exist in the repository.

If the file node doesn't exist, it is created from the log4j.xml file in the classpath by passing in an InputStream of the file. After this you will see the part of the unit test that retrieves the file node and checks if the first and last line are correct.

The first thing that is done is to retrieve the root node from the JCR Session by calling session.getRootNode(). Then the expected folder node that should contain the file is located just under the root node, so root.getNode(nodeName) is called to retrieve the folder node. Then if the file node doesn't exist under the folder node, it's created. First a node named based on the file name is created under the folder node, then a JCR content node is created under the file node and a data property is where the actual file (as an InputStream) is set.

                
private String nodeName = "fileFolder";

...

public void testAddFileIfDoesntExist() {
    Node node = (Node) template.execute(new JcrCallback() {
        @SuppressWarnings("unchecked")
        public Object doInJcr(Session session) throws RepositoryException,
                IOException {
            Node resultNode = null;

            Node root = session.getRootNode();
            logger.info("starting from root node.  node={}", root);

            // should have been created in previous test
            Node folderNode = root.getNode(nodeName);

            String fileName = "log4j.xml";

            if (folderNode.hasNode(fileName)) {
                logger.debug("File already exists.  file={}", fileName);
            } else {
                InputStream in = this.getClass().getResourceAsStream("/" + fileName);

                Node fileNode = folderNode.addNode(fileName, JcrConstants.NT_FILE);

                // create the mandatory child node - jcr:content
                resultNode = fileNode.addNode(JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE);

                resultNode.setProperty(JcrConstants.JCR_MIMETYPE, "text/xml");
                resultNode.setProperty(JcrConstants.JCR_ENCODING, "UTF-8");
                resultNode.setProperty(JcrConstants.JCR_DATA, in);
                Calendar lastModified = Calendar.getInstance();
                lastModified.setTimeInMillis(System.currentTimeMillis());
                resultNode.setProperty(JcrConstants.JCR_LASTMODIFIED, lastModified);

                session.save();

                IOUtils.closeQuietly(in);

                logger.debug("Created '{}' file in folder.", fileName);
            }

            ...
        }
    });
}
                
            

Example 3. File Node Retrieval (Excerpt from JcrNodeCreationTest)

This code using JcrTemplate to retrieve the content of a file node from the repository.

                
private String nodeName = "fileFolder";

...

public void testAddFileIfDoesntExist() {
    Node node = (Node) template.execute(new JcrCallback() {
        public Object doInJcr(Session session) throws RepositoryException,
                IOException {
            Node resultNode = null;

            JcrConstants jcrConstants = new JcrConstants(session);

            Node root = session.getRootNode();
            logger.info("starting from root node.  node={}", root);

            // should have been created in previous test
            Node folderNode = root.getNode(nodeName);

            String fileName = "log4j.xml";

...

            Node contentNode = folderNode.getNode(fileName).getNode(JcrConstants.JCR_CONTENT);

            Property dataProperty = contentNode.getProperty(JcrConstants.JCR_DATA);

            List<String> list = (List<String>) IOUtils.readLines(dataProperty.getStream());

...
        }
    });
}