Return-Path: Delivered-To: apmail-jackrabbit-users-archive@locus.apache.org Received: (qmail 16126 invoked from network); 23 Jul 2008 15:02:19 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 Jul 2008 15:02:19 -0000 Received: (qmail 40407 invoked by uid 500); 23 Jul 2008 15:02:16 -0000 Delivered-To: apmail-jackrabbit-users-archive@jackrabbit.apache.org Received: (qmail 40397 invoked by uid 500); 23 Jul 2008 15:02:16 -0000 Mailing-List: contact users-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@jackrabbit.apache.org Delivered-To: mailing list users@jackrabbit.apache.org Received: (qmail 40386 invoked by uid 99); 23 Jul 2008 15:02:16 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2008 08:02:16 -0700 X-ASF-Spam-Status: No, hits=3.2 required=10.0 tests=HTML_MESSAGE,NORMAL_HTTP_TO_IP,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [209.85.198.225] (HELO rv-out-0506.google.com) (209.85.198.225) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2008 15:01:23 +0000 Received: by rv-out-0506.google.com with SMTP id k40so2277976rvb.31 for ; Wed, 23 Jul 2008 08:01:48 -0700 (PDT) Received: by 10.140.126.14 with SMTP id y14mr123489rvc.59.1216825308203; Wed, 23 Jul 2008 08:01:48 -0700 (PDT) Received: by 10.141.122.15 with HTTP; Wed, 23 Jul 2008 08:01:48 -0700 (PDT) Message-ID: <272c2bf60807230801x4f8165f6j53df14ef42aa8494@mail.gmail.com> Date: Wed, 23 Jul 2008 11:01:48 -0400 From: "Nick Stuart" To: users@jackrabbit.apache.org Subject: Re: saving files In-Reply-To: <82670ba40807230756x2e1757c7vc07266be8a345a8b@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_40379_2517963.1216825308192" References: <82670ba40807221022w7d56f206h2d5ca6e18385b645@mail.gmail.com> <488624DA.9000804@gmx.de> <82670ba40807221205t1d59daf5see3fde2703f8a617@mail.gmail.com> <82670ba40807230716m43b7925wb9d72862d78d18c0@mail.gmail.com> <272c2bf60807230722i2243524g143804696c59f613@mail.gmail.com> <82670ba40807230735n3164b1a3qda1eb5b391a7f675@mail.gmail.com> <272c2bf60807230738r3f00d87bic595d0609d66fa03@mail.gmail.com> <82670ba40807230756x2e1757c7vc07266be8a345a8b@mail.gmail.com> X-Virus-Checked: Checked by ClamAV on apache.org ------=_Part_40379_2517963.1216825308192 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 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 > > 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@gmail.com> > > 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@speranzasystems.com > > > > > > > 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@gmail.com> > > > > 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@gmail.com> > > > > > wrote: > > > > > > > > > > > comments inline below. Thank you for the response. > > > > > > > > > > > > On Tue, Jul 22, 2008 at 2:20 PM, Julian Reschke < > > > julian.reschke@gmx.de > > > > > > > > > > > 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 > ------=_Part_40379_2517963.1216825308192--