saving files

View: New views
14 Messages — Rating Filter:   Alert me  

saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hey all,

more newb questions here.
Want to save a file in the repository.  found this on the wiki

Node folderNode = session.getRootNode();
        Node fileNode = folderNode.addNode(file.getName(), "nt:file");

        // create the mandatory child node - jcr:content
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr: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);

Is the file itself being put in the repository (jcr.data) or just the
content?

Do I even need the resNode?  Can I just save the file into the fileNode?

Finally, we are going to need to lock the file (which is why we are using
jcr).  Is it possible to attach info (like who owns the lock) to the locked
file?

Thanx.  This community has been quite helpful.

--
---------------------
Michael Harris

Re: saving files

by Julian Reschke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Michael Harris wrote:

> hey all,
>
> more newb questions here.
> Want to save a file in the repository.  found this on the wiki
>
> Node folderNode = session.getRootNode();
>         Node fileNode = folderNode.addNode(file.getName(), "nt:file");
>
>         // create the mandatory child node - jcr:content
>         Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>         resNode.setProperty("jcr:mimeType", "");
>         resNode.setProperty("jcr:encoding", "");
 > ...

That is very bad advice. If you don't know mimeType or encoding, do not
set it.

Where did you find that?

>         resNode.setProperty("jcr:data", new FileInputStream(file));
>         Calendar lastModified = Calendar.getInstance();
>         lastModified.setTimeInMillis(file.lastModified());
>         resNode.setProperty("jcr:lastModified", lastModified);
>
> Is the file itself being put in the repository (jcr.data) or just the
> content?

The contents.

> Do I even need the resNode?  Can I just save the file into the fileNode?

Not unless you use a different node type.

> Finally, we are going to need to lock the file (which is why we are using
> jcr).  Is it possible to attach info (like who owns the lock) to the locked
> file?

To some extent, that's part of the JCR locking model (it remembers who
owns the lock, and exposes the information both in the API and on
properties defined in mix:lockable).

> Thanx.  This community has been quite helpful.

BR, Julian

Re: saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

comments inline below.  Thank you for the response.

On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <julian.reschke@...>
wrote:

> Michael Harris wrote:
>
>> hey all,
>>
>> more newb questions here.
>> Want to save a file in the repository.  found this on the wiki
>>
>> Node folderNode = session.getRootNode();
>>        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
>>
>>        // create the mandatory child node - jcr:content
>>        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>>        resNode.setProperty("jcr:mimeType", "");
>>        resNode.setProperty("jcr:encoding", "");
>>
> > ...
>
> That is very bad advice. If you don't know mimeType or encoding, do not set
> it.


> Where did you find that?
>

I got the code from the wiki.  It had the mimetype and the encoding type.  I
modified the code and just put empty strings in for now.



>
>         resNode.setProperty("jcr:data", new FileInputStream(file));
>>        Calendar lastModified = Calendar.getInstance();
>>        lastModified.setTimeInMillis(file.lastModified());
>>        resNode.setProperty("jcr:lastModified", lastModified);
>>
>> Is the file itself being put in the repository (jcr.data) or just the
>> content?
>>
>
> The contents.
>
>  Do I even need the resNode?  Can I just save the file into the fileNode?
>>
>
> Not unless you use a different node type.
>

So does it mean anything to say "Can I save the file itself?"


so the above code I put into a DAO method saveFile(File, Session).  In the
test for that method, i have

        Node root = session.getRootNode();
        // testFile is the file in the Dao
        Node fileNode = root.getNode(testFile.getName());
        Node fileDetails = fileNode.getNode("jcr:content");

        Property fileProp = fileDetails.getProperty("jcr:data");

I am trying to get at the file contents, which is just a string "some
content".

on the property retrieved I dont see the file content (Im in eclipse's
debugger).  How do I retrieve the file.  What would a
loadFile(Session, filename) method look like?






>  Finally, we are going to need to lock the file (which is why we are using
>> jcr).  Is it possible to attach info (like who owns the lock) to the
>> locked
>> file?
>>
>
> To some extent, that's part of the JCR locking model (it remembers who owns
> the lock, and exposes the information both in the API and on properties
> defined in mix:lockable).
>
>  Thanx.  This community has been quite helpful.
>>
>
> BR, Julian
>



--
---------------------
Michael Harris

Re: saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ladies and gentlemen

its still not obvious to me how to retrieve the saved file.  Can someone
please illuminate the situation?

thanx in advance.

On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <michael.e.harris@...>
wrote:

