2 Replies Latest reply on Oct 17, 2012 11:38 AM by u005078

    How to add a document to the JCR Repository via Java API

    u005078 Level 1

      I'm trying to get started with the JCR 2.0 " Content Repository Java Technology API Specification" It's really a great document but I miss a minimum of examples to visualize how it is all fits together.

      For a start I want to add a pdf-document to my local CRX repository. Form the book I see, that I must add a Node and add the Properties.


      File file = new File("/mypath/mydocument.pdf"); 
      FileInputStream is = new FileInputStream(file); 
      ValueFactory valueFactory = session.getValueFactory(); 
      Binary myBinary = valFact.createBinary(is); 
      Value valueFactory =valFact.createValue(myBinary);
      Node nodeCarl=content.addNode("Carl", "nt:file");
      Property pCarl=nodeCarl.setProperty("jcr:data", myBinary); 

      The program fails with:

      javax.jcr.nodetype.ConstraintViolationException: no matching property definition found for {http://www.jcp.org/jcr/1.0}data in the lastt cited line.

      I don't know, why this comes up with ..jcr/1.0; The Classpath consists of JCR-2.0.jar and jackrabbit-standalone-2.2.13.jar.


      Any hint to get this stored is appreciated.


        • 1. Re: How to add a document to the JCR Repository via Java API
          MarcelBoucher Adobe Employee

          The reason you are getting a ConstraintViolationException is because you are creating a node of type nt:file. The nt:file node as per the JCR specification[0], requires a child node jcr:content which contains the actual file contents.

          Here is an updated snippet of Java code I created to store a PDF file into CRX:


          File file = new File("/mypath/mydocument.pdf");   
          FileInputStream is = new FileInputStream(file); 
          Node node = _session.getNode(parentNode);  
          ValueFactory valueFactory = session.getValueFactory();             
          Binary contentValue = valueFactory.createBinary(is);            
          Node fileNode = node.addNode(fileName, "nt:file"); 
          Node resNode = fileNode.addNode("jcr:content", "nt:resource"); 
          resNode.setProperty("jcr:mimeType", mimeType); 
          resNode.setProperty("jcr:data", contentValue); 
          Calendar lastModified = Calendar.getInstance(); 
          resNode.setProperty("jcr:lastModified", lastModified); 
          // Return the path to the document that was stored in CRX. 
          return fileNode.getPath();



          Hope this helps



          [0] http://www.day.com/specs/jcr/2.0/3_Repository_Model.html# nt:file

          • 2. Re: How to add a document to the JCR Repository via Java API
            u005078 Level 1

            Thank you - this fixed it.