in server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/WhiteListManager.java [425:539]
private void manageInsertRequest(Mail mail) throws MessagingException {
MailAddress senderMailAddress = mail.getMaybeSender().get();
String senderUser = senderMailAddress.getLocalPart().toLowerCase(Locale.US);
Domain senderHost = senderMailAddress.getDomain();
Connection conn = null;
PreparedStatement selectStmt = null;
PreparedStatement insertStmt = null;
boolean dbUpdated = false;
StringWriter sout = new StringWriter();
PrintWriter out = new PrintWriter(sout, true);
try {
out.println("Answering on behalf of: " + whitelistManagerAddress);
out.println("Inserting in the white list of " + (new MailAddress(senderUser, senderHost)) + " ...");
out.println();
MimeMessage message = mail.getMessage();
Object content = message.getContent();
if (message.getContentType().startsWith("text/plain") && content instanceof String) {
StringTokenizer st = new StringTokenizer((String) content, " \t\n\r\f,;:<>");
while (st.hasMoreTokens()) {
ResultSet selectRS = null;
try {
MailAddress recipientMailAddress;
try {
recipientMailAddress = new MailAddress(st.nextToken());
} catch (javax.mail.internet.ParseException pe) {
continue;
}
String recipientUser = recipientMailAddress.getLocalPart().toLowerCase(Locale.US);
Domain recipientHost = recipientMailAddress.getDomain();
if (getMailetContext().isLocalServer(recipientHost)) {
// not a remote recipient, so skip
continue;
}
if (conn == null) {
conn = datasource.getConnection();
}
if (selectStmt == null) {
selectStmt = conn.prepareStatement(selectByPK);
}
selectStmt.setString(1, senderUser);
selectStmt.setString(2, senderHost.asString());
selectStmt.setString(3, recipientUser);
selectStmt.setString(4, recipientHost.asString());
selectRS = selectStmt.executeQuery();
if (selectRS.next()) {
// This address was already in the list
out.println("Skipped: " + recipientMailAddress);
continue;
}
if (insertStmt == null) {
insertStmt = conn.prepareStatement(insert);
}
insertStmt.setString(1, senderUser);
insertStmt.setString(2, senderHost.asString());
insertStmt.setString(3, recipientUser);
insertStmt.setString(4, recipientHost.asString());
insertStmt.executeUpdate();
dbUpdated = true;
out.println("Inserted: " + recipientMailAddress);
} finally {
theJDBCUtil.closeJDBCResultSet(selectRS);
}
}
if (dbUpdated) {
LOGGER.debug("Insertion request issued by {}", senderMailAddress);
}
// Commit our changes if necessary.
if (conn != null && dbUpdated && !conn.getAutoCommit()) {
conn.commit();
dbUpdated = false;
}
} else {
out.println("The message must be plain - no action");
}
out.println();
out.println("Finished");
sendReplyFromPostmaster(mail, sout.toString());
} catch (SQLException sqle) {
out.println("Error accessing the database");
sendReplyFromPostmaster(mail, sout.toString());
throw new MessagingException("Error accessing the database", sqle);
} catch (IOException ioe) {
out.println("Error getting message content");
sendReplyFromPostmaster(mail, sout.toString());
throw new MessagingException("Error getting message content", ioe);
} finally {
theJDBCUtil.closeJDBCStatement(selectStmt);
theJDBCUtil.closeJDBCStatement(insertStmt);
// Rollback our changes if necessary.
try {
if (conn != null && dbUpdated && !conn.getAutoCommit()) {
conn.rollback();
dbUpdated = false;
}
} catch (Exception e) {
LOGGER.error("Ignored exception upon rollback", e);
}
theJDBCUtil.closeJDBCConnection(conn);
}
}