> comments inline below.  Thank you for the response.
>
> On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <julian.reschke@...>
> wrote:
>
>> Michael Harris wrote:
>>
>>> hey all,
>>>
>>> more newb questions here.
>>> Want to save a file in the repository.  found this on the wiki
>>>
>>> Node folderNode = session.getRootNode();
>>>        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
>>>
>>>        // create the mandatory child node - jcr:content
>>>        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>>>        resNode.setProperty("jcr:mimeType", "");
>>>        resNode.setProperty("jcr:encoding", "");
>>>
>> > ...
>>
>> That is very bad advice. If you don't know mimeType or encoding, do not
>> set it.
>
>
>> Where did you find that?
>>
>
> I got the code from the wiki.  It had the mimetype and the encoding type.
> I modified the code and just put empty strings in for now.
>
>
>
>>
>>         resNode.setProperty("jcr:data", new FileInputStream(file));
>>>        Calendar lastModified = Calendar.getInstance();
>>>        lastModified.setTimeInMillis(file.lastModified());
>>>        resNode.setProperty("jcr:lastModified", lastModified);
>>>
>>> Is the file itself being put in the repository (jcr.data) or just the
>>> content?
>>>
>>
>> The contents.
>>
>>  Do I even need the resNode?  Can I just save the file into the fileNode?
>>>
>>
>> Not unless you use a different node type.
>>
>
> So does it mean anything to say "Can I save the file itself?"
>
>
> so the above code I put into a DAO method saveFile(File, Session).  In the
> test for that method, i have
>
>         Node root = session.getRootNode();
>         // testFile is the file in the Dao
>         Node fileNode = root.getNode(testFile.getName());
>         Node fileDetails = fileNode.getNode("jcr:content");
>
>         Property fileProp = fileDetails.getProperty("jcr:data");
>
> I am trying to get at the file contents, which is just a string "some
> content".
>
> on the property retrieved I dont see the file content (Im in eclipse's
> debugger).  How do I retrieve the file.  What would a
> loadFile(Session, filename) method look like?
>
>
>
>
>
>
>>  Finally, we are going to need to lock the file (which is why we are using
>>> jcr).  Is it possible to attach info (like who owns the lock) to the
>>> locked
>>> file?
>>>
>>
>> To some extent, that's part of the JCR locking model (it remembers who
>> owns the lock, and exposes the information both in the API and on properties
>> defined in mix:lockable).
>>
>>  Thanx.  This community has been quite helpful.
>>>
>>
>> BR, Julian
>>
>
>
>
> --
> ---------------------
> Michael Harris
>



--
---------------------
Michael Harris

Re: saving files

by Nick Stuart-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All you can really do is retrieve the InputStream for said file. Which
should be enough, and the nodes hold the rest of the info if you need it
obviously (file name, type, etc).

Something like:

Node resNode = fileNode.getNode("jcr:content");
resNode.getProperty("jcr:data").getStream();

Where fileNode is the jcr:file node type. You wont be able to actually get a
File object becuase thats part of JCR, that it extracts that stuff away from
you, and you just deal with the contents and info.

-Nick

On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <michael.e.harris@...>
wrote:

> ladies and gentlemen
>
> its still not obvious to me how to retrieve the saved file.  Can someone
> please illuminate the situation?
>
> thanx in advance.
>
> On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <
> michael.e.harris@...>
> wrote:
>
> > comments inline below.  Thank you for the response.
> >
> > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <julian.reschke@...>
> > wrote:
> >
> >> Michael Harris wrote:
> >>
> >>> hey all,
> >>>
> >>> more newb questions here.
> >>> Want to save a file in the repository.  found this on the wiki
> >>>
> >>> Node folderNode = session.getRootNode();
> >>>        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
> >>>
> >>>        // create the mandatory child node - jcr:content
> >>>        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
> >>>        resNode.setProperty("jcr:mimeType", "");
> >>>        resNode.setProperty("jcr:encoding", "");
> >>>
> >> > ...
> >>
> >> That is very bad advice. If you don't know mimeType or encoding, do not
> >> set it.
> >
> >
> >> Where did you find that?
> >>
> >
> > I got the code from the wiki.  It had the mimetype and the encoding type.
> > I modified the code and just put empty strings in for now.
> >
> >
> >
> >>
> >>         resNode.setProperty("jcr:data", new FileInputStream(file));
> >>>        Calendar lastModified = Calendar.getInstance();
> >>>        lastModified.setTimeInMillis(file.lastModified());
> >>>        resNode.setProperty("jcr:lastModified", lastModified);
> >>>
> >>> Is the file itself being put in the repository (jcr.data) or just the
> >>> content?
> >>>
> >>
> >> The contents.
> >>
> >>  Do I even need the resNode?  Can I just save the file into the
> fileNode?
> >>>
> >>
> >> Not unless you use a different node type.
> >>
> >
> > So does it mean anything to say "Can I save the file itself?"
> >
> >
> > so the above code I put into a DAO method saveFile(File, Session).  In
> the
> > test for that method, i have
> >
> >         Node root = session.getRootNode();
> >         // testFile is the file in the Dao
> >         Node fileNode = root.getNode(testFile.getName());
> >         Node fileDetails = fileNode.getNode("jcr:content");
> >
> >         Property fileProp = fileDetails.getProperty("jcr:data");
> >
> > I am trying to get at the file contents, which is just a string "some
> > content".
> >
> > on the property retrieved I dont see the file content (Im in eclipse's
> > debugger).  How do I retrieve the file.  What would a
> > loadFile(Session, filename) method look like?
> >
> >
> >
> >
> >
> >
> >>  Finally, we are going to need to lock the file (which is why we are
> using
> >>> jcr).  Is it possible to attach info (like who owns the lock) to the
> >>> locked
> >>> file?
> >>>
> >>
> >> To some extent, that's part of the JCR locking model (it remembers who
> >> owns the lock, and exposes the information both in the API and on
> properties
> >> defined in mix:lockable).
> >>
> >>  Thanx.  This community has been quite helpful.
> >>>
> >>
> >> BR, Julian
> >>
> >
> >
> >
> > --
> > ---------------------
> > Michael Harris
> >
>
>
>
> --
> ---------------------
> Michael Harris
>

