Package org.apache.james.imap.api.process

Examples of org.apache.james.imap.api.process.SelectedMailbox


                            }
                        }
                    }

                    SelectedMailbox sm = session.getSelected();
                    if (sm != null) {
                        // We need to add the UID of the message to the recent
                        // list if we receive an flag update which contains a
                        // \RECENT flag
                        // See IMAP-287
                        List<UpdatedFlags> uflags = updated.getUpdatedFlags();
                        for (int i = 0; i < uflags.size(); i++) {
                            UpdatedFlags u = uflags.get(i);
                            Iterator<Flag> flags = u.systemFlagIterator();

                            while (flags.hasNext()) {
                                if (Flag.RECENT.equals(flags.next())) {
                                    MailboxPath path = sm.getPath();
                                    if (path != null && path.equals(event.getMailboxPath())) {
                                        sm.addRecent(u.getUid());
                                    }
                                }
                            }
                         
View Full Code Here


             
            }
            final List<Long> failed = new ArrayList<Long>();
            final List<String> userFlags = Arrays.asList(flags.getUserFlags());
            for (int i = 0; i < idSet.length; i++) {
                final SelectedMailbox selected = session.getSelected();
                MessageRange messageSet = messageRange(selected, idSet[i], useUids);
                if (messageSet != null) {
                   
                    if (unchangedSince != -1) {
                        // Ok we have a CONDSTORE option so use the CONDSTORE_COMMAND
                        imapCommand = CONDSTORE_COMMAND;
                       
                        List<Long> uids = new ArrayList<Long>();

                        MessageResultIterator results = mailbox.getMessages(messageSet, FetchGroupImpl.MINIMAL, mailboxSession);
                        while(results.hasNext()) {
                            MessageResult r = results.next();
                            long uid = r.getUid();
                           
                            boolean fail = false;
                           
                            // Check if UNCHANGEDSINCE 0 was used and the Message contains the request flag.
                            // In such cases we need to fail for this message.
                            //
                            // From RFC4551:
                            //       Use of UNCHANGEDSINCE with a modification sequence of 0 always
                            //       fails if the metadata item exists.  A system flag MUST always be
                            //       considered existent, whether it was set or not.
                            if (unchangedSince == 0) {
                                String[] uFlags = r.getFlags().getUserFlags();
                                for (int a = 0; a < uFlags.length; a++) {
                                    if (userFlags.contains(uFlags[a])) {
                                        fail = true;
                                        break;
                                    }
                                }
                            }
                           
                            // Check if the mod-sequence of the message is <= the unchangedsince.
                            //
                            // See RFC4551 3.2. STORE and UID STORE Commands
                            if (!fail && r.getModSeq() <= unchangedSince) {
                                uids.add(uid);
                            } else {
                                if (useUids) {
                                    failed.add(uid);
                                } else {
                                    failed.add((long) selected.msn(uid));
                                }
                            }
                        }
                        List<MessageRange> mRanges = MessageRange.toRanges(uids);
                        for (int a = 0 ; a < mRanges.size(); a++) {
View Full Code Here

        } else {
            replace = true;
            value = true;
        }
       
        SelectedMailbox selected = session.getSelected();
        final Map<Long, Flags> flagsByUid = mailbox.setFlags(flags, value, replace, messageSet, mailboxSession);
        // As the STORE command is allowed to create a new "flag/keyword", we need to send a FLAGS and PERMANENTFLAGS response before the FETCH response
        // if some new flag/keyword was used
        // See IMAP-303
        if (selected.hasNewApplicableFlags()) {
            flags(responder, selected);
            permanentFlags(responder, mailbox.getMetaData(false, mailboxSession, FetchGroup.NO_COUNT), selected);
            selected.resetNewApplicableFlags();
        }
       
        Set<String> enabled = EnableProcessor.getEnabledCapabilities(session);
        boolean qresyncEnabled = enabled.contains(ImapConstants.SUPPORTS_QRESYNC);
        boolean condstoreEnabled = enabled.contains(ImapConstants.SUPPORTS_CONDSTORE);
       
        if (!silent || unchangedSince != -1 || qresyncEnabled || condstoreEnabled) {
            final Map<Long, Long> modSeqs = new HashMap<Long, Long>();
          
            // Check if we need to also send the the mod-sequences back to the client
            //
            // This is the case if one of these is true:
            //      - UNCHANGEDSINCE was used
            //      - CONDSTORE was enabled via ENABLE CONDSTORE
            //      - QRESYNC was enabled via ENABLE QRESYNC
            //
            if (unchangedSince != -1 || qresyncEnabled || condstoreEnabled) {
                MessageResultIterator results = mailbox.getMessages(messageSet, FetchGroupImpl.MINIMAL, mailboxSession);
                while(results.hasNext()) {
                    MessageResult r = results.next();
                    // Store the modseq for the uid for later usage in the response
                    modSeqs.put(r.getUid(),r.getModSeq());
                }
            }
           
            for (Map.Entry<Long, Flags> entry : flagsByUid.entrySet()) {
                final long uid = entry.getKey();
                final int msn = selected.msn(uid);

                if (msn == SelectedMailbox.NO_SUCH_MESSAGE) {
                    if(session.getLog().isDebugEnabled()) {
                        session.getLog().debug("No message found with uid " + uid + " in the uid<->msn mapping for mailbox " + selected.getPath().getFullName(mailboxSession.getPathDelimiter()) +" , this may be because it was deleted by a concurrent session. So skip it..");
                       
                    }
                    // skip this as it was not found in the mapping
                    //
                    // See IMAP-346
                    continue;
                }

                final Flags resultFlags = entry.getValue();
                final Long resultUid;
               
                // Check if we need to include the uid. T
                //
                // This is the case if one of these is true:
                //      - FETCH (UID...)  was used
                //      - QRESYNC was enabled via ENABLE QRESYNC
                if (useUids || qresyncEnabled) {
                    resultUid = uid;
                } else {
                    resultUid = null;
                }

                if (selected.isRecent(uid)) {
                    resultFlags.add(Flags.Flag.RECENT);
                }
              
                final FetchResponse response;
                // For more informations related to the FETCH response see
View Full Code Here

        }
       
       
       
        final MessageManager.MetaData metaData = selectMailbox(fullMailboxPath, session);
        final SelectedMailbox selected = session.getSelected();
        Long firstUnseen = metaData.getFirstUnseen();
       
        flags(responder, selected);
        exists(responder, metaData);
        recent(responder, selected);
        uidValidity(responder, metaData);
       
       
        // try to write the UNSEEN message to the client and retry if we fail because of concurrent sessions.
        //
        // See IMAP-345
        int retryCount = 0;
        while(unseen(responder, firstUnseen, selected, ImapSessionUtils.getMailboxSession(session)) == false) {
            // if we not was able to get find the unseen within 5 retries we should just not send it
            if (retryCount == 5) {
                if (session.getLog().isInfoEnabled()) {
                    session.getLog().info("Unable to uid for unseen message " + firstUnseen + " in mailbox " + selected.getPath());
                }
                break;
            }
            firstUnseen = selectMailbox(fullMailboxPath, session).getFirstUnseen();
            retryCount++;
           
        }
       
        permanentFlags(responder, metaData, selected);
        highestModSeq(responder, metaData, selected);
        uidNext(responder, metaData);
       
        if (request.getCondstore()) {
           condstoreEnablingCommand(session, responder, metaData, false);
        }
       
        // Now do the QRESYNC processing if necessary
        //
        // If the mailbox does not store the mod-sequence in a permanent way its needed to not process the QRESYNC paramters
        // The same is true if none are given ;)
        if (metaData.isModSeqPermanent() && lastKnownUidValidity != null) {
            if (lastKnownUidValidity == metaData.getUidValidity()) {
               
                final MailboxManager mailboxManager = getMailboxManager();
                final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);
                final MessageManager mailbox = mailboxManager.getMailbox(fullMailboxPath, mailboxSession);
              
               
                //  If the provided UIDVALIDITY matches that of the selected mailbox, the
                //  server then checks the last known modification sequence.
                //
                //  The server sends the client any pending flag changes (using FETCH
                //  responses that MUST contain UIDs) and expunges those that have
                //  occurred in this mailbox since the provided modification sequence.
                SearchQuery sq = new SearchQuery();
                sq.andCriteria(SearchQuery.modSeqGreaterThan(request.getKnownModSeq()));
               
                IdRange[] uidSet = request.getUidSet();

                if (uidSet == null) {
                    // See mailbox had some messages stored before, if not we don't need to query at all
                    long uidNext = metaData.getUidNext();
                    if ( uidNext != 1) {
                        // Use UIDNEXT -1 as max uid as stated in the QRESYNC RFC
                        uidSet = new IdRange[] {new IdRange(1, uidNext -1)};
                    }
                }
               
                if (uidSet != null) {
                    // RFC5162 3.1. QRESYNC Parameter to SELECT/EXAMINE
                    //
                    // Message sequence match data:
                    //
                    //      A client MAY provide a parenthesized list of a message sequence set
                    //      and the corresponding UID sets.  Both MUST be provided in ascending
                    //      order.  The server uses this data to restrict the range for which it
                    //      provides expunged message information.
                    //
                    //
                    //      Conceptually, the client provides a small sample of sequence numbers
                    //      for which it knows the corresponding UIDs.  The server then compares
                    //      each sequence number and UID pair the client provides with the
                    //      current state of the mailbox.  If a pair matches, then the client
                    //      knows of any expunges up to, and including, the message, and thus
                    //      will not include that range in the VANISHED response, even if the
                    //      "mod-sequence-value" provided by the client is too old for the server
                    //      to have data of when those messages were expunged.
                    //
                    //      Thus, if the Nth message number in the first set in the list is 4,
                    //      and the Nth UID in the second set in the list is 8, and the mailbox's
                    //      fourth message has UID 8, then no UIDs equal to or less than 8 are
                    //      present in the VANISHED response.  If the (N+1)th message number is
                    //      12, and the (N+1)th UID is 24, and the (N+1)th message in the mailbox
                    //      has UID 25, then the lowest UID included in the VANISHED response
                    //      would be 9.
                    if (knownSequences != null && knownUids != null) {
                       
                        // Add all uids which are contained in the knownuidsset to a List so we can later access them via the index
                        List<Long> knownUidsList = new ArrayList<Long>();
                        for (int a = 0; a < knownUids.length; a++) {
                            Iterator<Long> it =  knownUids[a].iterator();
                           
                            while(it.hasNext()) {
                                knownUidsList.add(it.next());
                            }
                        }
                      
                       
                       
                        // loop over the known sequences and check the UID for MSN X again the known UID X
                        long firstUid = 1;
                        int index = 0;
                        for (int a = 0; a < knownSequences.length; a++) {
                            boolean done = false;
                            Iterator<Long> it =  knownSequences[a].iterator();
                            while(it.hasNext()) {
                               
                                // Check if we have uids left to check against
                                if (knownUidsList.size() > index++) {
                                    int msn = it.next().intValue();
                                    long knownUid = knownUidsList.get(index);
                                   
                                    // Check if the uid mathc if not we are done here
                                    if (selected.uid(msn) != knownUid) {
                                        done = true;
                                        break;
                                    } else {
                                        firstUid = knownUid;
                                    }
View Full Code Here

    private MessageManager.MetaData selectMailbox(MailboxPath mailboxPath, ImapSession session) throws MailboxException {
        final MailboxManager mailboxManager = getMailboxManager();
        final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);
        final MessageManager mailbox = mailboxManager.getMailbox(mailboxPath, mailboxSession);

        final SelectedMailbox sessionMailbox;
        final SelectedMailbox currentMailbox = session.getSelected();
        if (currentMailbox == null || !currentMailbox.getPath().equals(mailboxPath)) {
           
            // QRESYNC EXTENSION
            //
            // Response with the CLOSE return-code when the currently selected mailbox is closed implicitly using the SELECT/EXAMINE command on another mailbox
            //
View Full Code Here

     * @see org.apache.james.imap.processor.PermitEnableCapabilityProcessor#enable(org.apache.james.imap.api.ImapMessage, org.apache.james.imap.api.process.ImapProcessor.Responder, org.apache.james.imap.api.process.ImapSession, java.lang.String)
     */
    public void enable(ImapMessage message, Responder responder, ImapSession session, String capability) throws EnableException {

        if (EnableProcessor.getEnabledCapabilities(session).contains(capability) == false) {
            SelectedMailbox sm = session.getSelected();
            // Send a HIGHESTMODSEQ response if the there was a select mailbox before and the client just enabled
            // QRESYNC or CONDSTORE
            //
            // See http://www.dovecot.org/list/dovecot/2008-March/029561.html
            if (capability.equalsIgnoreCase(ImapConstants.SUPPORTS_CONDSTORE)|| capability.equalsIgnoreCase(ImapConstants.SUPPORTS_QRESYNC)) {
View Full Code Here

    }

    private void appendToMailbox(final InputStream message, final Date datetime, final Flags flagsToBeSet, final ImapSession session, final String tag, final ImapCommand command, final MessageManager mailbox, Responder responder, final MailboxPath mailboxPath) {
        try {
            final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);
            final SelectedMailbox selectedMailbox = session.getSelected();
            final MailboxManager mailboxManager = getMailboxManager();
            final boolean isSelectedMailbox = selectedMailbox != null && selectedMailbox.getPath().equals(mailboxPath);
            final long uid = mailbox.appendMessage(message, datetime, mailboxSession, !isSelectedMailbox, flagsToBeSet);
            if (isSelectedMailbox) {
                selectedMailbox.addRecent(uid);
            }

            // get folder UIDVALIDITY
            Long uidValidity = mailboxManager.getMailbox(mailboxPath, mailboxSession).getMetaData(false, mailboxSession, FetchGroup.NO_UNSEEN).getUidValidity();
View Full Code Here

        }
    }

    private int expunge(MessageManager mailbox, MessageRange range, ImapSession session, MailboxSession mailboxSession) throws MailboxException {
        final Iterator<Long> it = mailbox.expunge(range, mailboxSession);
        final SelectedMailbox selected = session.getSelected();
        int expunged = 0;
        if (mailboxSession != null) {
            while (it.hasNext()) {
                final long uid = it.next();
                selected.removeRecent(uid);
                expunged++;
            }
        }
        return expunged;
    }
View Full Code Here

    }


    private SearchQuery toQuery(final SearchKey key, final ImapSession session) throws MessageRangeException {
        final SearchQuery result = new SearchQuery();
        final SelectedMailbox selected = session.getSelected();
        if (selected != null) {
            result.addRecentMessageUids(selected.getRecent());
        }
        final SearchQuery.Criterion criterion = toCriterion(key, session);
        result.andCriteria(criterion);
        return result;
    }
View Full Code Here

     * @throws MessageRangeException
     */
    private Criterion sequence(IdRange[] sequenceNumbers, final ImapSession session, boolean msn) throws MessageRangeException {
        final int length = sequenceNumbers.length;
        final List<SearchQuery.NumericRange> ranges = new ArrayList<SearchQuery.NumericRange>();
        final SelectedMailbox selected = session.getSelected();
        boolean useUids = !msn;

        // First of check if we have any messages in the mailbox
        // if not we don't need to go through all of this
        if (selected.existsCount() > 0) {
            for (int i = 0; i < length; i++) {
                final IdRange range = sequenceNumbers[i];

                long lowVal = range.getLowVal();
                long highVal = range.getHighVal();
                if (useUids) {
                    // Take care of "*" and "*:*" values by return the last
                    // message in
                    // the mailbox. See IMAP-289
                    if (lowVal == Long.MAX_VALUE && highVal == Long.MAX_VALUE) {
                        ranges.add(new SearchQuery.NumericRange(selected.getLastUid()));
                    } else if (highVal == Long.MAX_VALUE && selected.getLastUid() < lowVal) {
                        // Sequence uid ranges which use
                        // *:<uid-higher-then-last-uid>
                        // MUST return at least the highest uid in the mailbox
                        // See IMAP-291
                        ranges.add(new SearchQuery.NumericRange(selected.getLastUid()));
                    } else {
                        ranges.add(new SearchQuery.NumericRange(lowVal, highVal));
                    }
                } else {
                    // Take care of "*" and "*:*" values by return the last
                    // message in
                    // the mailbox. See IMAP-289
                    if (lowVal == Long.MAX_VALUE && highVal == Long.MAX_VALUE) {
                        highVal = selected.getLastUid();

                        ranges.add(new SearchQuery.NumericRange(highVal));
                    } else {
                        if (lowVal != Long.MIN_VALUE) {
                            lowVal = selected.uid((int) lowVal);
                        } else {
                            lowVal = selected.getFirstUid();
                        }

                        // The lowVal should never be
                        // SelectedMailbox.NO_SUCH_MESSAGE but we check for it
                        // just to be safe
                        if (lowVal != SelectedMailbox.NO_SUCH_MESSAGE) {
                            if (highVal != Long.MAX_VALUE) {
                                highVal = selected.uid((int) highVal);
                                if (highVal == SelectedMailbox.NO_SUCH_MESSAGE) {
                                    // we requested a message with a MSN higher
                                    // then
                                    // the current msg count. So just use the
                                    // highest uid as max
                                    highVal = selected.getLastUid();
                                }
                            } else {
                                highVal = selected.getLastUid();
                            }
                            ranges.add(new SearchQuery.NumericRange(lowVal, highVal));
                        }
                    }
                }
View Full Code Here

TOP

Related Classes of org.apache.james.imap.api.process.SelectedMailbox

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.