Example usage for org.springframework.integration.mail MailReceiver receive

List of usage examples for org.springframework.integration.mail MailReceiver receive

Introduction

In this page you can find the example usage for org.springframework.integration.mail MailReceiver receive.

Prototype

Object[] receive() throws javax.mail.MessagingException;

Source Link

Usage

From source file:io.lavagna.service.MailTicketService.java

public void checkNew() {
    List<ProjectMailTicketConfig> entries = mailTicketRepository.findAll();

    for (ProjectMailTicketConfig entry : entries) {
        if (entry.getEntries().size() == 0 || !entry.getEnabled()) {
            continue;
        }/*www .ja va  2 s.  c  o  m*/

        MailReceiver receiver = entry.getConfig().getInboundProtocol().startsWith("pop3")
                ? getPop3MailReceiver(entry.getConfig())
                : getImapMailReceiver(entry.getConfig());

        try {
            Date updateLastChecked = entry.getLastChecked();

            Object[] messages = receiver.receive();
            LOG.debug("found {} messages", messages.length);

            for (int i = 0; i < messages.length; i++) {
                MimeMessage message = (MimeMessage) messages[i];
                if (!message.getReceivedDate().after(entry.getLastChecked())) {
                    continue;
                } else {
                    updateLastChecked = message.getReceivedDate().after(updateLastChecked)
                            ? message.getReceivedDate()
                            : updateLastChecked;
                }

                String deliveredTo = getDeliveredTo(message);

                for (ProjectMailTicket ticketConfig : entry.getEntries()) {
                    if (ticketConfig.getEnabled() && ticketConfig.getAlias().equals(deliveredTo)) {
                        String from = getFrom(message);
                        String name = getName(message);
                        Matcher m = CARD_SHORT_NAME.matcher(message.getSubject());

                        if (!m.find() || (m.find() && !cardService.existCardWith(m.group("shortname"),
                                Integer.parseInt(m.group("sequence"))))) {
                            try {
                                ImmutablePair<Card, User> cardAndUser = createCard(message.getSubject(),
                                        getTextFromMessage(message), from, ticketConfig.getColumnId());

                                notify(cardAndUser.getLeft(), entry, ticketConfig, cardAndUser.getRight(), from,
                                        name);
                            } catch (IOException | MessagingException e) {
                                LOG.error("failed to parse message body", e);
                            }
                        }
                    }
                }
            }

            mailTicketRepository.updateLastChecked(entry.getId(), updateLastChecked);
        } catch (MessagingException e) {
            LOG.error("could not retrieve messages for ticket mail config id: {}", entry.getId());
        }
    }
}