Author: norman Date: Tue Aug 29 14:02:53 2006 New Revision: 438212 URL: http://svn.apache.org/viewvc?rev=438212&view=rev Log: Add JDBCVirtualUserTableHandler. See JAMES-597
Added: james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JDBCVirtualUserTableHandler.java Modified: james/server/trunk/src/conf/james-smtphandlerchain.xml james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractVirtualUserTableHandler.java james/server/trunk/src/java/org/apache/james/transport/mailets/JDBCVirtualUserTable.java james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java Modified: james/server/trunk/src/conf/james-smtphandlerchain.xml URL: http://svn.apache.org/viewvc/james/server/trunk/src/conf/james-smtphandlerchain.xml?rev=438212&r1=438211&r2=438212&view=diff ============================================================================== --- james/server/trunk/src/conf/james-smtphandlerchain.xml (original) +++ james/server/trunk/src/conf/james-smtphandlerchain.xml Tue Aug 29 14:02:53 2006 @@ -112,12 +112,19 @@ </mapping> </handler> --> + + <!-- IF you want to use the ValidRcptHandler you can set virtual user which will accepted here.--> + <!-- + <handler class="org.apache.james.smtpserver.core.filter.fastfail.JDBCVirtualUserTableHandler" command="RCPT"> + <table> db://maildb/VirtualUserTable </table> + </handler> + --> <!-- If activated all email will get rejected which has no valid user --> <!-- You need to add the recipient to the validRecipient list if you want --> <!-- to accept email for a recipient which not exist on the server --> <!-- - <handler class="org.apache.james.smtpserver.core.filter.fastfail.ValidRcptHandler" command="MAIL"> + <handler class="org.apache.james.smtpserver.core.filter.fastfail.ValidRcptHandler" command="RCPT"> <validRecipients> </validRecipients> <validDomains> </validDomains> <validRegexPattern> </validRegexPattern> Modified: james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractVirtualUserTableHandler.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractVirtualUserTableHandler.java?rev=438212&r1=438211&r2=438212&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractVirtualUserTableHandler.java (original) +++ james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractVirtualUserTableHandler.java Tue Aug 29 14:02:53 2006 @@ -56,7 +56,9 @@ // Only non-null mappings are translated if (targetString != null) { if (targetString.startsWith("error:")) { - // No valid mapping + // write back the error + session.writeResponse(targetString.substring("error:".length())); + session.setStopHandlerProcessing(true); return; } else { StringTokenizer tokenizer = new StringTokenizer(targetString, @@ -68,10 +70,10 @@ if (targetAddress.startsWith("regex:")) { try { - targetAddress = VirtualUserTableUtil.regexMap(rcpt, targetAddress); - } catch (MalformedPatternException e) { + targetAddress = VirtualUserTableUtil.regexMap(rcpt, targetAddress); + } catch (MalformedPatternException e) { getLogger().error("Exception during regexMap processing: ", e); - } + } if (targetAddress == null) @@ -118,7 +120,7 @@ * @param recipientsMap the mapping of virtual to real recipients, as * <code>MailAddress</code>es to <code>String</code>s. */ - protected abstract String mapRecipients(MailAddress recipient);; + protected abstract String mapRecipients(MailAddress recipient); public Collection getImplCommands() { Collection c = new ArrayList(); Added: james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JDBCVirtualUserTableHandler.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JDBCVirtualUserTableHandler.java?rev=438212&view=auto ============================================================================== --- james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JDBCVirtualUserTableHandler.java (added) +++ james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JDBCVirtualUserTableHandler.java Tue Aug 29 14:02:53 2006 @@ -0,0 +1,158 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + + + +package org.apache.james.smtpserver.core.filter.fastfail; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.avalon.cornerstone.services.datasources.DataSourceSelector; +import org.apache.avalon.excalibur.datasource.DataSourceComponent; +import org.apache.avalon.framework.activity.Initializable; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; +import org.apache.james.util.JDBCUtil; +import org.apache.james.util.VirtualUserTableUtil; +import org.apache.mailet.MailAddress; + +/** + * + * Handler which check for valid mappings via JDBCVirtualUserTable + */ +public class JDBCVirtualUserTableHandler extends AbstractVirtualUserTableHandler implements Configurable,Serviceable,Initializable{ + + private DataSourceSelector datasources = null; + private DataSourceComponent dataSourceComponent = null; + private String tableName = null; + private String dataSourceName = null; + private String query = VirtualUserTableUtil.QUERY; + + /** + * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration) + */ + public void configure(Configuration arg0) throws ConfigurationException { + Configuration table = arg0.getChild("table",false); + + if (table != null) { + String tableURL = table.getValue(); + + String datasourceName = tableURL.substring(5); + int pos = datasourceName.indexOf("/"); + tableName = datasourceName.substring(pos + 1); + dataSourceName = datasourceName.substring(0, pos); + } else { + throw new ConfigurationException("Table location not specified for JDBCVirtualUserTableHandler"); + } + + Configuration queryConfig = arg0.getChild("query",false); + + if (queryConfig != null) { + query = queryConfig.getValue(); + } + } + + /** + * @see org.apache.avalon.framework.service.Serviceable#service(ServiceManager) + */ + public void service(ServiceManager arg0) throws ServiceException { + datasources = (DataSourceSelector)arg0.lookup(DataSourceSelector.ROLE); + } + + + /** + * @see org.apache.avalon.framework.activity.Initializable#initialize() + */ + public void initialize() throws Exception { + Connection conn = null; + + setDataSourceComponent((DataSourceComponent) datasources.select(dataSourceName)); + + try { + conn = dataSourceComponent.getConnection(); + if (!(theJDBCUtil.tableExists(conn.getMetaData(), tableName))) { + StringBuffer exceptionBuffer = + new StringBuffer(128) + .append("Could not find table '") + .append(tableName) + .append("' in datasource '") + .append(dataSourceName) + .append("'"); + throw new Exception(exceptionBuffer.toString()); + } + } finally { + theJDBCUtil.closeJDBCConnection(conn); + } + } + + public void setDataSourceComponent(DataSourceComponent dataSourceComponent) { + this.dataSourceComponent = dataSourceComponent; + } + + /** + * The JDBCUtil helper class + */ + private final JDBCUtil theJDBCUtil = new JDBCUtil() { + protected void delegatedLog(String logString) { + getLogger().debug("JDBCVirtualUserTable: " + logString); + } + }; + + /** + * @see org.apache.james.smtpserver.core.filter.fastfail.AbstractVirtualUserTableHandler#mapRecipients(MailAddress) + */ + protected String mapRecipients(MailAddress recipient) { + Connection conn = null; + PreparedStatement mappingStmt = null; + try { + conn = dataSourceComponent.getConnection(); + mappingStmt = conn.prepareStatement(query); + + ResultSet mappingRS = null; + try { + mappingStmt.setString(1, recipient.getUser()); + mappingStmt.setString(2, recipient.getHost()); + mappingStmt.setString(3, recipient.getHost()); + mappingRS = mappingStmt.executeQuery(); + if (mappingRS.next()) { + String targetString = mappingRS.getString(1); + return targetString; + } + } finally { + theJDBCUtil.closeJDBCResultSet(mappingRS); + } + + } catch (SQLException sqle) { + getLogger().error("Error accessing database", sqle); + } finally { + theJDBCUtil.closeJDBCStatement(mappingStmt); + theJDBCUtil.closeJDBCConnection(conn); + } + + return null; + } + +} Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/JDBCVirtualUserTable.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/JDBCVirtualUserTable.java?rev=438212&r1=438211&r2=438212&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/transport/mailets/JDBCVirtualUserTable.java (original) +++ james/server/trunk/src/java/org/apache/james/transport/mailets/JDBCVirtualUserTable.java Tue Aug 29 14:02:53 2006 @@ -26,6 +26,7 @@ import org.apache.avalon.framework.service.ServiceManager; import org.apache.james.Constants; import org.apache.james.util.JDBCUtil; +import org.apache.james.util.VirtualUserTableUtil; import org.apache.mailet.MailAddress; import org.apache.mailet.MailetException; @@ -149,7 +150,7 @@ } //Build the query - query = getInitParameter("sqlquery","select VirtualUserTable.target_address from VirtualUserTable, VirtualUserTable as VUTDomains where (VirtualUserTable.user like ? or VirtualUserTable.user like '\\%') and (VirtualUserTable.domain like ? or (VirtualUserTable.domain like '\\%' and VUTDomains.domain like ?)) order by concat(VirtualUserTable.user,'@',VirtualUserTable.domain) desc limit 1"); + query = getInitParameter("sqlquery",VirtualUserTableUtil.QUERY); } catch (MailetException me) { throw me; } catch (Exception e) { Modified: james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java?rev=438212&r1=438211&r2=438212&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java (original) +++ james/server/trunk/src/java/org/apache/james/util/VirtualUserTableUtil.java Tue Aug 29 14:02:53 2006 @@ -39,7 +39,8 @@ private VirtualUserTableUtil() {} - + + public static String QUERY = "select VirtualUserTable.target_address from VirtualUserTable, VirtualUserTable as VUTDomains where (VirtualUserTable.user like ? or VirtualUserTable.user like '\\%') and (VirtualUserTable.domain like ? or (VirtualUserTable.domain like '\\%' and VUTDomains.domain like ?)) order by concat(VirtualUserTable.user,'@',VirtualUserTable.domain) desc limit 1"; /** * Processes regex virtual user mapping * --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]