001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 *
019 * $Id: EndAction.java 55400 2008-05-26 09:46:02Z atchertchian $
020 */
021
022package org.nuxeo.ecm.platform.mail.listener.action;
023
024import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.IMAP;
025import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.IMAPS;
026import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.LEAVE_ON_SERVER_KEY;
027import static org.nuxeo.ecm.platform.mail.utils.MailCoreConstants.PROTOCOL_TYPE_KEY;
028
029import javax.mail.Flags.Flag;
030import javax.mail.Message;
031import javax.mail.MessagingException;
032
033import org.nuxeo.ecm.platform.mail.action.ExecutionContext;
034
035/**
036 * @author Catalin Baican
037 */
038public class EndAction extends AbstractMailAction {
039
040    @Override
041    public boolean execute(ExecutionContext context) {
042        try {
043            Message message = context.getMessage();
044            // erase marker: mail has been treated
045            // VDU it could be nice to have a field in schema 'protocol' that says
046            // messages stay in server or not. This only work with IMAP* protocols, as POP3*
047            // protocols does not support flags other than DELETED.
048            // message.setFlag(Flag.FLAGGED, false);
049            boolean leaveOnServer = (Boolean) context.getInitialContext().get(LEAVE_ON_SERVER_KEY);
050            String protocolType = (String) context.getInitialContext().get(PROTOCOL_TYPE_KEY);
051            // log.debug(PROTOCOL_TYPE_KEY + ": " + protocolType);
052            // log.debug(LEAVE_ON_SERVER_KEY + ": " + leaveOnServer);
053            if ((IMAP.equals(protocolType) || IMAPS.equals(protocolType)) && leaveOnServer) {
054                message.setFlag(Flag.SEEN, true);
055
056            } else {
057                message.setFlag(Flag.DELETED, true);
058            }
059            return true;
060        } catch (MessagingException e) {
061            return false;
062        }
063    }
064
065}