Search in sources :

Example 1 with Mailbox

use of org.apache.james.mailbox.store.mail.model.Mailbox in project i2p.i2p-bote by i2p.

the class MapperFactory method createUidProvider.

private UidProvider createUidProvider() {
    MailboxPathLocker uidLocker = new AbstractMailboxPathLocker() {

        @Override
        protected void lock(MailboxSession session, MailboxPath path, boolean writeLock) throws MailboxException {
            BoteMailbox mailbox = getMailboxes().get(path.getName().toLowerCase());
            mailbox.lockNextUid(writeLock);
        }

        @Override
        protected void unlock(MailboxSession session, MailboxPath path, boolean writeLock) throws MailboxException {
            BoteMailbox mailbox = getMailboxes().get(path.getName().toLowerCase());
            mailbox.unlockNextUid(writeLock);
        }
    };
    return new AbstractLockingUidProvider(uidLocker) {

        @Override
        protected MessageUid lockedNextUid(MailboxSession session, Mailbox mailbox) throws MailboxException {
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            return boteMailbox.lockedNextUid();
        }

        @Override
        public Optional<MessageUid> lastUid(MailboxSession session, Mailbox mailbox) throws MailboxException {
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            return boteMailbox.lastUid();
        }
    };
}
Also used : MailboxSession(org.apache.james.mailbox.MailboxSession) MailboxPath(org.apache.james.mailbox.model.MailboxPath) MessageUid(org.apache.james.mailbox.MessageUid) AbstractLockingUidProvider(org.apache.james.mailbox.store.mail.AbstractLockingUidProvider) Mailbox(org.apache.james.mailbox.store.mail.model.Mailbox) AbstractMailboxPathLocker(org.apache.james.mailbox.store.AbstractMailboxPathLocker) MailboxPathLocker(org.apache.james.mailbox.MailboxPathLocker) AbstractMailboxPathLocker(org.apache.james.mailbox.store.AbstractMailboxPathLocker)

Example 2 with Mailbox

use of org.apache.james.mailbox.store.mail.model.Mailbox in project i2p.i2p-bote by i2p.

the class MapperFactory method createMessageMapper.