Re: saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nick

thanx for the reply.  when I do that and inspect the returned stream in the
eclipse debugger there is nothing in it.

michael.



On Wed, Jul 23, 2008 at 10:22 AM, Nick Stuart <nstuart@...>
wrote:

> All you can really do is retrieve the InputStream for said file. Which
> should be enough, and the nodes hold the rest of the info if you need it
> obviously (file name, type, etc).
>
> Something like:
>
> Node resNode = fileNode.getNode("jcr:content");
> resNode.getProperty("jcr:data").getStream();
>
> Where fileNode is the jcr:file node type. You wont be able to actually get
> a
> File object becuase thats part of JCR, that it extracts that stuff away
> from
> you, and you just deal with the contents and info.
>
> -Nick
>
> On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <
> michael.e.harris@...>
> wrote:
>
> > ladies and gentlemen
> >
> > its still not obvious to me how to retrieve the saved file.  Can someone
> > please illuminate the situation?
> >
> > thanx in advance.
> >
> > On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <
> > michael.e.harris@...>
> > wrote:
> >
> > > comments inline below.  Thank you for the response.
> > >
> > > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <julian.reschke@...
> >
> > > wrote:
> > >
> > >> Michael Harris wrote:
> > >>
> > >>> hey all,
> > >>>
> > >>> more newb questions here.
> > >>> Want to save a file in the repository.  found this on the wiki
> > >>>
> > >>> Node folderNode = session.getRootNode();
> > >>>        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
> > >>>
> > >>>        // create the mandatory child node - jcr:content
> > >>>        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
> > >>>        resNode.setProperty("jcr:mimeType", "");
> > >>>        resNode.setProperty("jcr:encoding", "");
> > >>>
> > >> > ...
> > >>
> > >> That is very bad advice. If you don't know mimeType or encoding, do
> not
> > >> set it.
> > >
> > >
> > >> Where did you find that?
> > >>
> > >
> > > I got the code from the wiki.  It had the mimetype and the encoding
> type.
> > > I modified the code and just put empty strings in for now.
> > >
> > >
> > >
> > >>
> > >>         resNode.setProperty("jcr:data", new FileInputStream(file));
> > >>>        Calendar lastModified = Calendar.getInstance();
> > >>>        lastModified.setTimeInMillis(file.lastModified());
> > >>>        resNode.setProperty("jcr:lastModified", lastModified);
> > >>>
> > >>> Is the file itself being put in the repository (jcr.data) or just the
> > >>> content?
> > >>>
> > >>
> > >> The contents.
> > >>
> > >>  Do I even need the resNode?  Can I just save the file into the
> > fileNode?
> > >>>
> > >>
> > >> Not unless you use a different node type.
> > >>
> > >
> > > So does it mean anything to say "Can I save the file itself?"
> > >
> > >
> > > so the above code I put into a DAO method saveFile(File, Session).  In
> > the
> > > test for that method, i have
> > >
> > >         Node root = session.getRootNode();
> > >         // testFile is the file in the Dao
> > >         Node fileNode = root.getNode(testFile.getName());
> > >         Node fileDetails = fileNode.getNode("jcr:content");
> > >
> > >         Property fileProp = fileDetails.getProperty("jcr:data");
> > >
> > > I am trying to get at the file contents, which is just a string "some
> > > content".
> > >
> > > on the property retrieved I dont see the file content (Im in eclipse's
> > > debugger).  How do I retrieve the file.  What would a
> > > loadFile(Session, filename) method look like?
> > >
> > >
> > >
> > >
> > >
> > >
> > >>  Finally, we are going to need to lock the file (which is why we are
> > using
> > >>> jcr).  Is it possible to attach info (like who owns the lock) to the
> > >>> locked
> > >>> file?
> > >>>
> > >>
> > >> To some extent, that's part of the JCR locking model (it remembers who
> > >> owns the lock, and exposes the information both in the API and on
> > properties
> > >> defined in mix:lockable).
> > >>
> > >>  Thanx.  This community has been quite helpful.
> > >>>
> > >>
> > >> BR, Julian
> > >>
> > >
> > >
> > >
> > > --
> > > ---------------------
> > > Michael Harris
> > >
> >
> >
> >
> > --
> > ---------------------
> > Michael Harris
> >
>



--
---------------------
Michael Harris

Re: saving files

by Nick Stuart-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I know that works as I've used it extensively. I am on a 1.5 build, but it
was working with the 1.4.x build I started with. Have you looked at the
stream going in to make sure something is actually being saved? You
obviously have the node and property there or otherwise you would be getting
a RepositoryException.

I would check to make sure you are saving everything you think you are.
Also, try just using the stream and ignore the debugger (it might be lying
to you, who knows). But it looks like everything is in order.

On Wed, Jul 23, 2008 at 10:35 AM, Michael Harris <michael.e.harris@...>
wrote:

