IsInWhiteListpublic class IsInWhiteList extends GenericMatcher Matches recipients having the mail sender in the recipient's private whitelist .
The recipient name is always converted to its primary name (handling aliases).
Configuration string: The database name containing the white list table.
Example:
<mailet match="IsInWhiteList=db://maildb" class="ToProcessor">
<processor> transport </processor>
</mailet>
|
Fields Summary |
---|
private String | selectByPK | private DataSourceComponent | datasource | private UsersStore | usersStoreThe store containing the local user repository. | private UsersRepository | localusersThe user repository for this mail server. Contains all the users with inboxes
on this server. | private final JDBCUtil | theJDBCUtilThe JDBCUtil helper class | private SqlResources | sqlQueriesContains all of the sql strings for this component. | private File | sqlFileHolds value of property sqlFile. | private Map | sqlParametersHolds value of property sqlParameters. |
Methods Summary |
---|
private java.lang.String | getPrimaryName(java.lang.String originalUsername)Gets the main name of a local customer, handling alias
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;
| private java.util.Map | getSqlParameters()Getter for property sqlParameters.
return this.sqlParameters;
| public void | init()
String repositoryPath = null;
StringTokenizer st = new StringTokenizer(getCondition(), ", \t", false);
if (st.hasMoreTokens()) {
repositoryPath = st.nextToken().trim();
}
if (repositoryPath != null) {
log("repositoryPath: " + repositoryPath);
}
else {
throw new MessagingException("repositoryPath is null");
}
ServiceManager serviceManager = (ServiceManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
try {
// Get the DataSourceSelector block
DataSourceSelector datasources = (DataSourceSelector) serviceManager.lookup(DataSourceSelector.ROLE);
// Get the data-source required.
int stindex = repositoryPath.indexOf("://") + 3;
String datasourceName = repositoryPath.substring(stindex);
datasource = (DataSourceComponent) datasources.select(datasourceName);
} catch (Exception e) {
throw new MessagingException("Can't get datasource", e);
}
try {
// Get the UsersRepository
usersStore = (UsersStore)serviceManager.lookup(UsersStore.ROLE);
localusers = (UsersRepository)usersStore.getRepository("LocalUsers");
} catch (Exception e) {
throw new MessagingException("Can't get the local users repository", e);
}
try {
initSqlQueries(datasource.getConnection(), getMailetContext());
} catch (Exception e) {
throw new MessagingException("Exception initializing queries", e);
}
selectByPK = sqlQueries.getSqlString("selectByPK", true);
| public void | initSqlQueries(java.sql.Connection conn, org.apache.mailet.MailetContext mailetContext)Initializes the sql query environment from the SqlResources file.
Will look for conf/sqlResources.xml.
Will not create the database resources, if missing
(this task is done, if needed, in the {@link WhiteListManager}
initialization routine).
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());
} finally {
theJDBCUtil.closeJDBCConnection(conn);
}
| public java.util.Collection | match(Mail mail)
// check if it's a local sender
MailAddress senderMailAddress = mail.getSender();
if (senderMailAddress == null) {
return null;
}
String senderUser = senderMailAddress.getUser();
String senderHost = senderMailAddress.getHost();
if ( getMailetContext().isLocalServer(senderHost)
&& getMailetContext().isLocalUser(senderUser)) {
// is a local sender, so return
return null;
}
senderUser = senderUser.toLowerCase(Locale.US);
senderHost = senderHost.toLowerCase(Locale.US);
Collection recipients = mail.getRecipients();
Collection inWhiteList = new java.util.HashSet();
Connection conn = null;
PreparedStatement selectStmt = null;
ResultSet selectRS = null;
try {
for (Iterator i = recipients.iterator(); i.hasNext(); ) {
try {
MailAddress recipientMailAddress = (MailAddress)i.next();
String recipientUser = recipientMailAddress.getUser().toLowerCase(Locale.US);
String recipientHost = recipientMailAddress.getHost().toLowerCase(Locale.US);
if (!getMailetContext().isLocalServer(recipientHost)) {
// not a local recipient, so skip
continue;
}
recipientUser = getPrimaryName(recipientUser);
if (conn == null) {
conn = datasource.getConnection();
}
if (selectStmt == null) {
selectStmt = conn.prepareStatement(selectByPK);
}
selectStmt.setString(1, recipientUser);
selectStmt.setString(2, recipientHost);
selectStmt.setString(3, senderUser);
selectStmt.setString(4, senderHost);
selectRS = selectStmt.executeQuery();
if (selectRS.next()) {
//This address was already in the list
inWhiteList.add(recipientMailAddress);
}
} finally {
theJDBCUtil.closeJDBCResultSet(selectRS);
}
}
return inWhiteList;
} catch (SQLException sqle) {
log("Error accessing database", sqle);
throw new MessagingException("Exception thrown", sqle);
} finally {
theJDBCUtil.closeJDBCStatement(selectStmt);
theJDBCUtil.closeJDBCConnection(conn);
}
| private void | setSqlParameters(java.util.Map sqlParameters)Setter for property sqlParameters.
this.sqlParameters = sqlParameters;
|
|