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

📄 commandlistservprocessor.java

📁 java mail,java mailjava mailjava mailjava mail
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * return true if this is ok, false otherwise     * Check if the X-been-there header is set to the listserv's name     * (the address).  If it has, this means it's a message from this     * listserv that's getting bounced back, so we need to swallow it     *     * @param listservAddr     * @param mail     * @return true if this message has already bounced, false otherwse     * @throws MessagingException     */    protected boolean checkBeenThere(MailAddress listservAddr, Mail mail) throws MessagingException {        if (listservAddr.equals(mail.getMessage().getHeader("X-been-there"))) {            return false;        }        return true;    }    /**     * Returns true if this is ok to send to the list     * @param mail     * @return true if this message is ok, false otherwise     * @throws IOException     * @throws MessagingException     */    protected boolean checkAnnouncements(Mail mail) throws IOException, MessagingException {        if (!attachmentsAllowed && mail.getMessage().getContent() instanceof MimeMultipart) {            Properties standardProperties = getCommandListservManager().getStandardProperties();            getCommandListservManager().onError(mail,                    xmlResources.getString("invalid.mail.subject", standardProperties),                    xmlResources.getString("error.attachments", standardProperties));            return false;        }        return true;    }    /**     * Returns true if this user is ok to send to the list     *     * @param members     * @param mail     * @return true if this message is ok, false otherwise     * @throws MessagingException     */    protected boolean checkMembers(Collection members, Mail mail) throws MessagingException {        if (membersOnly && !members.contains(mail.getSender())) {            Properties standardProperties = getCommandListservManager().getStandardProperties();            getCommandListservManager().onError(mail,                    xmlResources.getString("invalid.mail.subject", standardProperties),                    xmlResources.getString("error.membersonly", standardProperties));            return false;        }        return true;    }    public Collection getMembers() throws ParseException {        Collection reply = new ArrayList();        for (Iterator it = usersRepository.list(); it.hasNext();) {            String member = it.next().toString();            try {                reply.add(new MailAddress(member));            } catch (Exception e) {                // Handle an invalid subscriber address by logging it and                // proceeding to the next member.                StringBuffer logBuffer =                        new StringBuffer(1024)                        .append("Invalid subscriber address: ")                        .append(member)                        .append(" caused: ")                        .append(e.getMessage());                log(logBuffer.toString());            }        }        return reply;    }    /**     * Get a configuration value     * @param attrName     * @param defValue     * @return the value if found, defValue otherwise     */    protected boolean getBoolean(String attrName, boolean defValue) {        boolean value = defValue;        try {            value = new Boolean(getInitParameter(attrName)).booleanValue();        } catch (Exception e) {            // Ignore any exceptions, default to false        }        return value;    }    /**     * Get a configuration value     * @param attrName     * @param defValue     * @return the attrValue if found, defValue otherwise     */    protected String getString(String attrName, String defValue) {        String value = defValue;        try {            value = getInitParameter(attrName);        } catch (Exception e) {            // Ignore any exceptions, default to false        }        return value;    }    /**     * initialize the resources     * @throws Exception     */    protected void initializeResources() throws Exception {        xmlResources = getCommandListservManager().initXMLResources(new String[]{"List Manager"})[0];    }    /**     * Fetch the repository of users     */    protected void initUsersRepository() throws Exception {        ServiceManager compMgr = (ServiceManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);        UsersStore usersStore = (UsersStore) compMgr.lookup(UsersStore.ROLE);        String repName = getInitParameter("repositoryName");        usersRepository = usersStore.getRepository(repName);        if (usersRepository == null) throw new Exception("Invalid user repository: " + repName);    }    /**     * <p>This takes the subject string and reduces (normailzes) it.     * Multiple "Re:" entries are reduced to one, and capitalized.  The     * prefix is always moved/placed at the beginning of the line, and     * extra blanks are reduced, so that the output is always of the     * form:</p>     * <code>     * &lt;prefix&gt; + &lt;one-optional-"Re:"*gt; + &lt;remaining subject&gt;     * </code>     * <p>I have done extensive testing of this routine with a standalone     * driver, and am leaving the commented out debug messages so that     * when someone decides to enhance this method, it can be yanked it     * from this file, embedded it with a test driver, and the comments     * enabled.</p>     */    static private String normalizeSubject(final String subj, final String prefix) {        // JDK IMPLEMENTATION NOTE!  When we require JDK 1.4+, all        // occurrences of subject.toString.().indexOf(...) can be        // replaced by subject.indexOf(...).        StringBuffer subject = new StringBuffer(subj);        int prefixLength = prefix.length();        // System.err.println("In:  " + subject);        // If the "prefix" is not at the beginning the subject line, remove it        int index = subject.toString().indexOf(prefix);        if (index != 0) {            // System.err.println("(p) index: " + index + ", subject: " + subject);            if (index > 0) {                subject.delete(index, index + prefixLength);            }            subject.insert(0, prefix); // insert prefix at the front        }        // Replace Re: with RE:        String match = "Re:";        index = subject.toString().indexOf(match, prefixLength);        while(index > -1) {            // System.err.println("(a) index: " + index + ", subject: " + subject);            subject.replace(index, index + match.length(), "RE:");            index = subject.toString().indexOf(match, prefixLength);            // System.err.println("(b) index: " + index + ", subject: " + subject);        }        // Reduce them to one at the beginning        match ="RE:";        int indexRE = subject.toString().indexOf(match, prefixLength) + match.length();        index = subject.toString().indexOf(match, indexRE);        while(index > 0) {            // System.err.println("(c) index: " + index + ", subject: " + subject);            subject.delete(index, index + match.length());            index = subject.toString().indexOf(match, indexRE);            // System.err.println("(d) index: " + index + ", subject: " + subject);        }        // Reduce blanks        match = "  ";        index = subject.toString().indexOf(match, prefixLength);        while(index > -1) {            // System.err.println("(e) index: " + index + ", subject: " + subject);            subject.replace(index, index + match.length(), " ");            index = subject.toString().indexOf(match, prefixLength);            // System.err.println("(f) index: " + index + ", subject: " + subject);        }        // System.err.println("Out: " + subject);        return subject.toString();    }    /**     * lazy retrieval     * @return ICommandListservManager     */    protected ICommandListservManager getCommandListservManager() {        if (commandListservManager == null) {            commandListservManager = (ICommandListservManager) getMailetContext().getAttribute(ICommandListservManager.ID + listName);            if (commandListservManager == null) {                throw new IllegalStateException("Unable to find command list manager named: " + listName);            }        }        return commandListservManager;    }    /**     * Lazy init     * @throws MessagingException     */    protected CommandListservFooter getCommandListservFooter() throws MessagingException {        if (commandListservFooter == null) {            commandListservFooter = new CommandListservFooter(getCommandListservManager());            commandListservFooter.init(getMailetConfig());        }        return commandListservFooter;    }    /**     * Retrieves a data field, potentially defined by a super class.     * @return null if not found, the object otherwise     */    protected static Object getField(Object instance, String name) throws IllegalAccessException {        Class clazz = instance.getClass();        Field[] fields;        while (clazz != null) {            fields = clazz.getDeclaredFields();            for (int index = 0; index < fields.length; index++) {                Field field = fields[index];                if (field.getName().equals(name)) {                    field.setAccessible(true);                    return field.get(instance);                }            }            clazz = clazz.getSuperclass();        }        return null;    }    protected void initAllowedPosters(Configuration configuration) throws Exception {        final Configuration allowedPostersElement = configuration.getChild("allowedposters");        allowedPosters = new ArrayList();        if (allowedPostersElement != null) {            final Configuration[] addresses = allowedPostersElement.getChildren("address");            for (int index = 0; index < addresses.length; index++) {                Configuration address = addresses[index];                String emailAddress = address.getValue();                allowedPosters.add(new MailAddress(emailAddress));            }        }    }    /**     * Returns true if this user is ok to send to the list     *     * @param mail     * @return true if this message is ok, false otherwise     * @throws MessagingException     */    protected boolean checkAllowedPoster(Mail mail, Collection members) throws MessagingException {        /*        if we don't require someone to be an allowed poster, then allow post if we don't require require them to be a subscriber, or they are one.        if the sender is in the allowed list, post        */        if ((!specificPostersOnly && (!membersOnly || members.contains(mail.getSender()))) || allowedPosters.contains(mail.getSender())) {            return true;        } else {            Properties standardProperties = getCommandListservManager().getStandardProperties();            getCommandListservManager().onError(mail,                                                xmlResources.getString("invalid.mail.subject", standardProperties),                                                xmlResources.getString("error.membersonly", standardProperties));            return false;        }    }}

⌨️ 快捷键说明

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