Search code examples
jackrabbitjcr

Update content of node in JCR 2.0


I try to update node in JCR 2.0

InputStream content = node.getProperty("jcr:content").getProperty("jcr:data").getBinary().getStream();

//TODO same with stream
Binary value = ...;

Node contentNode = node.getProperty("jcr:content");
contentNode.setProperty("jcr:content", value);

And I get exception "javax.jcr.nodetype.ConstraintViolationException: Item is protected". What`s wrong?


Solution

  • The "jcr:content" you're referring to is typically the name of a child node (usually of type nt:resource, or something similar), rather than a property. Thus your code sample should rather be:

    // read value
    Binary value = node.getNode("jcr:content").getProperty("jcr:data").getBinary();
    
    // update value
    Binary value = ...;
    node.getNode("jcr:content").setProperty("jcr:data", value);
    

    See also the putFile() utility methods in the JcrUtils class of the jackrabbit-jcr-commons library.