File Source: BlacklistChecker.java

     1  /*
     2  * Licensed to the Apache Software Foundation (ASF) under one or more
     3  *  contributor license agreements.  The ASF licenses this file to You
     4  * under the Apache License, Version 2.0 (the "License"); you may not
     5  * use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *     http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.  For additional information regarding
    15  * copyright in this work, please see the NOTICE file in the top level
    16  * directory of this distribution.
    17  */
    18  package org.apache.roller.weblogger.util;
    19  
    20  import java.util.ArrayList;
    21  import java.util.List;
    22  import java.util.StringTokenizer;
    23  import java.util.regex.Pattern;
    24  import org.apache.commons.logging.Log;
    25  import org.apache.commons.logging.LogFactory;
    26  import org.apache.roller.weblogger.config.WebloggerConfig;
    27  import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
    28  import org.apache.roller.weblogger.pojos.WeblogEntryComment;
    29  import org.apache.roller.weblogger.pojos.Weblog;
    30  
    31  /**
    32   * Checks comment, trackbacks and referrers for spam.
    33   * @author Lance Lavandowska
    34   * @author Dave Johnson
    35   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.util.BlacklistChecker()
          */
    36  public class BlacklistChecker { 
             /* 
    P/P       *  Method: org.apache.roller.weblogger.util.BlacklistChecker__static_init
              * 
              *  Postconditions:
              *    init'ed(mLogger)
              */
    37      private static Log mLogger = LogFactory.getLog(BlacklistChecker.class);
    38      
    39      /** 
    40       * Test comment, applying all blacklists, if configured 
    41       * @return True if comment matches blacklist term
    42       */
    43      public static boolean checkComment(WeblogEntryComment comment) {
                 /* 
    P/P           *  Method: bool checkComment(WeblogEntryComment)
                  * 
                  *  Preconditions:
                  *    (soft) comment != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistRegex != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistStr != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.mLogger != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@44: {0}, {1}
                  */
    44          if (WebloggerConfig.getBooleanProperty("site.blacklist.enable.comments")) {
    45              return testComment(comment);
    46          }
    47          return false;
    48      }
    49      
    50      /** 
    51       * Test trackback comment, applying all blacklists, if configured 
    52       * @return True if comment matches blacklist term
    53       */
    54      public static boolean checkTrackback(WeblogEntryComment comment) {
                 /* 
    P/P           *  Method: bool checkTrackback(WeblogEntryComment)
                  * 
                  *  Preconditions:
                  *    (soft) comment != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistRegex != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistStr != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.mLogger != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@55: {0}, {1}
                  */
    55          if (WebloggerConfig.getBooleanProperty("site.blacklist.enable.trackbacks")) {
    56              return testComment(comment);
    57          }
    58          return false;
    59      }
    60  
    61      /** 
    62       * Test referrer URL, applying blacklist and website blacklist only if configured 
    63       * @return True if comment matches blacklist term
    64       */
    65      public static boolean checkReferrer(Weblog website, String referrerURL) {
                 /* 
    P/P           *  Method: bool checkReferrer(Weblog, String)
                  * 
                  *  Preconditions:
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.mLogger != null
                  *    (soft) referrerURL != null
                  *    (soft) website != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getBooleanProperty(...)@66: {0}, {1}
                  *    org.apache.roller.weblogger.config.WebloggerRuntimeConfig:getProperty(...)@71: Addr_Set{null}, Inverse{null}
                  */
    66          if (WebloggerConfig.getBooleanProperty("site.blacklist.enable.referrers")) {
    67              List stringRules = new ArrayList();
    68              List regexRules = new ArrayList();
    69              Blacklist.populateSpamRules(
    70                  website.getBlacklist(), stringRules, regexRules, null);
    71              if (WebloggerRuntimeConfig.getProperty("spam.blacklist") != null) {
    72                  Blacklist.populateSpamRules(
    73                      WebloggerRuntimeConfig.getProperty("spam.blacklist"), stringRules, regexRules, null);
    74              }
    75              return Blacklist.matchesRulesOnly(referrerURL, stringRules, regexRules);
    76          }
    77          return false;
    78      }
    79  
    80      /** 
    81       * Test comment against built in blacklist, site blacklist and website blacklist 
    82       * @return True if comment matches blacklist term
    83       */
    84      private static boolean testComment(WeblogEntryComment c) {
                 /* 
    P/P           *  Method: bool testComment(WeblogEntryComment)
                  * 
                  *  Preconditions:
                  *    c != null
                  *    org/apache/roller/weblogger/util/Blacklist.blacklist != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistRegex != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.blacklist.blacklistStr != null
                  *    (soft) org/apache/roller/weblogger/util/Blacklist.mLogger != null
                  * 
                  *  Presumptions:
                  *    org.apache.roller.weblogger.pojos.WeblogEntry:getWebsite(...)@88 != null
                  *    org.apache.roller.weblogger.pojos.WeblogEntryComment:getWeblogEntry(...)@88 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    85          boolean ret = false;
    86          List stringRules = new ArrayList();
    87          List regexRules = new ArrayList();
    88          Weblog website = c.getWeblogEntry().getWebsite();
    89          Blacklist.populateSpamRules(
    90              website.getBlacklist(), stringRules, regexRules, 
    91              WebloggerRuntimeConfig.getProperty("spam.blacklist"));
    92          Blacklist blacklist = Blacklist.getBlacklist();
    93          if (   blacklist.isBlacklisted(c.getUrl(),     stringRules, regexRules)
    94              || blacklist.isBlacklisted(c.getEmail(),   stringRules, regexRules)
    95              || blacklist.isBlacklisted(c.getName(),    stringRules, regexRules)
    96              || blacklist.isBlacklisted(c.getContent(), stringRules, regexRules)) {
    97              ret = true;
    98          }
    99          return ret;
   100      }        
   101  }
   102  








SofCheck Inspector Build Version : 2.18479
BlacklistChecker.java 2009-Jan-02 14:25:06
BlacklistChecker.class 2009-Sep-04 03:12:32