> Nick
>
> thanx for the reply.  when I do that and inspect the returned stream in the
> eclipse debugger there is nothing in it.
>
> michael.
>
>
>
> On Wed, Jul 23, 2008 at 10:22 AM, Nick Stuart <nstuart@...
> >
> wrote:
>
> > All you can really do is retrieve the InputStream for said file. Which
> > should be enough, and the nodes hold the rest of the info if you need it
> > obviously (file name, type, etc).
> >
> > Something like:
> >
> > Node resNode = fileNode.getNode("jcr:content");
> > resNode.getProperty("jcr:data").getStream();
> >
> > Where fileNode is the jcr:file node type. You wont be able to actually
> get
> > a
> > File object becuase thats part of JCR, that it extracts that stuff away
> > from
> > you, and you just deal with the contents and info.
> >
> > -Nick
> >
> > On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <
> > michael.e.harris@...>
> > wrote:
> >
> > > ladies and gentlemen
> > >
> > > its still not obvious to me how to retrieve the saved file.  Can
> someone
> > > please illuminate the situation?
> > >
> > > thanx in advance.
> > >
> > > On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <
> > > michael.e.harris@...>
> > > wrote:
> > >
> > > > comments inline below.  Thank you for the response.
> > > >
> > > > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <
> julian.reschke@...
> > >
> > > > wrote:
> > > >
> > > >> Michael Harris wrote:
> > > >>
> > > >>> hey all,
> > > >>>
> > > >>> more newb questions here.
> > > >>> Want to save a file in the repository.  found this on the wiki
> > > >>>
> > > >>> Node folderNode = session.getRootNode();
> > > >>>        Node fileNode = folderNode.addNode(file.getName(),
> "nt:file");
> > > >>>
> > > >>>        // create the mandatory child node - jcr:content
> > > >>>        Node resNode = fileNode.addNode("jcr:content",
> "nt:resource");
> > > >>>        resNode.setProperty("jcr:mimeType", "");
> > > >>>        resNode.setProperty("jcr:encoding", "");
> > > >>>
> > > >> > ...
> > > >>
> > > >> That is very bad advice. If you don't know mimeType or encoding, do
> > not
> > > >> set it.
> > > >
> > > >
> > > >> Where did you find that?
> > > >>
> > > >
> > > > I got the code from the wiki.  It had the mimetype and the encoding
> > type.
> > > > I modified the code and just put empty strings in for now.
> > > >
> > > >
> > > >
> > > >>
> > > >>         resNode.setProperty("jcr:data", new FileInputStream(file));
> > > >>>        Calendar lastModified = Calendar.getInstance();
> > > >>>        lastModified.setTimeInMillis(file.lastModified());
> > > >>>        resNode.setProperty("jcr:lastModified", lastModified);
> > > >>>
> > > >>> Is the file itself being put in the repository (jcr.data) or just
> the
> > > >>> content?
> > > >>>
> > > >>
> > > >> The contents.
> > > >>
> > > >>  Do I even need the resNode?  Can I just save the file into the
> > > fileNode?
> > > >>>
> > > >>
> > > >> Not unless you use a different node type.
> > > >>
> > > >
> > > > So does it mean anything to say "Can I save the file itself?"
> > > >
> > > >
> > > > so the above code I put into a DAO method saveFile(File, Session).
>  In
> > > the
> > > > test for that method, i have
> > > >
> > > >         Node root = session.getRootNode();
> > > >         // testFile is the file in the Dao
> > > >         Node fileNode = root.getNode(testFile.getName());
> > > >         Node fileDetails = fileNode.getNode("jcr:content");
> > > >
> > > >         Property fileProp = fileDetails.getProperty("jcr:data");
> > > >
> > > > I am trying to get at the file contents, which is just a string "some
> > > > content".
> > > >
> > > > on the property retrieved I dont see the file content (Im in
> eclipse's
> > > > debugger).  How do I retrieve the file.  What would a
> > > > loadFile(Session, filename) method look like?
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >>  Finally, we are going to need to lock the file (which is why we are
> > > using
> > > >>> jcr).  Is it possible to attach info (like who owns the lock) to
> the
> > > >>> locked
> > > >>> file?
> > > >>>
> > > >>
> > > >> To some extent, that's part of the JCR locking model (it remembers
> who
> > > >> owns the lock, and exposes the information both in the API and on
> > > properties
> > > >> defined in mix:lockable).
> > > >>
> > > >>  Thanx.  This community has been quite helpful.
> > > >>>
> > > >>
> > > >> BR, Julian
> > > >>
> > > >
> > > >
> > > >
> > > > --
> > > > ---------------------
> > > > Michael Harris
> > > >
> > >
> > >
> > >
> > > --
> > > ---------------------
> > > Michael Harris
> > >
> >
>
>
>
> --
> ---------------------
> Michael Harris
>

Re: saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yeah man I tried writing the retrieved stream to a file, it is empty.  The
file that I save definitely has content (confirmed by less).

