You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Audrey Colbrant <au...@inria.fr> on 2010/07/29 19:04:26 UTC
Creating a file
Hi,
I am trying to create a file from server side following some examples, but no way, the file is not created.
I have had a look to server logs but I don't find any error and the source code is not throwing anything.
Here the piece of code:
Node root = session.getRootNode();
Node userRoute=root.getNode("content/myProjectNode");
Node fileNode = userRoute.addNode("myFile.gpx", "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", "text/xml");
resNode.setProperty("jcr:encoding", "UTF-8");
resNode.setProperty("jcr:lastModified",Calendar.getInstance().getTimeInMillis());
resNode.setProperty("jcr:data",new ByteArrayInputStream(myString.getBytes()) );
Does anybody find something wrong?
Thanks in advance,
Audrey
Re: Creating a file
Posted by Federico Paparoni <fe...@gmail.com>.
Hi Audrey,
2010/7/29 Audrey Colbrant <au...@inria.fr>
> Hi,
>
> I am trying to create a file from server side following some examples, but
> no way, the file is not created.
> I have had a look to server logs but I don't find any error and the source
> code is not throwing anything.
>
> Here the piece of code:
> Node root = session.getRootNode();
> Node userRoute=root.getNode("content/myProjectNode");
>
> Node fileNode = userRoute.addNode("myFile.gpx", "nt:file");
> Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>
> resNode.setProperty("jcr:mimeType", "text/xml");
> resNode.setProperty("jcr:encoding", "UTF-8");
>
> resNode.setProperty("jcr:lastModified",Calendar.getInstance().getTimeInMillis());
> resNode.setProperty("jcr:data",new
> ByteArrayInputStream(myString.getBytes()) );
>
>
A stupid question: did you call save() method on the root node?
--
Federico
Re: Creating a file
Posted by Audrey Colbrant <au...@inria.fr>.
> >If you extend a SlingSafeMethodsServlet there's no automatic save(),
> >so you might want to check your code in that case.
Oh you're right, I haven't noticed that in my classes that extend SlingSafeMethodsServlet, I just retrieve content and sent it back to the client, so no need to persist anything.
Thanks for your helpful answer ;)
Audrey
Re: Creating a file
Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi Audrey,
On Fri, Jul 30, 2010 at 11:40 AM, Audrey Colbrant
<au...@inria.fr> wrote:
> ...I have defined several handlers (extending SlingSafeMethodsServlet & AbstractSlingPostOperation) in which I modify JCR content (create/delete/modify nodes and properties of nodes but never manipulated a file). I have never called save() in handlers (seems to be a mistake) and changes were persisted....
If you look at the code of AbstractSlingPostOperation, the run()
method does a save(), so that's where it happened.
If you extend a SlingSafeMethodsServlet there's no automatic save(),
so you might want to check your code in that case.
> I agree with you, it means somewhere, save() is called, but not in my code...
If you really want to find out you could just debug and set a break
point on the jackrabbit implementation of save()
> So two things:
> - why modifications are persisted if I don't call save() in my other handlers,
> while for manipulating a file I need to explicitly call this method?...
See above
> - do you recommend me to call save() in the end of all my handlers?
It's not needed in some cases (like extending
AbstractSlingPostOperation) but it generally will not hurt if you call
it - the next save() will just do nothing.
Considering the JCR transient space as a simplified transaction is
probably the best way to handle this - as long as save() is not called
on a Session, its changes are "local" to the code doing them.
-Bertrand
Re: Creating a file
Posted by Justin Edelson <ju...@gmail.com>.
On 7/30/10 5:40 AM, Audrey Colbrant wrote:
> Hi Bertrand,
>
> Thanks for your answer.
>
>>> If your other changes were persisted, a save() call must have happened
>>> somewhere.
> You've pointed out what I don't understand.
>
> I have defined several handlers (extending SlingSafeMethodsServlet & AbstractSlingPostOperation) in which I modify JCR content (create/delete/modify nodes and properties of nodes but never manipulated a file). I have never called save() in handlers (seems to be a mistake) and changes were persisted. I agree with you, it means somewhere, save() is called, but not in my code.
>
> Here, inserting a file in a class extending SlingSafeMethodsServlet, I need to explicitly call save().
A warning - SlingSafeMethodsServlet is specifically designed for
read-only operations. You should not be writing to the repository from
within a subclass of SlingSafeMethodsServlet. Use SlingAllMethodsServlet
and override doPut() or doPost().
Creating/deleting/modifying the repository inside
SlingSafeMethodsServlet will cause Roy Fielding to haunt your dreams.
Justin
>
> So two things:
> - why modifications are persisted if I don't call save() in my other handlers, while for manipulating a file I need to explicitly call this method?
> - do you recommend me to call save() in the end of all my handlers?
>
> Audrey
>
Re: Creating a file
Posted by Audrey Colbrant <au...@inria.fr>.
Hi Bertrand,
Thanks for your answer.
>>If your other changes were persisted, a save() call must have happened
>>somewhere.
You've pointed out what I don't understand.
I have defined several handlers (extending SlingSafeMethodsServlet & AbstractSlingPostOperation) in which I modify JCR content (create/delete/modify nodes and properties of nodes but never manipulated a file). I have never called save() in handlers (seems to be a mistake) and changes were persisted. I agree with you, it means somewhere, save() is called, but not in my code.
Here, inserting a file in a class extending SlingSafeMethodsServlet, I need to explicitly call save().
So two things:
- why modifications are persisted if I don't call save() in my other handlers, while for manipulating a file I need to explicitly call this method?
- do you recommend me to call save() in the end of all my handlers?
Audrey
Re: Creating a file
Posted by Bertrand Delacretaz <bd...@apache.org>.
On Fri, Jul 30, 2010 at 11:01 AM, Audrey Colbrant
<au...@inria.fr> wrote:
> ...I don't understand why I need a save() here, while in other pieces of code creating/deleting/modifing
> content of JCR, I don't need it...?...
You always need a save() at the JCR level, otherwise changes are only
made to the session's transient space.
If your other changes were persisted, a save() call must have happened
somewhere.
-Bertrand
Re: Creating a file
Posted by Audrey Colbrant <au...@inria.fr>.
> >> Did you call session.save() at the end?
I have forgotten...
It works perfectly with session.save()
I don't understand why I need a save() here, while in other pieces of code creating/deleting/modifing content of JCR, I don't need it...?
Audrey
Re: Creating a file
Posted by Eric Norman <er...@gmail.com>.
Hi Audrey,
It looks ok to me. Did you call session.save() at the end?
On Jul 29, 2010 10:03 AM, "Audrey Colbrant" <au...@inria.fr>
wrote:
Hi,
I am trying to create a file from server side following some examples, but
no way, the file is not created.
I have had a look to server logs but I don't find any error and the source
code is not throwing anything.
Here the piece of code:
Node root = session.getRootNode();
Node userRoute=root.getNode("content/myProjectNode");
Node fileNode = userRoute.addNode("myFile.gpx", "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", "text/xml");
resNode.setProperty("jcr:encoding", "UTF-8");
resNode.setProperty("jcr:lastModified",Calendar.getInstance().getTimeInMillis());
resNode.setProperty("jcr:data",new
ByteArrayInputStream(myString.getBytes()) );
Does anybody find something wrong?
Thanks in advance,
Audrey