You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by "Brian E. Lavender" <br...@brie.com> on 2021/08/25 22:59:58 UTC

how to add a binary to a node?

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

Re: how to add a binary to a node?

Posted by "Brian E. Lavender" <br...@brie.com>.
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 <en...@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.kaniewsky@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

Re: how to add a binary to a node?

Posted by Eric Norman <en...@apache.org>.
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 <en...@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.kaniewsky@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
>>
>

Re: how to add a binary to a node?

Posted by Eric Norman <en...@apache.org>.
>
> Maybe it was because I didn't put in the JcrConstants.NT_FILE
> before?


Yes, that was likely contributing.  The Node#addNode call without
specifying a node type argument will choose the child node type by
inspecting the child node definitions for the parent node type (nt:folder
in this case).  A quick look at the nt:folder node type definition shows
that it allows children with any name whose node type is "nt:heirarchyNode"
(or derived from that type).

I haven't tried this, but I believe in your example it may have
chosen "nt:heirarchyNode" as the node type for the new child which wouldn't
be valid since that is an abstract node type.

Adding the JcrConstants.NT_FILE argument to the addNode call makes the
configuration valid for a child of an nt:folder node.

Regards,
-Eric

On Thu, Aug 26, 2021 at 3:33 PM Brian E. Lavender <br...@brie.com> wrote:

> Hmmm, it seems to work now for adding the file to the folder. See my
> code below. I was contrasting it to my code that previously didn't work.
>
> Maybe it was because I didn't put in the JcrConstants.NT_FILE
> before?
>
>
> ```java
> Node root, folder;
> root = session.getRootNode();
> folder = root.getNode("bar");
>
> Node child = folder.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);
> ```
>
> On Thu, Aug 26, 2021 at 02:28:30PM -0700, Eric Norman wrote:
> > >
> > > "No child node definition for grade.png found in node /bar"
> >
> >
> > I see.  Well, the folder you created via webdav must be a "nt:folder"
> node
> > type?  In that case, the type of the child must extend "nt:hierarchyNode"
> > to conform with the "nt:folder" node type definition.
> >
> > In the example I provided in my last reply,  the child node is created
> > using the JcrConstants.NT_FILE node type which would satisfy that
> condition
> > since "nt:file" extends the "nt:hierarchyNode" node type.
> >
> > Hope that helps.
> >
> > Regards,
> > -Eric
> >
> > On Thu, Aug 26, 2021 at 2:17 PM Brian E. Lavender <br...@brie.com>
> wrote:
> >
> > > I clarified the root node and made a new variable to hold the node for
> > > the bar folder.
> > >
> > > ```java
> > > // bar is a folder created through the webdav interface.
> > > Node root,folder;
> > > root = session.getRootNode();
> > > folder = root.getNode("bar");
> > >
> > > // fails here
> > > Node child = folder.addNode("grade.png");
> > > ```
> > >
> > > Error message is the following
> > >
> > > "No child node definition for grade.png found in node /bar"
> > >
> > > On Thu, Aug 26, 2021 at 02:03:27PM -0700, Eric Norman 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.kaniewsky@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
> > >
>
> --
> 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
>

Re: how to add a binary to a node?

Posted by "Brian E. Lavender" <br...@brie.com>.
Hmmm, it seems to work now for adding the file to the folder. See my
code below. I was contrasting it to my code that previously didn't work.

Maybe it was because I didn't put in the JcrConstants.NT_FILE
before?


```java
Node root, folder;
root = session.getRootNode();
folder = root.getNode("bar");

Node child = folder.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);
```

On Thu, Aug 26, 2021 at 02:28:30PM -0700, Eric Norman wrote:
> >
> > "No child node definition for grade.png found in node /bar"
> 
> 
> I see.  Well, the folder you created via webdav must be a "nt:folder" node
> type?  In that case, the type of the child must extend "nt:hierarchyNode"
> to conform with the "nt:folder" node type definition.
> 
> In the example I provided in my last reply,  the child node is created
> using the JcrConstants.NT_FILE node type which would satisfy that condition
> since "nt:file" extends the "nt:hierarchyNode" node type.
> 
> Hope that helps.
> 
> Regards,
> -Eric
> 
> On Thu, Aug 26, 2021 at 2:17 PM Brian E. Lavender <br...@brie.com> wrote:
> 
> > I clarified the root node and made a new variable to hold the node for
> > the bar folder.
> >
> > ```java
> > // bar is a folder created through the webdav interface.
> > Node root,folder;
> > root = session.getRootNode();
> > folder = root.getNode("bar");
> >
> > // fails here
> > Node child = folder.addNode("grade.png");
> > ```
> >
> > Error message is the following
> >
> > "No child node definition for grade.png found in node /bar"
> >
> > On Thu, Aug 26, 2021 at 02:03:27PM -0700, Eric Norman 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.kaniewsky@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
> >

-- 
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

Re: how to add a binary to a node?

Posted by Eric Norman <en...@apache.org>.
>
> "No child node definition for grade.png found in node /bar"


I see.  Well, the folder you created via webdav must be a "nt:folder" node
type?  In that case, the type of the child must extend "nt:hierarchyNode"
to conform with the "nt:folder" node type definition.

In the example I provided in my last reply,  the child node is created
using the JcrConstants.NT_FILE node type which would satisfy that condition
since "nt:file" extends the "nt:hierarchyNode" node type.

Hope that helps.

Regards,
-Eric

On Thu, Aug 26, 2021 at 2:17 PM Brian E. Lavender <br...@brie.com> wrote:

> I clarified the root node and made a new variable to hold the node for
> the bar folder.
>
> ```java
> // bar is a folder created through the webdav interface.
> Node root,folder;
> root = session.getRootNode();
> folder = root.getNode("bar");
>
> // fails here
> Node child = folder.addNode("grade.png");
> ```
>
> Error message is the following
>
> "No child node definition for grade.png found in node /bar"
>
> On Thu, Aug 26, 2021 at 02:03:27PM -0700, Eric Norman 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.kaniewsky@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
>

Re: how to add a binary to a node?

Posted by "Brian E. Lavender" <br...@brie.com>.
I clarified the root node and made a new variable to hold the node for
the bar folder.

```java
// bar is a folder created through the webdav interface.
Node root,folder;
root = session.getRootNode();
folder = root.getNode("bar");

// fails here
Node child = folder.addNode("grade.png");
```

Error message is the following

"No child node definition for grade.png found in node /bar"

On Thu, Aug 26, 2021 at 02:03:27PM -0700, Eric Norman 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.kaniewsky@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

Re: how to add a binary to a node?

Posted by Eric Norman <en...@apache.org>.
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.kaniewsky@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
>

Re: how to add a binary to a node?

Posted by "Brian E. Lavender" <br...@brie.com>.
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.kaniewsky@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

Re: how to add a binary to a node?

Posted by Jakub Kaniewski <ja...@gmail.com>.
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.kaniewsky@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


Re: how to add a binary to a node?

Posted by Eric Norman <en...@apache.org>.
Hi Brian,

You would probably want to use the ValueFactory that you can get from your
jcr session to create the binary value from an input stream.

1.
https://docs.adobe.com/content/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Session.html#getValueFactory()
2.
https://docs.adobe.com/content/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/ValueFactory.html#createBinary(java.io.InputStream)

Regards,
-Eric

On Wed, Aug 25, 2021 at 4:00 PM 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
>