Perhaps you can take a look see (the code isn't complicated); having more
experience than me you could identify obvious problems pretty quickly.

here is the save code

    public void saveFile(File file, Session session) throws Exception {
        // create the file node - see section 6.7.22.6 of the spec

        Node folderNode = session.getRootNode();
        Node fileNode = folderNode.addNode(file.getName(), "nt:file");

        // create the mandatory child node - jcr:content
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr:mimeType", "");
        resNode.setProperty("jcr:encoding", "");
        FileInputStream fileInputStream = new FileInputStream(file);
        resNode.setProperty("jcr:data", fileInputStream);
        Calendar lastModified = Calendar.getInstance();
        lastModified.setTimeInMillis(file.lastModified());
        resNode.setProperty("jcr:lastModified", lastModified);

        session.save();
    }

and here is the test code

    protected void setUp() throws Exception {
        super.setUp();
        String testFileName = "/tmp/" + getName() + ".txt";
        FileWriter fw = new FileWriter(testFileName);
        fw.write("some content");
        fw.flush();
        testFile = new File(testFileName);
    }


    public void testSaveFile() throws Exception {
        Session session = getSession();
        dao.saveFile(testFile, session);

        Node root = session.getRootNode();
        Node fileNode = root.getNode(testFile.getName());
        Node fileDetails = fileNode.getNode("jcr:content");

        Property fileProp = fileDetails.getProperty("jcr:data");
        InputStream retrievedStream = fileProp.getStream();

        FileOutputStream fos = new
FileOutputStream("/tmp/retrievedFile.txt");
        byte[] buf = new byte[256];
        int read = 0;
        while ((read = retrievedStream.read(buf)) > 0) {
            fos.write(buf, 0, read);
            fos.flush();
        }


    }

On Wed, Jul 23, 2008 at 10:38 AM, Nick Stuart <nstuart@...>
wrote:

> I know that works as I've used it extensively. I am on a 1.5 build, but it
> was working with the 1.4.x build I started with. Have you looked at the
> stream going in to make sure something is actually being saved? You
> obviously have the node and property there or otherwise you would be
> getting
> a RepositoryException.
>
> I would check to make sure you are saving everything you think you are.
> Also, try just using the stream and ignore the debugger (it might be lying
> to you, who knows). But it looks like everything is in order.
>
> On Wed, Jul 23, 2008 at 10:35 AM, Michael Harris <
> michael.e.harris@...>
> wrote:
>
> > Nick
> >
> > thanx for the reply.  when I do that and inspect the returned stream in
> the
> > eclipse debugger there is nothing in it.
> >
> > michael.
> >
> >
> >
> > On Wed, Jul 23, 2008 at 10:22 AM, Nick Stuart <
> nstuart@...
> > >
> > wrote:
> >
> > > All you can really do is retrieve the InputStream for said file. Which
> > > should be enough, and the nodes hold the rest of the info if you need
> it
> > > obviously (file name, type, etc).
> > >
> > > Something like:
> > >
> > > Node resNode = fileNode.getNode("jcr:content");
> > > resNode.getProperty("jcr:data").getStream();
> > >
> > > Where fileNode is the jcr:file node type. You wont be able to actually
> > get
> > > a
> > > File object becuase thats part of JCR, that it extracts that stuff away
> > > from
> > > you, and you just deal with the contents and info.
> > >
> > > -Nick
> > >
> > > On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <
> > > michael.e.harris@...>
> > > wrote:
> > >
> > > > ladies and gentlemen
> > > >
> > > > its still not obvious to me how to retrieve the saved file.  Can
> > someone
> > > > please illuminate the situation?
> > > >
> > > > thanx in advance.
> > > >
> > > > On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <
> > > > michael.e.harris@...>
> > > > wrote:
> > > >
> > > > > comments inline below.  Thank you for the response.
> > > > >
> > > > > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <
> > julian.reschke@...
> > > >
> > > > > wrote:
> > > > >
> > > > >> Michael Harris wrote:
> > > > >>
> > > > >>> hey all,
> > > > >>>
> > > > >>> more newb questions here.
> > > > >>> Want to save a file in the repository.  found this on the wiki
> > > > >>>
> > > > >>> Node folderNode = session.getRootNode();
> > > > >>>        Node fileNode = folderNode.addNode(file.getName(),
> > "nt:file");
> > > > >>>
> > > > >>>        // create the mandatory child node - jcr:content
> > > > >>>        Node resNode = fileNode.addNode("jcr:content",
> > "nt:resource");
> > > > >>>        resNode.setProperty("jcr:mimeType", "");
> > > > >>>        resNode.setProperty("jcr:encoding", "");
> > > > >>>
> > > > >> > ...
> > > > >>
> > > > >> That is very bad advice. If you don't know mimeType or encoding,
> do
> > > not
> > > > >> set it.
> > > > >
> > > > >
> > > > >> Where did you find that?
> > > > >>
> > > > >
> > > > > I got the code from the wiki.  It had the mimetype and the encoding
> > > type.
> > > > > I modified the code and just put empty strings in for now.
> > > > >
> > > > >
> > > > >
> > > > >>
> > > > >>         resNode.setProperty("jcr:data", new
> FileInputStream(file));
> > > > >>>        Calendar lastModified = Calendar.getInstance();
> > > > >>>        lastModified.setTimeInMillis(file.lastModified());
> > > > >>>        resNode.setProperty("jcr:lastModified", lastModified);
> > > > >>>
> > > > >>> Is the file itself being put in the repository (jcr.data) or just
> > the
> > > > >>> content?
> > > > >>>
> > > > >>
> > > > >> The contents.
> > > > >>
> > > > >>  Do I even need the resNode?  Can I just save the file into the
> > > > fileNode?
> > > > >>>
> > > > >>
> > > > >> Not unless you use a different node type.
> > > > >>
> > > > >
> > > > > So does it mean anything to say "Can I save the file itself?"
> > > > >
> > > > >
> > > > > so the above code I put into a DAO method saveFile(File, Session).
> >  In
> > > > the
> > > > > test for that method, i have
> > > > >
> > > > >         Node root = session.getRootNode();
> > > > >         // testFile is the file in the Dao
> > > > >         Node fileNode = root.getNode(testFile.getName());
> > > > >         Node fileDetails = fileNode.getNode("jcr:content");
> > > > >
> > > > >         Property fileProp = fileDetails.getProperty("jcr:data");
> > > > >
> > > > > I am trying to get at the file contents, which is just a string
> "some
> > > > > content".
> > > > >
> > > > > on the property retrieved I dont see the file content (Im in
> > eclipse's
> > > > > debugger).  How do I retrieve the file.  What would a
> > > > > loadFile(Session, filename) method look like?
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >>  Finally, we are going to need to lock the file (which is why we
> are
> > > > using
> > > > >>> jcr).  Is it possible to attach info (like who owns the lock) to
> > the
> > > > >>> locked
> > > > >>> file?
> > > > >>>
> > > > >>
> > > > >> To some extent, that's part of the JCR locking model (it remembers
> > who
> > > > >> owns the lock, and exposes the information both in the API and on
> > > > properties
> > > > >> defined in mix:lockable).
> > > > >>
> > > > >>  Thanx.  This community has been quite helpful.
> > > > >>>
> > > > >>
> > > > >> BR, Julian
> > > > >>
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > ---------------------
> > > > > Michael Harris
> > > > >
> > > >
> > > >
> > > >
> > > > --
> > > > ---------------------
> > > > Michael Harris
> > > >
> > >
> >
> >
> >
> > --
> > ---------------------
> > Michael Harris
> >
>



--
---------------------
Michael Harris

Re: saving files

by Nick Stuart-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Well the only thats really different between that and my code is setting
mimeType and encoding to empty. I don't touch the encoding at all, and
always set the mimeType to something even if it is just text/plain. Other
then that it looks about the same. Try hardcoding at least mimeType to
'text/plain' and remove the encoding bit and see if you get better results.
That may be screwing up the retrieval bit, not sure why it would though.

-Nick

On Wed, Jul 23, 2008 at 10:56 AM, Michael Harris <michael.e.harris@...>
wrote:

> yeah man I tried writing the retrieved stream to a file, it is empty.  The
> file that I save definitely has content (confirmed by less).
>
> Perhaps you can take a look see (the code isn't complicated); having more
> experience than me you could identify obvious problems pretty quickly.
>
> here is the save code
>
>    public void saveFile(File file, Session session) throws Exception {
>        // create the file node - see section 6.7.22.6 of the spec
>
>        Node folderNode = session.getRootNode();
>        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
>
>        // create the mandatory child node - jcr:content
>        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>        resNode.setProperty("jcr:mimeType", "");
>        resNode.setProperty("jcr:encoding", "");
>         FileInputStream fileInputStream = new FileInputStream(file);
>        resNode.setProperty("jcr:data", fileInputStream);
>         Calendar lastModified = Calendar.getInstance();
>        lastModified.setTimeInMillis(file.lastModified());
>        resNode.setProperty("jcr:lastModified", lastModified);
>
>         session.save();
>    }
>
> and here is the test code
>
>    protected void setUp() throws Exception {
>        super.setUp();
>        String testFileName = "/tmp/" + getName() + ".txt";
>        FileWriter fw = new FileWriter(testFileName);
>        fw.write("some content");
>        fw.flush();
>        testFile = new File(testFileName);
>    }
>
>
>    public void testSaveFile() throws Exception {
>        Session session = getSession();
>        dao.saveFile(testFile, session);
>
>        Node root = session.getRootNode();
>         Node fileNode = root.getNode(testFile.getName());
>        Node fileDetails = fileNode.getNode("jcr:content");
>
>        Property fileProp = fileDetails.getProperty("jcr:data");
>         InputStream retrievedStream = fileProp.getStream();
>
>        FileOutputStream fos = new
> FileOutputStream("/tmp/retrievedFile.txt");
>        byte[] buf = new byte[256];
>        int read = 0;
>        while ((read = retrievedStream.read(buf)) > 0) {
>            fos.write(buf, 0, read);
>            fos.flush();
>        }
>
>
>    }
>
> On Wed, Jul 23, 2008 at 10:38 AM, Nick Stuart <nstuart@...
> >
> wrote:
>
> > I know that works as I've used it extensively. I am on a 1.5 build, but
> it
> > was working with the 1.4.x build I started with. Have you looked at the
> > stream going in to make sure something is actually being saved? You
> > obviously have the node and property there or otherwise you would be
> > getting
> > a RepositoryException.
> >
> > I would check to make sure you are saving everything you think you are.
> > Also, try just using the stream and ignore the debugger (it might be
> lying
> > to you, who knows). But it looks like everything is in order.
> >
> > On Wed, Jul 23, 2008 at 10:35 AM, Michael Harris <
> > michael.e.harris@...>
> > wrote:
> >
> > > Nick
> > >
> > > thanx for the reply.  when I do that and inspect the returned stream in
> > the
> > > eclipse debugger there is nothing in it.
> > >
> > > michael.
> > >
> > >
> > >
> > > On Wed, Jul 23, 2008 at 10:22 AM, Nick Stuart <
> > nstuart@...
> > > >
> > > wrote:
> > >
> > > > All you can really do is retrieve the InputStream for said file.
> Which
> > > > should be enough, and the nodes hold the rest of the info if you need
> > it
> > > > obviously (file name, type, etc).
> > > >
> > > > Something like:
> > > >
> > > > Node resNode = fileNode.getNode("jcr:content");
> > > > resNode.getProperty("jcr:data").getStream();
> > > >
> > > > Where fileNode is the jcr:file node type. You wont be able to
> actually
> > > get
> > > > a
> > > > File object becuase thats part of JCR, that it extracts that stuff
> away
> > > > from
> > > > you, and you just deal with the contents and info.
> > > >
> > > > -Nick
> > > >
> > > > On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <
> > > > michael.e.harris@...>
> > > > wrote:
> > > >
> > > > > ladies and gentlemen
> > > > >
> > > > > its still not obvious to me how to retrieve the saved file.  Can
> > > someone
> > > > > please illuminate the situation?
> > > > >
> > > > > thanx in advance.
> > > > >
> > > > > On Tue, Jul 22, 2008 at 3:05 PM, Michael Harris <
> > > > > michael.e.harris@...>
> > > > > wrote:
> > > > >
> > > > > > comments inline below.  Thank you for the response.
> > > > > >
> > > > > > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke <
> > > julian.reschke@...
> > > > >
> > > > > > wrote:
> > > > > >
> > > > > >> Michael Harris wrote:
> > > > > >>
> > > > > >>> hey all,
> > > > > >>>
> > > > > >>> more newb questions here.
> > > > > >>> Want to save a file in the repository.  found this on the wiki
> > > > > >>>
> > > > > >>> Node folderNode = session.getRootNode();
> > > > > >>>        Node fileNode = folderNode.addNode(file.getName(),
> > > "nt:file");
> > > > > >>>
> > > > > >>>        // create the mandatory child node - jcr:content
> > > > > >>>        Node resNode = fileNode.addNode("jcr:content",
> > > "nt:resource");
> > > > > >>>        resNode.setProperty("jcr:mimeType", "");
> > > > > >>>        resNode.setProperty("jcr:encoding", "");
> > > > > >>>
> > > > > >> > ...
> > > > > >>
> > > > > >> That is very bad advice. If you don't know mimeType or encoding,
> > do
> > > > not
> > > > > >> set it.
> > > > > >
> > > > > >
> > > > > >> Where did you find that?
> > > > > >>
> > > > > >
> > > > > > I got the code from the wiki.  It had the mimetype and the
> encoding
> > > > type.
> > > > > > I modified the code and just put empty strings in for now.
> > > > > >
> > > > > >
> > > > > >
> > > > > >>
> > > > > >>         resNode.setProperty("jcr:data", new
> > FileInputStream(file));
> > > > > >>>        Calendar lastModified = Calendar.getInstance();
> > > > > >>>        lastModified.setTimeInMillis(file.lastModified());
> > > > > >>>        resNode.setProperty("jcr:lastModified", lastModified);
> > > > > >>>
> > > > > >>> Is the file itself being put in the repository (jcr.data) or
> just
> > > the
> > > > > >>> content?
> > > > > >>>
> > > > > >>
> > > > > >> The contents.
> > > > > >>
> > > > > >>  Do I even need the resNode?  Can I just save the file into the
> > > > > fileNode?
> > > > > >>>
> > > > > >>
> > > > > >> Not unless you use a different node type.
> > > > > >>
> > > > > >
> > > > > > So does it mean anything to say "Can I save the file itself?"
> > > > > >
> > > > > >
> > > > > > so the above code I put into a DAO method saveFile(File,
> Session).
> > >  In
> > > > > the
> > > > > > test for that method, i have
> > > > > >
> > > > > >         Node root = session.getRootNode();
> > > > > >         // testFile is the file in the Dao
> > > > > >         Node fileNode = root.getNode(testFile.getName());
> > > > > >         Node fileDetails = fileNode.getNode("jcr:content");
> > > > > >
> > > > > >         Property fileProp = fileDetails.getProperty("jcr:data");
> > > > > >
> > > > > > I am trying to get at the file contents, which is just a string
> > "some
> > > > > > content".
> > > > > >
> > > > > > on the property retrieved I dont see the file content (Im in
> > > eclipse's
> > > > > > debugger).  How do I retrieve the file.  What would a
> > > > > > loadFile(Session, filename) method look like?
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > >>  Finally, we are going to need to lock the file (which is why we
> > are
> > > > > using
> > > > > >>> jcr).  Is it possible to attach info (like who owns the lock)
> to
> > > the
> > > > > >>> locked
> > > > > >>> file?
> > > > > >>>
> > > > > >>
> > > > > >> To some extent, that's part of the JCR locking model (it
> remembers
> > > who
> > > > > >> owns the lock, and exposes the information both in the API and
> on
> > > > > properties
> > > > > >> defined in mix:lockable).
> > > > > >>
> > > > > >>  Thanx.  This community has been quite helpful.
> > > > > >>>
> > > > > >>
> > > > > >> BR, Julian
> > > > > >>
> > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > > ---------------------
> > > > > > Michael Harris
> > > > > >
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > ---------------------
> > > > > Michael Harris
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > ---------------------
> > > Michael Harris
> > >
> >
>
>
>
> --
> ---------------------
> Michael Harris
>

