Java tutorial
/* * Copyright (c) 2015, OpinionLab * * This file is part of The Wall of Awesome. * * The Wall of Awesome is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The Wall of Awesome is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with The Wall of Awesome. If not, see <http://www.gnu.org/licenses/>. */ package com.opinionlab.woa; import org.apache.commons.mail.util.MimeMessageParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.regex.Pattern; import static java.lang.String.format; import static java.time.ZoneId.systemDefault; public class Awesome { private static final Logger LOGGER = LoggerFactory.getLogger(Awesome.class); private static final Pattern ID_PTN = Pattern.compile("[^a-zA-Z0-9]"); private static final Pattern SIG_PTN = Pattern.compile("^(.*?)\n--.*$", Pattern.DOTALL); private static final Pattern FWD_PTN = Pattern.compile("^(.*?)\nFrom: .*$", Pattern.DOTALL); private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("MMMM dd"); private final String messageID; private final String id; private final Date receivedDate; private final String monthDay; private final Person to; private final Person from; private final String subject; private final String comment; public Awesome(Message message) { try { this.messageID = ((MimeMessage) message).getMessageID(); this.id = ID_PTN.matcher(this.messageID).replaceAll("_"); this.receivedDate = message.getReceivedDate(); this.monthDay = DATE_FORMATTER .format(LocalDateTime.ofInstant(receivedDate.toInstant(), systemDefault())); this.to = new Person((InternetAddress) //todo Do this smarter message.getRecipients(Message.RecipientType.TO)[0]); this.from = new Person((InternetAddress) message.getFrom()[0]); this.subject = message.getSubject(); final MimeMessageParser parser = new MimeMessageParser((MimeMessage) message).parse(); if (!parser.hasPlainContent()) { LOGGER.error(format("Unable to parse message '%s'; has no plain content", this.messageID)); this.comment = "Unknown content."; } else { this.comment = parseContent(parser.getPlainContent()); } } catch (Throwable t) { throw new IllegalArgumentException(t); } } @Override public String toString() { return format("Awesome{monthDay='%s', to=%s, from=%s, subject='%s', content='%s'}", monthDay, to, from, subject, comment); } public String getMessageID() { return messageID; } public String getId() { return id; } public Date getReceivedDate() { return receivedDate; } public String getMonthDay() { return monthDay; } public Person getTo() { return to; } public Person getFrom() { return from; } public String getSubject() { return subject; } public String getComment() { return comment; } public static String parseContent(String content) { final String phase1 = FWD_PTN.matcher(content).replaceAll("$1"); final String phase2 = SIG_PTN.matcher(phase1).replaceAll("$1"); return phase2.trim(); } public static void main(String[] args) throws MessagingException { AwesomeImap.fetchAwesome().forEach(System.out::println); } }