jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexander Klimetschek" <aklim...@day.com>
Subject Re: reading a file from the jacrabbit repository
Date Sun, 23 Nov 2008 13:09:41 GMT
Hi,

not sure what the actual problem is, but two things I noted in your code:

- session.save() can throw an exception (eg. if there is a jcr
constraint violation, etc. - many validations in Jackrabbit are hold
back until you actually save the session)
=> don't put it in the finally block but before

- You shouldn't put a return statement in a finally block. It might
not be a difference, but I am not sure what happens in Java if there
is an exception in your code, then the finally block is executed after
the exception and suddenly there is a return statement - will the
exception throwing (to the calling method) continue or will the return
statement be executed?
=> put the return inside the try block

Regards,
Alex

On Sun, Nov 23, 2008 at 12:41 PM, far4ever <fathimafarhath@gmail.com> wrote:
>
> hi
> i added an image to the repository but when i tried to get it back i wont
> get it. instead i seems to be null..
> is it a mistake is reading the repository or isn't the file is getting save
> ( because there is no way of checking if the file is loaded by using the
> file explorer )
> please help.
>  the code of adding the file ------
> try {
>            Repository repository = new TransientRepository();
>            session = repository.login(new SimpleCredentials("username",
> "password".toCharArray()));
>
>            System.out.println("File Content length - " + file.length());
>            MimeTable mt = MimeTable.getDefaultTable();
>            String mimeType = mt.getContentTypeFor(file.getName());
>
>            System.out.println("MIME Type - " + mimeType);
>            if (mimeType == null) {
>                mimeType = "application/octet-stream";
>            }
>            Node root = session.getRootNode();
>
>            Node userFile = root.addNode(id, "nt:folder");
>            Node fileNode = userFile.addNode("picture", "nt:file");
>            Node resNode = fileNode.addNode("jcr:content", "nt:resource");
>            resNode.setProperty("jcr:mimeType", mimeType);
>            resNode.setProperty("jcr:data", new FileInputStream(file));
>            resNode.setProperty("jcr:encoding", "");
>            Calendar lastModified = Calendar.getInstance();
>            lastModified.setTimeInMillis(file.lastModified());
>            resNode.setProperty("jcr:lastModified", lastModified);
>            System.out.println("it s after saving the file.");
>            done = true;
>        } finally{
>            session.save();
>             session.logout();
>            System.out.println("there ");
>            return done;
>
>        }
> -----------
> code to get the file is
>
>
> Session session = null;
>         FileInputStream picture = null;
>         try{
>
>
>         Repository repository = new TransientRepository();
>         session = repository.login(new SimpleCredentials("username",
> "password".toCharArray()));
>         Node root = session.getRootNode();
>         Node userfile = root.getNode(id);
>         Node pic = userfile.getNode("picture");
>         Node image = pic.getNode("jcr:content");
>         picture = (FileInputStream)
> image.getProperty("jcr:data").getValue().getStream();
>         if(picture == null)
>             System.out.println("errorr in reading the file");
>         }
>         finally{
>         session.logout();
>         System.out.println("after reading the image");
>
>         return picture;
>         }
>
> --
> View this message in context: http://www.nabble.com/reading-a-file-from-the-jacrabbit-repository-tp20645363p20645363.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>
>


-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Mime
View raw message