Example usage for javax.mail Flags contains

List of usage examples for javax.mail Flags contains

Introduction

In this page you can find the example usage for javax.mail Flags contains.

Prototype

public boolean contains(Flags f) 

Source Link

Document

Check whether all the flags in the specified Flags object are present in this Flags object.

Usage

From source file:org.apache.james.protocols.imap.utils.DecoderUtilsTest.java

@Test
public void testExtensionFlag() throws Exception {
    Flags flags = new Flags();
    DecoderUtils.setFlag(EXTENSION_FLAG, flags);
    assertTrue("Extension flags should be added", flags.contains(EXTENSION_FLAG));
}

From source file:org.nuxeo.ecm.platform.mail.utils.MailCoreHelper.java

protected static void doCheckMail(DocumentModel currentMailFolder, CoreSession coreSession)
        throws MessagingException {
    String email = (String) currentMailFolder.getPropertyValue(EMAIL_PROPERTY_NAME);
    String password = (String) currentMailFolder.getPropertyValue(PASSWORD_PROPERTY_NAME);
    if (!StringUtils.isEmpty(email) && !StringUtils.isEmpty(password)) {
        mailService = getMailService();//www  .ja  v  a 2s .co m

        MessageActionPipe pipe = mailService.getPipe(PIPE_NAME);

        Visitor visitor = new Visitor(pipe);
        Thread.currentThread().setContextClassLoader(Framework.class.getClassLoader());

        // initialize context
        ExecutionContext initialExecutionContext = new ExecutionContext();

        initialExecutionContext.put(MIMETYPE_SERVICE_KEY, getMimeService());

        initialExecutionContext.put(PARENT_PATH_KEY, currentMailFolder.getPathAsString());

        initialExecutionContext.put(CORE_SESSION_KEY, coreSession);

        initialExecutionContext.put(LEAVE_ON_SERVER_KEY, Boolean.TRUE); // TODO should be an attribute in 'protocol'
                                                                        // schema

        Folder rootFolder = null;
        Store store = null;
        try {
            String protocolType = (String) currentMailFolder.getPropertyValue(PROTOCOL_TYPE_PROPERTY_NAME);
            initialExecutionContext.put(PROTOCOL_TYPE_KEY, protocolType);
            // log.debug(PROTOCOL_TYPE_KEY + ": " + (String) initialExecutionContext.get(PROTOCOL_TYPE_KEY));

            String host = (String) currentMailFolder.getPropertyValue(HOST_PROPERTY_NAME);
            String port = (String) currentMailFolder.getPropertyValue(PORT_PROPERTY_NAME);
            Boolean socketFactoryFallback = (Boolean) currentMailFolder
                    .getPropertyValue(SOCKET_FACTORY_FALLBACK_PROPERTY_NAME);
            String socketFactoryPort = (String) currentMailFolder
                    .getPropertyValue(SOCKET_FACTORY_PORT_PROPERTY_NAME);
            Boolean starttlsEnable = (Boolean) currentMailFolder
                    .getPropertyValue(STARTTLS_ENABLE_PROPERTY_NAME);
            String sslProtocols = (String) currentMailFolder.getPropertyValue(SSL_PROTOCOLS_PROPERTY_NAME);
            Long emailsLimit = (Long) currentMailFolder.getPropertyValue(EMAILS_LIMIT_PROPERTY_NAME);
            long emailsLimitLongValue = emailsLimit == null ? EMAILS_LIMIT_DEFAULT : emailsLimit.longValue();

            String imapDebug = Framework.getProperty(IMAP_DEBUG, "false");

            Properties properties = new Properties();
            properties.put("mail.store.protocol", protocolType);
            // properties.put("mail.host", host);
            // Is IMAP connection
            if (IMAP.equals(protocolType)) {
                properties.put("mail.imap.host", host);
                properties.put("mail.imap.port", port);
                properties.put("mail.imap.starttls.enable", starttlsEnable.toString());
                properties.put("mail.imap.debug", imapDebug);
                properties.put("mail.imap.partialfetch", "false");
            } else if (IMAPS.equals(protocolType)) {
                properties.put("mail.imaps.host", host);
                properties.put("mail.imaps.port", port);
                properties.put("mail.imaps.starttls.enable", starttlsEnable.toString());
                properties.put("mail.imaps.ssl.protocols", sslProtocols);
                properties.put("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                properties.put("mail.imaps.socketFactory.fallback", socketFactoryFallback.toString());
                properties.put("mail.imaps.socketFactory.port", socketFactoryPort);
                properties.put("mail.imap.partialfetch", "false");
                properties.put("mail.imaps.partialfetch", "false");
            } else if (POP3S.equals(protocolType)) {
                properties.put("mail.pop3s.host", host);
                properties.put("mail.pop3s.port", port);
                properties.put("mail.pop3s.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                properties.put("mail.pop3s.socketFactory.fallback", socketFactoryFallback.toString());
                properties.put("mail.pop3s.socketFactory.port", socketFactoryPort);
                properties.put("mail.pop3s.ssl.protocols", sslProtocols);
            } else {
                // Is POP3 connection
                properties.put("mail.pop3.host", host);
                properties.put("mail.pop3.port", port);
            }

            properties.put("user", email);
            properties.put("password", password);

            Session session = Session.getInstance(properties);

            store = session.getStore();
            store.connect(email, password);

            String folderName = INBOX; // TODO should be an attribute in 'protocol' schema
            rootFolder = store.getFolder(folderName);

            // need RW access to update message flags
            rootFolder.open(Folder.READ_WRITE);

            Message[] allMessages = rootFolder.getMessages();
            // VDU
            log.debug("nbr of messages in folder:" + allMessages.length);

            FetchProfile fetchProfile = new FetchProfile();
            fetchProfile.add(FetchProfile.Item.FLAGS);
            fetchProfile.add(FetchProfile.Item.ENVELOPE);
            fetchProfile.add(FetchProfile.Item.CONTENT_INFO);
            fetchProfile.add("Message-ID");
            fetchProfile.add("Content-Transfer-Encoding");

            rootFolder.fetch(allMessages, fetchProfile);

            if (rootFolder instanceof IMAPFolder) {
                // ((IMAPFolder)rootFolder).doCommand(FetchProfile)
            }

            List<Message> unreadMessagesList = new ArrayList<Message>();
            for (Message message : allMessages) {
                Flags flags = message.getFlags();
                int unreadMessagesListSize = unreadMessagesList.size();
                if (flags != null && !flags.contains(Flag.SEEN)
                        && unreadMessagesListSize < emailsLimitLongValue) {
                    unreadMessagesList.add(message);
                    if (unreadMessagesListSize == emailsLimitLongValue - 1) {
                        break;
                    }
                }
            }

            Message[] unreadMessagesArray = unreadMessagesList.toArray(new Message[unreadMessagesList.size()]);

            // perform email import
            visitor.visit(unreadMessagesArray, initialExecutionContext);

            // perform flag update globally
            Flags flags = new Flags();
            flags.add(Flag.SEEN);

            boolean leaveOnServer = (Boolean) initialExecutionContext.get(LEAVE_ON_SERVER_KEY);
            if ((IMAP.equals(protocolType) || IMAPS.equals(protocolType)) && leaveOnServer) {
                flags.add(Flag.SEEN);
            } else {
                flags.add(Flag.DELETED);
            }
            rootFolder.setFlags(unreadMessagesArray, flags, true);

        } finally {
            if (rootFolder != null && rootFolder.isOpen()) {
                rootFolder.close(true);
            }
            if (store != null) {
                store.close();
            }
        }
    }
}

From source file:org.springframework.integration.mail.AbstractMailReceiver.java

private void setMessageFlags(Message[] filteredMessages) throws MessagingException {
    boolean recentFlagSupported = false;

    Flags flags = this.getFolder().getPermanentFlags();

    if (flags != null) {
        recentFlagSupported = flags.contains(Flags.Flag.RECENT);
    }/*w ww.j  a v a2  s.  c  om*/
    for (Message message : filteredMessages) {
        if (!recentFlagSupported) {
            if (flags != null && flags.contains(Flags.Flag.USER)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("USER flags are supported by this mail server. Flagging message with '"
                            + SI_USER_FLAG + "' user flag");
                }
                Flags siFlags = new Flags();
                siFlags.add(SI_USER_FLAG);
                message.setFlags(siFlags, true);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "USER flags are not supported by this mail server. Flagging message with system flag");
                }
                message.setFlag(Flags.Flag.FLAGGED, true);
            }
        }
        this.setAdditionalFlags(message);
    }
}

