That works, yet it adds it to the root node.

Now I just need to traverse into the bar folder.

```
Node root, folder;
root = session.getRootNode();

// how to traverse into "bar" folder?

Node child = root.addNode("grade.png", JcrConstants.NT_FILE);
Node content = child.addNode(JcrConstants.JCR_CONTENT,
        JcrConstants.NT_RESOURCE);
// Set the property for the node.
content.setProperty(JcrConstants.JCR_DATA,
root.getSession().getValueFactory().createBinary(new
        FileInputStream("grade.png")));
content.setProperty(JcrConstants.JCR_MIMETYPE,
        "image/png");
Calendar rightNow = Calendar.getInstance();
content.setProperty(JcrConstants.JCR_LASTMODIFIED, rightNow);

session.save();
```

On Thu, Aug 26, 2021 at 02:16:18PM -0700, Eric Norman wrote:
> Actually, ignore that last repy.  I now see that you have redefined the
> root variable to be the bar node.
> 
> I will say that if you want the node to be treated as a file (from webdav),
> then you probably will want the structure to look something like below
> where the data is stored under a jcr:content node like below:
> 
>     Node child = root.addNode("grade.png", JcrConstants.NT_FILE);
>     Node content = child.addNode(JcrConstants.JCR_CONTENT,
> JcrConstants.NT_RESOURCE);
>     // Set the property for the node.
>     content.setProperty(JcrConstants.JCR_DATA,
>             root.getSession().getValueFactory().createBinary(new
> FileInputStream("grade.png")));
>     content.setProperty(JcrConstants.JCR_MIMETYPE, "image/png");
>     content.setProperty(JcrConstants.JCR_LASTMODIFIED, rightNow);
> 
> Regards,
> -Eric
> 
> On Thu, Aug 26, 2021 at 2:03 PM Eric Norman <enor...@apache.org> wrote:
> 
> > Maybe this a typo?  It looks like your sample code added the child to the
> > root node, not under "/bar" where you were doing the lookup.
> >
> > Node child = *root*.addNode("grade.png");
> >
> >
> > On Thu, Aug 26, 2021 at 1:47 PM Brian E. Lavender <br...@brie.com> wrote:
> >
> >> I still can not seem to add a file.
> >>
> >> I went through the webdav interface and I created a 'folder' called bar.
> >> Then, I tried to programmatically add a file to that folder. I am
> >> getting nothing.
> >>
> >> ```java
> >> // get the path for where to add the file.
> >> // We want to put it in the `bar` folder
> >> Node root;
> >> root = session.getRootNode();
> >> root = root.getNode("bar");
> >>
> >>
> >> System.out.print("Adding binary ");
> >> Calendar rightNow = Calendar.getInstance();
> >> // Add a node for the file
> >> // It fails here
> >> Node child = root.addNode("grade.png");
> >>
> >> // Set the property for the node.
> >> child.setProperty(JcrConstants.JCR_DATA,
> >>         root.getSession().getValueFactory().createBinary(new
> >> FileInputStream("grade.png")));
> >> child.setProperty(JcrConstants.JCR_MIMETYPE, "image/png");
> >> child.setProperty(JcrConstants.JCR_LASTMODIFIED, rightNow);
> >>
> >> session.save();
> >> ```
> >>
> >> ```
> >> javax.jcr.nodetype.ConstraintViolationException: No child node
> >> definition for grade.png found in node /bar
> >> <snip>
> >> at
> >> org.apache.jackrabbit.rmi.server.ServerObject.getRepositoryException(ServerObject.java:109)
> >> at org.apache.jackrabbit.rmi.client.ClientNode.addNode(ClientNode.java:95)
> >> at gov.ca.brea.jackrabbit.FifthHop.main(FifthHop.java:74)
> >>
> >>
> >>
> >> On Thu, Aug 26, 2021 at 01:04:35AM +0200, Jakub Kaniewski wrote:
> >> > For this you use ValueFactory
> >> >
> >> > Example
> >> >
> >> node.setProperty(JcrConstants.JCR_DATA,node.getSession().getValueFactory().createBinary
> >> <
> >> https://www.tabnine.com/code/java/methods/javax.jcr.ValueFactory/createBinary>(new
> >> FileInputStream(“/tmp/test.jpg)));
> >> >
> >> >
> >> > —
> >> > Jakub Kaniewski | 666 831 500 | jakub.kaniew...@gmail.com
> >> >
> >> >
> >> >
> >> >
> >> > > On 26 Aug 2021, at 00:59, Brian E. Lavender <br...@brie.com> wrote:
> >> > >
> >> > > How is it I add a binary to a Node?
> >> > >
> >> > > I see the following method.
> >> > > Property setProperty(java.lang.String name, Binary value)
> >> > >
> >> > > I see that Binary is an interface, yet I am not sure how to create a
> >> > > Binary from a File that I open.
> >> > >
> >> > > Or, stated another way, say I want to add a jpg image from my disk to
> >> > > a node. Would I use this setProperty method? How would I do it?
> >> > >
> >> > > Brian
> >> > > --
> >> > > Brian Lavender
> >> > > http://www.brie.com/brian/
> >> > >
> >> > > "There are two ways of constructing a software design. One way is to
> >> > > make it so simple that there are obviously no deficiencies. And the
> >> other
> >> > > way is to make it so complicated that there are no obvious
> >> deficiencies."
> >> > >
> >> > > Professor C. A. R. Hoare
> >> > > The 1980 Turing award lecture
> >> >
> >>
> >> --
> >> Brian Lavender
> >> http://www.brie.com/brian/
> >>
> >> "There are two ways of constructing a software design. One way is to
> >> make it so simple that there are obviously no deficiencies. And the other
> >> way is to make it so complicated that there are no obvious deficiencies."
> >>
> >> Professor C. A. R. Hoare
> >> The 1980 Turing award lecture
> >>
> >

-- 
Brian Lavender
http://www.brie.com/brian/

"There are two ways of constructing a software design. One way is to
make it so simple that there are obviously no deficiencies. And the other
way is to make it so complicated that there are no obvious deficiencies."

Professor C. A. R. Hoare
The 1980 Turing award lecture

Reply via email to