Re: How do i get a stored file back from the repository?

2007-12-12 Thread Jukka Zitting
Hi,

Another code snippet for reading the contents of a file in a repository:

Session session = ...;
Node file = session.getRootNode().getNode("path/to/file");
InputStream content = file.getProperty("jcr:content/jcr:data").getStream();
try {
// read the content stream
} finally {
content.close();
}

Note that the above code works for typical nt:file/nt:resource nodes.
For more complex file structures you could use something like this:

InputStream getFileStream(Node file) throws ... {
Node resource = getFileResource(file);
return getResourceStream(resource);
}

Node getFileResource(Node file) throws... {
if (file.isNodeType("nt:file")) {
return file.getNode("jcr:content");
} else if (file.isNodeType("nt:linkedFile")) {
return file.getProperty("jcr:content").getNode();
} else {
// custom handling
}
}

InputStream getResourceStream(Node resource) throws {
if (resource.isNodeType("nt:resource")) {
return resource.getProperty("jcr:data").getStream();
} else {
// custom handling
}
}

You can also take a look at the IO handler abstraction used by the
Jackrabbit simple webdav feature.

BR,

Jukka Zitting


Re: How do i get a stored file back from the repository?

2007-12-11 Thread Fabián Mandelbaum
File f = new File("file_retrieved_from_the_repo.txt");
Node contentNode =
instance.session.getRootNode().getNode("/hello/world/test/jcr:content");
InputStream contentStream = contentNode.getProperty("jcr:data").getStream();
IOUtils.copy(contentStream, FileUtils.openOutputStream(f));

Please note the following:

* This example was written QUICKLY, so it may have errors, double check
everything, notably the XPATH for the getNode().

* The example uses IOUtils and FileUtils from Apache's Common IO lib
(http://commons.apache.org/io/), you can copy the stream as you wish.

Good luck!

Christian Cronen escribió:
> hey there, 
> i have saved a simple file called example.txt in my repository with the 
> following code lines. 
> root = instance.session.getRootNode();
> Node hello = root.addNode("hello");
> Node world = hello.addNode("world");
> world.setProperty("message", "Hello, 
> world!");
> Node test = world.addNode("test", 
> "nt:file");
> Node res = test.addNode("jcr:content", 
> "nt:resource");
>  
> File testfile = new 
> File("D:/example.txt");
>  
> world.setProperty("filename", 
> testfile.getName() );
> world.setProperty("size", 
> testfile.length() );
> FileInputStream in = new 
> FileInputStream(testfile);
> res.setProperty("jcr:encoding", "");
> res.setProperty("jcr:mimeType", 
> "text/plain");
> res.setProperty("jcr:data", in );
>  
> Calendar lastModified = 
> Calendar.getInstance();
> lastModified.setTimeInMillis( 
> testfile.lastModified() );
>  
> res.setProperty("jcr:lastModified", 
> lastModified);
>  
> instance.session.save();
> But now i could not get the file back from the repository. So my question 
> is what i have to do to get it back. 
>
> Kind regards
> Christian
>
>
> Christian Cronen
> Diplom-Student 
>
> maxence solutions gmbh 
> phone
> fax
> cell
> internet
> skype 
> +49-(0)-2133-2599-0
> +49-(0)-2133-2599-29
> +49-(0)-160-90532507
> [EMAIL PROTECTED]
> christian.cronen
>
>
>
> Am Weissen Stein 1
> Stuerzelbergcenter
> D - 41541 Dormagen
> Sitz der Gesellschaft: Dormagen
> Handelsregister: HRB 12561, Neuss
> Geschäftsführer: Dipl.-Math. Ralf Granderath 
> www.maxence.de
>
>
>