From source file:org.springframework.ws.transport.mail.monitor.AbstractMonitoringStrategy.java

/**
 * Retrieves new messages from the given folder. This implementation creates a {@link SearchTerm} that searches for
 * all messages in the folder that are {@link javax.mail.Flags.Flag#RECENT RECENT}, not {@link
 * javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term
 * is used to {@link Folder#search(SearchTerm) search} for new messages.
 *
 * @param folder the folder to retrieve new messages from
 * @return the new messages/*from ww  w .j a  v a  2s.c  o  m*/
 * @throws MessagingException in case of JavaMail errors
 */
protected Message[] searchForNewMessages(Folder folder) throws MessagingException {
    if (!folder.isOpen()) {
        return new Message[0];
    }
    Flags supportedFlags = folder.getPermanentFlags();
    SearchTerm searchTerm = null;
    if (supportedFlags != null) {
        if (supportedFlags.contains(Flags.Flag.RECENT)) {
            searchTerm = new FlagTerm(new Flags(Flags.Flag.RECENT), true);
        }
        if (supportedFlags.contains(Flags.Flag.ANSWERED)) {
            FlagTerm answeredTerm = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false);
            if (searchTerm == null) {
                searchTerm = answeredTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, answeredTerm);
            }
        }
        if (supportedFlags.contains(Flags.Flag.DELETED)) {
            FlagTerm deletedTerm = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
            if (searchTerm == null) {
                searchTerm = deletedTerm;
            } else {
                searchTerm = new AndTerm(searchTerm, deletedTerm);
            }
        }
    }
    return searchTerm != null ? folder.search(searchTerm) : folder.getMessages();
}