You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by garihood <ga...@flambeau.com> on 2013/04/22 17:10:35 UTC
Learning Jackrabbit - Extracted files are length zero
Hello.
I'm learning Jackrabbit and am running into a problem extracting files maybe
someone can point me to where I'm going wrong. I've checked the forum here
and didn't find anything (or I missed something) that really answered my
question. Every file I try to extract from the repository ends up being zero
bytes long.
I have the following code that is called from a loop that traverses a folder
structure. From what I can tell, the information is inserted properly. That
is, I can later traverse the repository and see all the nodes and
properties. Here is the traversal and import code that I'm using.
TRAVERSAL:
private static void importFolder(Node parentnode,
File directory) throws
RepositoryException,
IOException {
File[] direntries = directory.listFiles();
System.out.println(parentnode.getPath());
for (int i = 0; i < direntries.length; i++) {
File direntry = direntries[i];
if (direntry.isDirectory()) {
Node childnode =
parentnode.addNode(direntry.getName(), "nt:folder");
importFolder(childnode, direntry);
} else {
importFile(parentnode, direntry);
}
}
}
IMPORT:
public static void importFile(Node parentnode,
File file) throws RepositoryException,
IOException {
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";
Node fileNode = parentnode.addNode(file.getName(), "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", 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);
System.out.println(fileNode.getPath());
}
I am using the following code to extract the file. As I mentioned before,
the problem is that the file I get out, while it has the correct filename,
always has a file size of zero bytes.
private static void ExtractFile(Session session, String nodePath, String
fileName) throws RepositoryException {
log.debug("In ExtractFile");
Node root = session.getRootNode();
Node pathNode = root.getNode(nodePath);
Node fileNode = pathNode.getNode(fileName);
Node content = fileNode.getNode("jcr:content");
String path = content.getPath();
log.debug("Path = " + path);
Binary bin =
session.getNode(path).getProperty("jcr:data").getBinary();
InputStream stream = bin.getStream();
File f=new File("C:\\TEMP\\"+fileName);
try {
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len = 0;
try {
while(stream.read(buf)>0)
out.write(buf,0,len);
out.close();
stream.close();
log.debug("\nFile is
created...................................");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Any insights you can provide will be greatly appreciated.
-Greg
--
View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: Learning Jackrabbit - Extracted files are length zero
Posted by Alexander Klimetschek <ak...@adobe.com>.
Please note that the logs etc. did not come through to the mailing list. I can see them on Nabble, but please note that Nabble is not the reference for the mail archive and doesn't respect the actual mailling list rules, for example not allowing attachments. So I would avoid using nabble and subscribe to the list directly.
For read-only browsing and searching the lists I usually prefer markmail: http://jackrabbit.markmail.org (but that's also an external provider, not the reference)
Cheers,
Alex
On 23.04.2013, at 17:30, garihood <ga...@flambeau.com> wrote:
> Alex,
>
> Thank you for the reply and here are the answers to your questions:
>
>
> I don't think there are errors in the log. Here are the log entries that I
> think relate to the storing of the file. I'm a Jackrabbit newb, so this
> might not be right.
>
>
>
> There is a session save right after I iterate through all the folders and
> import the data into the repository. This code is from the main() method.
>
>
>
> I am able to read non-binary properties. I get what looks like valid output
> from the dump() method that immediately follows the session.save(). It
> iterates through the entire content of the repository and outputs what has
> been stored. Here is the portion of that output that deals with the file I
> am trying to extract.
>
>
>
>
> Since I'm new to Jackrabbit, I'm assuming that this is what you what to know
> when you asked about the repository/persistence manager configuration.
>
>
>
>
>
> --
> View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658402.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
RE: AW: Learning Jackrabbit - Extracted files are length zero
Posted by garihood <ga...@flambeau.com>.
Ulrich,
Thank you. That was it.
That's what I get for copying and pasting code from the internet and not looking it over line by line. Still, I don't know how I missed that everything was being written from offset 0.
Greg Arihood
Programmer/Analyst
801 Lynn Ave
Baraboo, WI 53913
Phone: 608-356-5551 6223
Fax: 608-355-2293
Email: garihood@flambeau.com
www.flambeau.com<http://www.flambeau.com>
[Our Website...]<http://www.flambeau.com/>
[http://images.flambeau.com/sig/img/shaping...inplastic.jpg]
"This message is intended for the viewing and use of the listed parties only. E-mail is not a secure transmission medium and while it is convenient, careful consideration should be given prior to its use to communicate information of a confidential nature. If you elect to send or receive information via e-mail, Flambeau cannot provide any assurance regarding security and will not be liable if the e-mail transmission is intercepted or viewed by another party."
From: Ulrich [via Jackrabbit] [mailto:ml-node+s510166n4658428h26@n4.nabble.com]
Sent: Thursday, April 25, 2013 12:59 AM
To: Arihood, Greg
Subject: Re: AW: Learning Jackrabbit - Extracted files are length zero
You have got a logic error in your ExtractFile()-method. You do a
"out.write(buf,0,len);" but don't set len other than "0".
Ulrich
garihood <[hidden email]</user/SendEmail.jtp?type=node&node=4658428&i=0>> hat am 23. April 2013 um 17:36 geschrieben:
> Thanks for the tip. I will do that.
>
>
>
> --
> View this message in context:
> http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658404.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
________________________________
If you reply to this email, your message will be added to the discussion below:
http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658428.html
To unsubscribe from Learning Jackrabbit - Extracted files are length zero, click here<http://jackrabbit.510166.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4658373&code=Z2FyaWhvb2RAZmxhbWJlYXUuY29tfDQ2NTgzNzN8LTI2OTY2NjUxNQ==>.
NAML<http://jackrabbit.510166.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
image001.jpg (39K) <http://jackrabbit.510166.n4.nabble.com/attachment/4658443/0/image001.jpg>
image002.jpg (5K) <http://jackrabbit.510166.n4.nabble.com/attachment/4658443/1/image002.jpg>
--
View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658443.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: AW: Learning Jackrabbit - Extracted files are length zero
Posted by Ulrich <Fo...@gombers.de>.
You have got a logic error in your ExtractFile()-method. You do a
"out.write(buf,0,len);" but don't set len other than "0".
Ulrich
garihood <ga...@flambeau.com> hat am 23. April 2013 um 17:36 geschrieben:
> Thanks for the tip. I will do that.
>
>
>
> --
> View this message in context:
> http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658404.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: AW: Learning Jackrabbit - Extracted files are length zero
Posted by garihood <ga...@flambeau.com>.
Thanks for the tip. I will do that.
--
View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658404.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
AW: Learning Jackrabbit - Extracted files are length zero
Posted by "Cech. Ulrich" <Ul...@aeb.de>.
< There is a session save right after I iterate through all the folders>
Try to make a session.save() after every imported file, not after the whole iteration.
________________________________
AEB treffen Sie im Juni auf diesen Veranstaltungen:
transport logistic | 4.-7. Juni 2013 | München
EXCHAiNGE | 18.-19. Juni 2013 | Frankfurt am Main
Weitere Informationen und Terminreservierung unter: www.aeb.de/events<http://logi4.xiti.com/gopc.url?xts=487638&xtor=AD-5-[aeb%20mails]-[link%20in%20mailsignatur]-[intext]-[e-mail-signatur]-[0]-[]&url=http://www.aeb.de/de/events/index.php>
Re: Learning Jackrabbit - Extracted files are length zero
Posted by garihood <ga...@flambeau.com>.
Alex,
Thank you for the reply and here are the answers to your questions:
I don't think there are errors in the log. Here are the log entries that I
think relate to the storing of the file. I'm a Jackrabbit newb, so this
might not be right.
There is a session save right after I iterate through all the folders and
import the data into the repository. This code is from the main() method.
I am able to read non-binary properties. I get what looks like valid output
from the dump() method that immediately follows the session.save(). It
iterates through the entire content of the repository and outputs what has
been stored. Here is the portion of that output that deals with the file I
am trying to extract.
Since I'm new to Jackrabbit, I'm assuming that this is what you what to know
when you asked about the repository/persistence manager configuration.
--
View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658402.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: Learning Jackrabbit - Extracted files are length zero
Posted by Alexander Klimetschek <ak...@adobe.com>.
Can you read the other non-binary properties? Do you do a session save? Do you seen any errors in the log? What is your repository / persistence manager configuration?
Cheers,
Alex
On 22.04.2013, at 18:05, garihood <ga...@flambeau.com> wrote:
> Thanks for the reply.
>
> I've created an InputStream variable:
> InputStream input = new FileInputStream(file);
>
> and replaced my old code:
> resNode.setProperty("jcr:data", new FileInputStream(file));
>
> with your suggestion:
> resNode.setProperty(JcrConstants.JCR_DATA,
>
> resNode.getSession().getValueFactory().createBinary(input));
>
> Unfortunately, the files I try to extract remain 0 bytes long.
>
>
>
> This is the current importFile() method:
>
> public static void importFile(Node parentnode,
> File file) throws RepositoryException,
> IOException {
> InputStream input = new FileInputStream(file);
>
> MimeTable mt = MimeTable.getDefaultTable();
> String mimeType = mt.getContentTypeFor(file.getName());
> if (mimeType == null)
> mimeType = "application/octet-stream";
>
> Node fileNode = parentnode.addNode(file.getName(), "nt:file");
> Node resNode = fileNode.addNode("jcr:content", "nt:resource");
> resNode.setProperty("jcr:mimeType", mimeType);
> resNode.setProperty("jcr:encoding", "");
> resNode.setProperty(JcrConstants.JCR_DATA,
>
> resNode.getSession().getValueFactory().createBinary(input));
> Calendar lastModified = Calendar.getInstance();
> lastModified.setTimeInMillis(file.lastModified());
> resNode.setProperty("jcr:lastModified", lastModified);
> System.out.println(fileNode.getPath());
> }
>
>
>
>
> --
> View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658375.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
Re: AW: Learning Jackrabbit - Extracted files are length zero
Posted by garihood <ga...@flambeau.com>.
Thanks for the reply.
I've created an InputStream variable:
InputStream input = new FileInputStream(file);
and replaced my old code:
resNode.setProperty("jcr:data", new FileInputStream(file));
with your suggestion:
resNode.setProperty(JcrConstants.JCR_DATA,
resNode.getSession().getValueFactory().createBinary(input));
Unfortunately, the files I try to extract remain 0 bytes long.
This is the current importFile() method:
public static void importFile(Node parentnode,
File file) throws RepositoryException,
IOException {
InputStream input = new FileInputStream(file);
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null)
mimeType = "application/octet-stream";
Node fileNode = parentnode.addNode(file.getName(), "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:encoding", "");
resNode.setProperty(JcrConstants.JCR_DATA,
resNode.getSession().getValueFactory().createBinary(input));
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(file.lastModified());
resNode.setProperty("jcr:lastModified", lastModified);
System.out.println(fileNode.getPath());
}
--
View this message in context: http://jackrabbit.510166.n4.nabble.com/Learning-Jackrabbit-Extracted-files-are-length-zero-tp4658373p4658375.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
AW: Learning Jackrabbit - Extracted files are length zero
Posted by "Cech. Ulrich" <Ul...@aeb.de>.
< resNode.setProperty("jcr:data", new FileInputStream(file));>
I think, the problem lies here in the importFile() method:
Try out this one:
resNode.setProperty(JcrConstants.JCR_DATA, resNode.getSession().getValueFactory().createBinary(input));
important: "input" is an InputStream to the file, you want to import