* org.apache.james.mailbox.store.mail.AbstractMessageMapper#save(org.apache
* .james.mailbox.store.mail.model.Mailbox,
* org.apache.james.mailbox.store.mail.model.Message)
*/
protected MessageMetaData save(Mailbox<Integer> mailbox, Message<Integer> message) throws MailboxException {
MaildirFolder folder = maildirStore.createMaildirFolder(mailbox);
long uid = 0;
// a new message
// save file to "tmp" folder
File tmpFolder = folder.getTmpFolder();
// The only case in which we could get problems with clashing names
// is if the system clock
// has been set backwards, then the server is restarted with the
// same pid, delivers the same
// number of messages since its start in the exact same millisecond
// as done before and the
// random number generator returns the same number.
// In order to prevent this case we would need to check ALL files in
// all folders and compare
// them to this message name. We rather let this happen once in a
// billion years...
MaildirMessageName messageName = MaildirMessageName.createUniqueName(folder, message.getFullContentOctets());
File messageFile = new File(tmpFolder, messageName.getFullName());
FileOutputStream fos = null;
InputStream input = null;
try {
messageFile.createNewFile();
fos = new FileOutputStream(messageFile);
input = ResultUtils.toInput(message);
byte[] b = new byte[BUF_SIZE];
int len = 0;
while ((len = input.read(b)) != -1)
fos.write(b, 0, len);
} catch (IOException ioe) {
throw new MailboxException("Failure while save Message " + message + " in Mailbox " + mailbox, ioe);
} finally {
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
try {
if (input != null)
input.close();
} catch (IOException e) {
}
}
File newMessageFile = null;
// delivered via SMTP, goes to ./new without flags
if (message.isRecent()) {
messageName.setFlags(message.createFlags());
newMessageFile = new File(folder.getNewFolder(), messageName.getFullName());
// System.out.println("save new recent " + message + " as " +
// newMessageFile.getName());
}
// appended via IMAP (might already have flags etc, goes to ./cur
// directly)
else {
messageName.setFlags(message.createFlags());
newMessageFile = new File(folder.getCurFolder(), messageName.getFullName());
// System.out.println("save new not recent " + message + " as "
// + newMessageFile.getName());
}
try {
FileUtils.moveFile(messageFile, newMessageFile);
} catch (IOException e) {
// TODO: Try copy and delete
throw new MailboxException("Failure while save Message " + message + " in Mailbox " + mailbox, e);
}
try {
uid = folder.appendMessage(mailboxSession, newMessageFile.getName());
message.setUid(uid);
message.setModSeq(newMessageFile.lastModified());
return new SimpleMessageMetaData(message);
} catch (MailboxException e) {
throw new MailboxException("Failure while save Message " + message + " in Mailbox " + mailbox, e);