Re: saving files

by shrndegruv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

yeah tried that and no dice.  wierd eh.  I confirmed that the testFile has
content in it....

On Wed, Jul 23, 2008 at 11:01 AM, Nick Stuart <nstuart@...>
wrote:

> Well the only thats really different between that and my code is setting
> mimeType and encoding to empty. I don't touch the encoding at all, and
> always set the mimeType to something even if it is just text/plain. Other
> then that it looks about the same. Try hardcoding at least mimeType to
> 'text/plain' and remove the encoding bit and see if you get better results.
> That may be screwing up the retrieval bit, not sure why it would though.
>
> -Nick
>
> On Wed, Jul 23, 2008 at 10:56 AM, Michael Harris <
> michael.e.harris@...>
> wrote:
>
> > yeah man I tried writing the retrieved stream to a file, it is empty.
>  The
> > file that I save definitely has content (confirmed by less).
> >
> > Perhaps you can take a look see (the code isn't complicated); having more
> > experience than me you could identify obvious problems pretty quickly.
> >
> > here is the save code
> >
> >    public void saveFile(File file, Session session) throws Exception {
> >        // create the file node - see section 6.7.22.6 of the spec
> >
> >        Node folderNode = session.getRootNode();
> >        Node fileNode = folderNode.addNode(file.getName(), "nt:file");
> >
> >        // create the mandatory child node - jcr:content
> >        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
> >        resNode.setProperty("jcr:mimeType", "");
> >        resNode.setProperty("jcr:encoding", "");
> >         FileInputStream fileInputStream = new FileInputStream(file);
> >        resNode.setProperty("jcr:data", fileInputStream);
> >         Calendar lastModified = Calendar.getInstance();
> >        lastModified.setTimeInMillis(file.lastModified());
> >        resNode.setProperty("jcr:lastModified", lastModified);
> >
> >         session.save();
> >    }
> >
> > and here is the test code
> >
> >    protected void setUp() throws Exception {
> >        super.setUp();
> >        String testFileName = "/tmp/" + getName() + ".txt";
> >        FileWriter fw = new FileWriter(testFileName);
> >        fw.write("some content");
> >        fw.flush();
> >        testFile = new File(testFileName);
> >    }
> >
> >
> >    public void testSaveFile() throws Exception {
> >        Session session = getSession();
> >        dao.saveFile(testFile, session);
> >
> >        Node root = session.getRootNode();
> >         Node fileNode = root.getNode(testFile.getName());
> >        Node fileDetails = fileNode.getNode("jcr:content");
> >
> >        Property fileProp = fileDetails.getProperty("jcr:data");
> >         InputStream retrievedStream = fileProp.getStream();
> >
> >        FileOutputStream fos = new
> > FileOutputStream("/tmp/retrievedFile.txt");
> >        byte[] buf = new byte[256];
> >        int read = 0;
> >        while ((read = retrievedStream.read(buf)) > 0) {
> >            fos.write(buf, 0, read);
> >            fos.flush();
> >        }
> >
> >
> >    }
> >
> > On Wed, Jul 23, 2008 at 10:38 AM, Nick Stuart <
> nstuart@...
> > >
> > wrote:
> >
> > > I know that works as I've used it extensively. I am on a 1.5 build, but
> > it
> > > was working with the 1.4.x build I started with. Have you looked at the
> > > stream going in to make sure something is actually being saved? You
> > > obviously have the node and property there or otherwise you would be
> > > getting
> > > a RepositoryException.
> > >
> > > I would check to make sure you are saving everything you think you are.
> > > Also, try just using the stream and ignore the debugger (it might be
> > lying
> > > to you, who knows). But it looks like everything is in order.
> > >
> > > On Wed, Jul 23, 2008 at 10:35 AM, Michael Harris <
> > > michael.e.harris@...>
> > > wrote:
> > >
> > > > Nick
> > > >
> > > > thanx for the reply.  when I do that and inspect the returned stream
> in
> > > the
> > > > eclipse debugger there is nothing in it.
> > > >
> > > > michael.
> > > >
> > > >
> > > >
> > > > On Wed, Jul 23, 2008 at 10:22 AM, Nick Stuart <
> > > nstuart@...
> > > > >
> > > > wrote:
> > > >
> > > > > All you can really do is retrieve the InputStream for said file.
> > Which
> > > > > should be enough, and the nodes hold the rest of the info if you
> need
> > > it
> > > > > obviously (file name, type, etc).
> > > > >
> > > > > Something like:
> > > > >
> > > > > Node resNode = fileNode.getNode("jcr:content");
> > > > > resNode.getProperty("jcr:data").getStream();
> > > > >
> > > > > Where fileNode is the jcr:file node type. You wont be able to
> > actually
> > > > get
> > > > > a
> > > > > File object becuase thats part of JCR, that it extracts that stuff
> > away
> > > > > from
> > > > > you, and you just deal with the contents and info.
> > > > >
> > > > > -Nick
> > > > >
> > > > > On Wed, Jul 23, 2008 at 10:16 AM, Michael Harris <
> > > > > michael.e.harris@...>
> > > > > wrote:
> > > > >
> > > > > > ladies and gentlemen
> > > > > >
> > > > > > its still not obvious to me how to retrieve the saved file.  Can
> > > > someone
> > > > > > please ill