📄 whitelistmanager.java
字号:
catch (Exception e) {} theJDBCUtil.closeJDBCConnection(conn); } } /** Manages a remove request. */ private void manageRemoveRequest(Mail mail) throws MessagingException { MailAddress senderMailAddress = mail.getSender(); String senderUser = senderMailAddress.getUser().toLowerCase(Locale.US); String senderHost = senderMailAddress.getHost().toLowerCase(Locale.US); senderUser = getPrimaryName(senderUser); Connection conn = null; PreparedStatement selectStmt = null; PreparedStatement deleteStmt = null; boolean dbUpdated = false; StringWriter sout = new StringWriter(); PrintWriter out = new PrintWriter(sout, true); try { out.println("Answering on behalf of: " + whitelistManagerAddress); out.println("Removing from 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.getUser().toLowerCase(Locale.US); String recipientHost = recipientMailAddress.getHost().toLowerCase(Locale.US); 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); selectStmt.setString(3, recipientUser); selectStmt.setString(4, recipientHost); selectRS = selectStmt.executeQuery(); if (!selectRS.next()) { //This address was not in the list out.println("Skipped: " + recipientMailAddress); continue; } if (deleteStmt == null) { deleteStmt = conn.prepareStatement(deleteByPK); } deleteStmt.setString(1, senderUser); deleteStmt.setString(2, senderHost); deleteStmt.setString(3, recipientUser); deleteStmt.setString(4, recipientHost); deleteStmt.executeUpdate(); dbUpdated = true; out.println("Removed: " + recipientMailAddress); } finally { theJDBCUtil.closeJDBCResultSet(selectRS); } } if (dbUpdated) { log("Removal 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(deleteStmt); //Rollback our changes if necessary. try { if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.rollback() ; dbUpdated = false; } } catch (Exception e) {} theJDBCUtil.closeJDBCConnection(conn); } } private void sendReplyFromPostmaster(Mail mail, String stringContent) throws MessagingException { try { MailAddress notifier = getMailetContext().getPostmaster(); MailAddress senderMailAddress = mail.getSender(); MimeMessage message = mail.getMessage(); //Create the reply message MimeMessage reply = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null)); //Create the list of recipients in the Address[] format InternetAddress[] rcptAddr = new InternetAddress[1]; rcptAddr[0] = senderMailAddress.toInternetAddress(); reply.setRecipients(Message.RecipientType.TO, rcptAddr); //Set the sender... reply.setFrom(notifier.toInternetAddress()); //Create the message body MimeMultipart multipart = new MimeMultipart(); //Add message as the first mime body part MimeBodyPart part = new MimeBodyPart(); part.setContent(stringContent, "text/plain"); part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); multipart.addBodyPart(part); reply.setContent(multipart); reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); //Create the list of recipients in our MailAddress format Set recipients = new HashSet(); recipients.add(senderMailAddress); //Set additional headers if (reply.getHeader(RFC2822Headers.DATE)==null){ reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new java.util.Date())); } String subject = message.getSubject(); if (subject == null) { subject = ""; } if (subject.indexOf("Re:") == 0){ reply.setSubject(subject); } else { reply.setSubject("Re:" + subject); } reply.setHeader(RFC2822Headers.IN_REPLY_TO, message.getMessageID()); //Send it off... getMailetContext().sendMail(notifier, recipients, reply); } catch (Exception e) { log("Exception found sending reply", e); } } /** Gets the main name of a local customer, handling alias */ private String getPrimaryName(String originalUsername) { String username; try { username = localusers.getRealName(originalUsername); JamesUser user = (JamesUser) localusers.getUserByName(username); if (user.getAliasing()) { username = user.getAlias(); } } catch (Exception e) { username = originalUsername; } return username; } /** * Initializes the sql query environment from the SqlResources file. * Will look for conf/sqlResources.xml. * @param conn The connection for accessing the database * @param mailetContext The current mailet context, * for finding the conf/sqlResources.xml file * @throws Exception If any error occurs */ public void initSqlQueries(Connection conn, org.apache.mailet.MailetContext mailetContext) throws Exception { try { if (conn.getAutoCommit()) { conn.setAutoCommit(false); } this.sqlFile = new File((String) mailetContext.getAttribute("confDir"), "sqlResources.xml").getCanonicalFile(); sqlQueries.init(this.sqlFile, "WhiteList" , conn, getSqlParameters()); checkTables(conn); } finally { theJDBCUtil.closeJDBCConnection(conn); } } private void checkTables(Connection conn) throws SQLException { DatabaseMetaData dbMetaData = conn.getMetaData(); // Need to ask in the case that identifiers are stored, ask the DatabaseMetaInfo. // Try UPPER, lower, and MixedCase, to see if the table is there. boolean dbUpdated = false; dbUpdated = createTable(conn, "whiteListTableName", "createWhiteListTable"); //Commit our changes if necessary. if (conn != null && dbUpdated && !conn.getAutoCommit()) { conn.commit(); dbUpdated = false; } } private boolean createTable(Connection conn, String tableNameSqlStringName, String createSqlStringName) throws SQLException { String tableName = sqlQueries.getSqlString(tableNameSqlStringName, true); DatabaseMetaData dbMetaData = conn.getMetaData(); // Try UPPER, lower, and MixedCase, to see if the table is there. if (theJDBCUtil.tableExists(dbMetaData, tableName)) { return false; } PreparedStatement createStatement = null; try { createStatement = conn.prepareStatement(sqlQueries.getSqlString(createSqlStringName, true)); createStatement.execute(); StringBuffer logBuffer = null; logBuffer = new StringBuffer(64) .append("Created table '") .append(tableName) .append("' using sqlResources string '") .append(createSqlStringName) .append("'."); log(logBuffer.toString()); } finally { theJDBCUtil.closeJDBCStatement(createStatement); } return true; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -