}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DataManager dataStoreManager = DataManager.getInstance();
try {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = null;
try {
message = new MimeMessage(session, req.getInputStream());
} catch (MessagingException e) {
throw new ServletException(
"Error while retrieving an incoming mail", e);
}
Address[] froms = null;
try {
froms = message.getFrom();
} catch (MessagingException e) {
throw new ServletException(
"Error while retrieving an the 'from' field from the following message: "
+ message, e);
}
InternetAddress from = null;
switch (froms.length) {
case 0: {
/*
* No from address? How are we supposed to check the validity of
* the test?
*
* TODO Check if the email has at least a valid test identifier
* inside. If not, figure out something
*/
throw new ServletException(
"The following message has no 'from' field set: "
+ message);
}
case 1: {
/*
* That's how we like it ;-)
*/
from = (InternetAddress) froms[0];
break;
}
default: {
/*
* Uhm... multiple 'from'? TODO Check for a 'from' we know, and
* assume that. Plus, log a warning.
*/
throw new ServletException(
"The following message has multiple 'from' fields set: "
+ message);
}
}
try {
if (message
.getHeader(TestMessageConstants.SMTP_HEADER_TEST_IDENTIFIER) != null
|| message
.getHeader(TestMessageConstants.SMTP_HEADER_ACCOUNT_IDENTIFIER) != null) {
processTestAnswerInHeaders(dataStoreManager, message);
} else {
processTestAnswerInMessageBody(dataStoreManager, message,
from);
}
} catch (DataManagerException e) {
throw new ServletException(e.getMessage());
} catch (Exception e) {
UnprocessedEMail em = dataStoreManager
.storeUnprocessedEMail(message);
LOGGER.log(Level.SEVERE,
"Incoming message could not be processed, saved as UnprocessedEMail "
+ KeyFactory.keyToString(em.getIdentifier()), e);
}
} finally {
dataStoreManager.close();
}
}