@Override
public MessageMapper createMessageMapper(MailboxSession mailboxSession) throws MailboxException {
    return new AbstractMessageMapper(mailboxSession, uidProvider, modSeqProvider) {

        @Override
        public long countMessagesInMailbox(Mailbox mailbox) throws MailboxException {
            String mailboxName = mailbox.getName().toLowerCase();
            BoteMailbox boteMailbox = getMailboxes().get(mailboxName);
            return boteMailbox == null ? -1 : boteMailbox.getNumMessages();
        }

        @Override
        public long countUnseenMessagesInMailbox(Mailbox mailbox) throws MailboxException {
            int count = 0;
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            for (BoteMessage message : boteMailbox.getAllMessages()) if (!message.isSeen())
                count++;
            return count;
        }

        @Override
        public void delete(Mailbox mailbox, MailboxMessage message) throws MailboxException {
            EmailFolder folder = ((BoteMailbox) mailbox).getFolder();
            MessageId messageId = message.getMessageId();
            boolean deleted = folderManager.deleteEmail(folder, messageId.serialize());
            if (!deleted)
                log.error("Can't delete Message " + messageId + " from folder " + folder);
        }

        @Override
        public Map<MessageUid, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox mailbox, MessageRange set) throws MailboxException {
            final Map<MessageUid, MessageMetaData> filteredResult = new HashMap<>();
            Iterator<MailboxMessage> it = findInMailbox(mailbox, set, FetchType.Metadata, -1);
            while (it.hasNext()) {
                MailboxMessage message = it.next();
                if (message.isDeleted()) {
                    filteredResult.put(message.getUid(), new SimpleMessageMetaData(message));
                    delete(mailbox, message);
                }
            }
            return filteredResult;
        }

        @Override
        public MessageUid findFirstUnseenMessageUid(Mailbox mailbox) throws MailboxException {
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            for (BoteMessage message : boteMailbox.getAllMessages()) if (!message.isSeen())
                return message.getUid();
            return null;
        }

        @Override
        public Iterator<MailboxMessage> findInMailbox(Mailbox mailbox, MessageRange set, FetchType type, int limit) throws MailboxException {
            BoteMailbox boteBox = (BoteMailbox) mailbox;
            return boteBox.getMessages(set, limit).iterator();
        }

        @Override
        public List<MessageUid> findRecentMessageUidsInMailbox(Mailbox mailbox) throws MailboxException {
            final List<MessageUid> results = new ArrayList<>();
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            for (BoteMessage message : boteMailbox.getAllMessages()) if (message.isRecent())
                results.add(message.getUid());
            Collections.sort(results);
            return results;
        }

        @Override
        public void endRequest() {
        }

        @Override
        public MessageMetaData copy(Mailbox mailbox, MessageUid uid, long modSeq, MailboxMessage original) throws MailboxException {
            BoteMailbox boteBox = (BoteMailbox) mailbox;
            try {
                BoteMessage newMessage = new BoteMessage((BoteMessage) original);
                newMessage.setUid(uid);
                newMessage.setModSeq(modSeq);
                Flags flags = original.createFlags();
                // Mark message as recent as it is a copy
                flags.add(Flag.RECENT);
                newMessage.setFlags(flags);
                boteBox.add(newMessage);
                return new SimpleMessageMetaData(newMessage);
            } catch (Exception e) {
                throw new MailboxException("Can't copy message [" + original + "] to folder [" + mailbox + "]");
            }
        }

        @Override
        public MessageMetaData move(Mailbox mailbox, MailboxMessage message) throws MailboxException {
            MessageMetaData metadata = copy(mailbox, message);
            delete(mailbox, message);
            return metadata;
        }

        @Override
        public Flags getApplicableFlag(Mailbox mailbox) throws MailboxException {
            Flags flags = new Flags();
            flags.add(Flag.ANSWERED);
            flags.add(Flag.DELETED);
            flags.add(Flag.RECENT);
            flags.add(Flag.SEEN);
            return flags;
        }

        /**
         * Updates the metadata
         */
        @Override
        protected MessageMetaData save(Mailbox mailbox, MailboxMessage message) throws MailboxException {
            // (I2P-Bote does this already, plus the email shouldn't show up in "sent" until it's actually been sent to the DHT)
            if (!(message instanceof BoteMessage))
                return new SimpleMessageMetaData(message);
            BoteMailbox boteBox = (BoteMailbox) mailbox;
            BoteMessage boteMessage = (BoteMessage) message;
            try {
                boteBox.saveMetadata(boteMessage);
                return new SimpleMessageMetaData(message);
            } catch (Exception e) {
                throw new MailboxException("Can't add message to folder " + boteBox.getFolder(), e);
            }
        }

        @Override
        protected void begin() throws MailboxException {
        // not implemented for now
        }

        @Override
        protected void commit() throws MailboxException {
        // not implemented for now
        }

        @Override
        protected void rollback() throws MailboxException {
        // not implemented for now
        }
    };
}
Also used : MessageMetaData(org.apache.james.mailbox.model.MessageMetaData) SimpleMessageMetaData(org.apache.james.mailbox.store.SimpleMessageMetaData) MailboxException(org.apache.james.mailbox.exception.MailboxException) MessageUid(org.apache.james.mailbox.MessageUid) HashMap(java.util.HashMap) SimpleMessageMetaData(org.apache.james.mailbox.store.SimpleMessageMetaData) ArrayList(java.util.ArrayList) Flags(javax.mail.Flags) MailboxException(org.apache.james.mailbox.exception.MailboxException) MailboxNotFoundException(org.apache.james.mailbox.exception.MailboxNotFoundException) SubscriptionException(org.apache.james.mailbox.exception.SubscriptionException) Mailbox(org.apache.james.mailbox.store.mail.model.Mailbox) MessageRange(org.apache.james.mailbox.model.MessageRange) AbstractMessageMapper(org.apache.james.mailbox.store.mail.AbstractMessageMapper) MailboxMessage(org.apache.james.mailbox.store.mail.model.MailboxMessage) EmailFolder(i2p.bote.folder.EmailFolder) MessageId(org.apache.james.mailbox.model.MessageId)

Example 3 with Mailbox

use of org.apache.james.mailbox.store.mail.model.Mailbox in project i2p.i2p-bote by i2p.

the class MapperFactory method createModSeqProvider.

private ModSeqProvider createModSeqProvider() {
    MailboxPathLocker modSeqLocker = new AbstractMailboxPathLocker() {

        @Override
        protected void lock(MailboxSession session, MailboxPath path, boolean writeLock) throws MailboxException {
            BoteMailbox mailbox = getMailboxes().get(path.getName().toLowerCase());
            mailbox.lockNextModSeq(writeLock);
        }

        @Override
        protected void unlock(MailboxSession session, MailboxPath path, boolean writeLock) throws MailboxException {
            BoteMailbox mailbox = getMailboxes().get(path.getName().toLowerCase());
            mailbox.unlockNextModSeq(writeLock);
        }
    };
    return new AbstractLockingModSeqProvider(modSeqLocker) {

        @Override
        protected long lockedNextModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException {
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            return boteMailbox.lockedNextModSeq();
        }

        @Override
        public long highestModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException {
            BoteMailbox boteMailbox = (BoteMailbox) mailbox;
            return boteMailbox.highestModSeq();
        }

        @Override
        public long highestModSeq(MailboxSession session, MailboxId mailboxId) throws MailboxException {
            return highestModSeq(session, getMailboxes().get(mailboxId.serialize().toLowerCase()));
        }
    };
}
Also used : MailboxSession(org.apache.james.mailbox.MailboxSession) AbstractLockingModSeqProvider(org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider) MailboxPath(org.apache.james.mailbox.model.MailboxPath) Mailbox(org.apache.james.mailbox.store.mail.model.Mailbox) MailboxId(org.apache.james.mailbox.model.MailboxId) AbstractMailboxPathLocker(org.apache.james.mailbox.store.AbstractMailboxPathLocker) MailboxPathLocker(org.apache.james.mailbox.MailboxPathLocker) AbstractMailboxPathLocker(org.apache.james.mailbox.store.AbstractMailboxPathLocker)

Aggregations

Mailbox (org.apache.james.mailbox.store.mail.model.Mailbox)3 MailboxPathLocker (org.apache.james.mailbox.MailboxPathLocker)2 MailboxSession (org.apache.james.mailbox.MailboxSession)2 MessageUid (org.apache.james.mailbox.MessageUid)2 MailboxPath (org.apache.james.mailbox.model.MailboxPath)2 AbstractMailboxPathLocker (org.apache.james.mailbox.store.AbstractMailboxPathLocker)2 EmailFolder (i2p.bote.folder.EmailFolder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Flags (javax.mail.Flags)1 MailboxException (org.apache.james.mailbox.exception.MailboxException)1 MailboxNotFoundException (org.apache.james.mailbox.exception.MailboxNotFoundException)1 SubscriptionException (org.apache.james.mailbox.exception.SubscriptionException)1 MailboxId (org.apache.james.mailbox.model.MailboxId)1 MessageId (org.apache.james.mailbox.model.MessageId)1 MessageMetaData (org.apache.james.mailbox.model.MessageMetaData)1 MessageRange (org.apache.james.mailbox.model.MessageRange)1 SimpleMessageMetaData (org.apache.james.mailbox.store.SimpleMessageMetaData)1 AbstractLockingModSeqProvider (org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider)1 AbstractLockingUidProvider (org.apache.james.mailbox.store.mail.AbstractLockingUidProvider)1