⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whitelistmanager.java

📁 java mail,java mailjava mailjava mailjava mail
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**************************************************************** * 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.transport.mailets;import org.apache.mailet.*;import org.apache.mailet.dates.RFC822DateFormat;import org.apache.avalon.cornerstone.services.datasources.*;import org.apache.avalon.excalibur.datasource.*;import org.apache.avalon.framework.service.*;import org.apache.james.*;import org.apache.james.core.*;import org.apache.james.services.*;import org.apache.james.util.*;import javax.mail.*;import javax.mail.internet.*;import java.sql.*;import java.util.*;import java.text.*;import java.io.*;/** <P>Manages for each local user a "white list" of remote addresses whose messages * should never be blocked as spam.</P> * <P>The normal behaviour is to check, for a local sender, if a remote recipient * is already in the list: if not, it will be automatically inserted. * This is under the interpretation that if a local sender <I>X</I> sends a message to a * remote recipient <I>Y</I>, then later on if a message is sent by <I>Y</I> to <I>X</I> it should be * considered always valid and never blocked; hence <I>Y</I> should be in the white list * of <I>X</I>.</P> * <P>Another mode of operations is when a local sender sends a message to <I>whitelistManagerAddress</I> * with one of three specific values in the subject, to * (i) send back a message displaying a list of the addresses in his own list; * (ii) insert some new addresses in his own list; * (iii) remove some addresses from his own list. * In all this cases the message will be ghosted and the postmaster will reply * to the sender.</P> * <P> The sender name is always converted to its primary name (handling aliases).</P> * <P>Sample configuration:</P> * <PRE><CODE> * &lt;mailet match="SMTPAuthSuccessful" class="WhiteListManager"&gt; *   &lt;repositoryPath&gt; db://maildb &lt;/repositoryPath&gt; *   &lt;!-- *     If true automatically inserts the local sender to remote recipients entries in the whitelist (default is false). *   --&gt; *   &lt;automaticInsert&gt;true&lt;/automaticInsert&gt; *   &lt;!-- *     Set this to an email address of the "whitelist manager" to send commands to (default is null). *   --&gt; *   &lt;whitelistManagerAddress&gt;whitelist.manager@xxx.yyy&lt;/whitelistManagerAddress&gt; *   &lt;!-- *     Set this to a unique text that you can use (by sending a message to the "whitelist manager" above) *     to tell the mailet to send back the contents of the white list (default is null). *   --&gt; *   &lt;displayFlag&gt;display whitelist&lt;/displayFlag&gt; *   &lt;!-- *     Set this to a unique text that you can use (by sending a message to the "whitelist manager" above) *     to tell the mailet to insert some new remote recipients to the white list (default is null). *   --&gt; *   &lt;insertFlag&gt;insert whitelist&lt;/insertFlag&gt; *   &lt;!-- *     Set this to a unique text that you can use (by sending a message to the "whitelist manager" above) *     to tell the mailet to remove some remote recipients from the white list (default is null). *   --&gt; *   &lt;removeFlag&gt;remove whitelist&lt;/removeFlag&gt; * &lt;/mailet&gt; * </CODE></PRE> * * @see org.apache.james.transport.matchers.IsInWhiteList * @version SVN $Revision: $ $Date: $ * @since 2.3.0 */public class WhiteListManager extends GenericMailet {        private boolean automaticInsert;    private String displayFlag;    private String insertFlag;    private String removeFlag;    private MailAddress whitelistManagerAddress;        private String selectByPK;    private String selectBySender;    private String insert;    private String deleteByPK;        /** The date format object used to generate RFC 822 compliant date headers. */    private RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();    private DataSourceComponent datasource;       /** The store containing the local user repository. */    private UsersStore usersStore;    /** The user repository for this mail server.  Contains all the users with inboxes     * on this server.     */    private UsersRepository localusers;    /**     * The JDBCUtil helper class     */    private final JDBCUtil theJDBCUtil = new JDBCUtil() {        protected void delegatedLog(String logString) {            log("WhiteListManager: " + logString);        }    };        /**     * Contains all of the sql strings for this component.     */    private SqlResources sqlQueries = new SqlResources();    /**     * Holds value of property sqlFile.     */    private File sqlFile;     /**     * Holds value of property sqlParameters.     */    private Map sqlParameters = new HashMap();    /**     * Getter for property sqlParameters.     * @return Value of property sqlParameters.     */    private Map getSqlParameters() {        return this.sqlParameters;    }    /**     * Setter for property sqlParameters.     * @param sqlParameters New value of property sqlParameters.     */    private void setSqlParameters(Map sqlParameters) {        this.sqlParameters = sqlParameters;    }    /** Initializes the mailet.     */    public void init() throws MessagingException {        automaticInsert = new Boolean(getInitParameter("automaticInsert")).booleanValue();        log("automaticInsert: " + automaticInsert);        displayFlag = getInitParameter("displayFlag");        insertFlag = getInitParameter("insertFlag");        removeFlag = getInitParameter("removeFlag");                String whitelistManagerAddressString = getInitParameter("whitelistManagerAddress");        if (whitelistManagerAddressString != null) {            whitelistManagerAddressString = whitelistManagerAddressString.trim();            log("whitelistManagerAddress: " + whitelistManagerAddressString);            try {                whitelistManagerAddress = new MailAddress(whitelistManagerAddressString);            }            catch (javax.mail.internet.ParseException pe) {                throw new MessagingException("Bad whitelistManagerAddress", pe);            }                        if (displayFlag != null) {                displayFlag = displayFlag.trim();                log("displayFlag: " + displayFlag);            }            else {                log("displayFlag is null");            }            if (insertFlag != null) {                insertFlag = insertFlag.trim();                log("insertFlag: " + insertFlag);            }            else {                log("insertFlag is null");            }            if (removeFlag != null) {                removeFlag = removeFlag.trim();                log("removeFlag: " + removeFlag);            }            else {                log("removeFlag is null");            }        }        else {            log("whitelistManagerAddress is null; will ignore commands");        }                String repositoryPath = getInitParameter("repositoryPath");        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);        selectBySender = sqlQueries.getSqlString("selectBySender", true);        insert = sqlQueries.getSqlString("insert", true);        deleteByPK = sqlQueries.getSqlString("deleteByPK", true);    }        /** Services the mailet.     */        public void service(Mail mail) throws MessagingException {                // check if it's a local sender        MailAddress senderMailAddress = mail.getSender();        if (senderMailAddress == null) {            return;        }        String senderUser = senderMailAddress.getUser();        String senderHost = senderMailAddress.getHost();        if (   !getMailetContext().isLocalServer(senderHost)            || !getMailetContext().isLocalUser(senderUser)) {            // not a local sender, so return            return;        }                Collection recipients = mail.getRecipients();                if (recipients.size() == 1        && whitelistManagerAddress != null        && whitelistManagerAddress.equals(recipients.toArray()[0])) {                        mail.setState(Mail.GHOST);                    String subject = mail.getMessage().getSubject();            if (displayFlag != null && displayFlag.equals(subject)) {                manageDisplayRequest(mail);            }            else if (insertFlag != null && insertFlag.equals(subject)) {                manageInsertRequest(mail);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -