You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by Michael Wechner <mi...@wyona.com> on 2005/03/08 12:11:30 UTC

How to attach the content of a File to a Node?

Hi

Does it make sense to attach the content of a file to a node as a property

InputStream in = new FileInputStream("/foo/bar.gif");
if (in != null) {
  n.setProperty("data", ...);
}

of is there any other preferable way?

Thanks

Michi

-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: How to attach the content of a File to a Node?

Posted by Michael Wechner <mi...@wyona.com>.
Stefano Mazzocchi wrote:

> I think it would be very useful if we had a list of JCR "recipes" 
> somewhere (either docs or wiki.... or even in a JCR-enabled CMS, hint 
> hint ;-)
>
> JCR is a pretty verbose API and having code snippets like those make a 
> huge difference for adoption.
>
> WDYT?


it definitely helped myself and I think it would make sense to add more 
examples
to the contrib directory, which would focuse on very specific issues.

Michi


-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: How to attach the content of a File to a Node?

Posted by Stefano Mazzocchi <st...@apache.org>.
Tobias Strasser wrote:
> here a generic example:
> 
>     public Node importFile(Node parent, File file) 
>             throws RepositoryException, IOException {
> 
>         // add some other means to extract mime type from filename
>         String mimeType = "application/octet-stream";
>         
>         Node fileNode = parent.addNode(file.getName(), "nt:file");
>         Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>         resNode.setProperty("jcr:mimeType", mimeType);
>         resNode.setProperty("jcr:encoding", "");
>         resNode.setProperty("jcr:data", new FileInputStream(file));
> 
>         Calendar lastModified = Calendar.getInstance();
>         lastModified.setTimeInMillis(file.lastModified());
>         resNode.setProperty("jcr:lastModified", lastModified);
> 
>         // this might not be usefull here
>         parent.save();
>         
>         return fileNode;
>     }

I think it would be very useful if we had a list of JCR "recipes" 
somewhere (either docs or wiki.... or even in a JCR-enabled CMS, hint 
hint ;-)

JCR is a pretty verbose API and having code snippets like those make a 
huge difference for adoption.

WDYT?

-- 
Stefano.


Re: How to attach the content of a File to a Node?

Posted by Tobias Strasser <to...@gmail.com>.
here a generic example:

    public Node importFile(Node parent, File file) 
            throws RepositoryException, IOException {

        // add some other means to extract mime type from filename
        String mimeType = "application/octet-stream";
        
        Node fileNode = parent.addNode(file.getName(), "nt:file");
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr:mimeType", mimeType);
        resNode.setProperty("jcr:encoding", "");
        resNode.setProperty("jcr:data", new FileInputStream(file));

        Calendar lastModified = Calendar.getInstance();
        lastModified.setTimeInMillis(file.lastModified());
        resNode.setProperty("jcr:lastModified", lastModified);

        // this might not be usefull here
        parent.save();
        
        return fileNode;
    }


cheers, tobi

Re: How to attach the content of a File to a Node?

Posted by Peeter Piegaze <pe...@gmail.com>.
Oh...Marcel beat to the punch! :-)


On Tue, 8 Mar 2005 12:26:42 +0100, Peeter Piegaze
<pe...@gmail.com> wrote:
> Hi Michael,
> 
> Have a look at the following sections of the spec:
> 6.7.22.6 nt:file
> 6.7.22.7 nt:linkedFile
> 6.7.22.9 nt:resource
> 
> These predefined node types are available by default in jackrabbit.
> The standard usage is to represent a file with an nt:file node, where
> the jcr:content subnode of the th nt:file node is a n nt:resource
> node. Then the actual data is stored in the jcr:data property of of
> the nt:resource node.
> 
> Of course you are free to do anything you want (define your own node
> types and use them, for example) but I would say that the above is the
> "best practice", to the extend that "best practice" exists in the JCR
> world.
> 
> Cheers,
> Peeter
> 
> On Tue, 08 Mar 2005 12:11:30 +0100, Michael Wechner
> <mi...@wyona.com> wrote:
> > Hi
> >
> > Does it make sense to attach the content of a file to a node as a property
> >
> > InputStream in = new FileInputStream("/foo/bar.gif");
> > if (in != null) {
> >   n.setProperty("data", ...);
> > }
> >
> > of is there any other preferable way?
> >
> > Thanks
> >
> > Michi
> >
> > --
> > Michael Wechner
> > Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
> > http://www.wyona.com                      http://lenya.apache.org
> > michael.wechner@wyona.com                        michi@apache.org
> >
> >
>

Re: How to attach the content of a File to a Node?

Posted by Peeter Piegaze <pe...@gmail.com>.
Hi Michael,

Have a look at the following sections of the spec:
6.7.22.6 nt:file
6.7.22.7 nt:linkedFile
6.7.22.9 nt:resource

These predefined node types are available by default in jackrabbit.
The standard usage is to represent a file with an nt:file node, where
the jcr:content subnode of the th nt:file node is a n nt:resource
node. Then the actual data is stored in the jcr:data property of of
the nt:resource node.

Of course you are free to do anything you want (define your own node
types and use them, for example) but I would say that the above is the
"best practice", to the extend that "best practice" exists in the JCR
world.

Cheers,
Peeter

On Tue, 08 Mar 2005 12:11:30 +0100, Michael Wechner
<mi...@wyona.com> wrote:
> Hi
> 
> Does it make sense to attach the content of a file to a node as a property
> 
> InputStream in = new FileInputStream("/foo/bar.gif");
> if (in != null) {
>   n.setProperty("data", ...);
> }
> 
> of is there any other preferable way?
> 
> Thanks
> 
> Michi
> 
> --
> Michael Wechner
> Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
> http://www.wyona.com                      http://lenya.apache.org
> michael.wechner@wyona.com                        michi@apache.org
> 
>

Re: How to attach the content of a File to a Node?

Posted by Michael Wechner <mi...@wyona.com>.
Hi Marcel, Peeter and Tobias

Thanks a lot for your help

Michi

Marcel Reutegger wrote:

> Hi Michi,
>
> Michael Wechner wrote:
>
>> Does it make sense to attach the content of a file to a node as a 
>> property
>>
>> InputStream in = new FileInputStream("/foo/bar.gif");
>> if (in != null) {
>>  n.setProperty("data", ...);
>> }
>
>
> I would rather use a sub node of type nt:file, which then defines a 
> jcr:content node of type nt:resource. nt:resource defines useful 
> properties such as jcr:encoding, jcr:mimeType, etc. or you could also 
> use a nt:resource node directly as a child node of your content 
> structure.
>
> regards,
>  marcel
>


-- 
Michael Wechner
Wyona Inc.  -   Open Source Content Management   -   Apache Lenya
http://www.wyona.com                      http://lenya.apache.org
michael.wechner@wyona.com                        michi@apache.org


Re: How to attach the content of a File to a Node?

Posted by Marcel Reutegger <ma...@gmx.net>.
Hi Michi,

Michael Wechner wrote:
> Does it make sense to attach the content of a file to a node as a property
> 
> InputStream in = new FileInputStream("/foo/bar.gif");
> if (in != null) {
>  n.setProperty("data", ...);
> }

I would rather use a sub node of type nt:file, which then defines a 
jcr:content node of type nt:resource. nt:resource defines useful 
properties such as jcr:encoding, jcr:mimeType, etc. or you could also 
use a nt:resource node directly as a child node of your content structure.

regards,
  marcel