Jan
31
JCR for Email Storage
filed under jcr tutorial | posted by Michael Marth
Currently, I am working on importing several mailing lists into my Java Content Repository (with the ultimate aim of making them searchable on dev.day.com). While this is not really a complex thing to do I still thought I might give a pointer to help others who want to do something similar.
There is a sandbox project of Apache James (the mail server) where Jukka Zitting coded an integration of James with a JCR backend. An easy way to get started with JCR/mail integration is to reuse Jukka's JCRStoreBean (find it here in Apache's svn). The method of interest is:
public void storeMessage(Message message)
throws MessagingException,
RepositoryException {
try {
Node node = createNode(parent,
getMessageName(message), "nt:file");
importEntity(message, node);
parent.save();
} catch (IOException e) {
throw new MessagingException
("Could not read message", e);
}
}
It stores the passed javax.mail.Message. Before using it make sure to set the parent node property of the JCRStoreBean. Through the parent node JCRStoreBean has access to the repository you want to use.
Jukka has structured a mail node such that the node has a child named "content". This child node contains properties like "to", "from" as well as the actual mail body which is stored as a binary attachment (or multiple in case of multi-part mails). Here is an example node:
If you use the code you need to keep the Apache copyright notice around somewhere.
Related Posts
3 comments
I just added some extra documentation and an intermediate .../year/month/day/... folder structure to the implementation.
Here's a code snippet for using the JCRStoreBean:
JCRStoreBean bean = new JCRStoreBean();
bean.setParentNode(...);
for (Message message : ...) {
bean.storeMessage(message);
}
Note that the JCRStoreBean uses no locking or other form of synchronization, so you'll want to make sure that only a single thread is concurrently writing to the message subtree. Of course you can still have any number of readers browsing the messages even while new messages are being added.
This idea has a whole lot of good in it, here's why I think so.. http://blog.killerbees.co.uk/2008/02/storing-mime-email-in-jcr-with-james.html
Does it handle attachments properly?