Author: norman
Date: Fri Nov 17 08:22:47 2006
New Revision: 476193
URL: http://svn.apache.org/viewvc?view=rev&rev=476193
Log:
More refactoring to support JunkScore.See JAMES-614
Add more javadocs
Modified:
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JunkScoreHandler.java
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
Modified:
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JunkScoreHandler.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JunkScoreHandler.java?view=diff&rev=476193&r1=476192&r2=476193
==============================================================================
---
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JunkScoreHandler.java
(original)
+++
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/JunkScoreHandler.java
Fri Nov 17 08:22:47 2006
@@ -40,6 +40,13 @@
import org.apache.james.util.junkscore.JunkScoreImpl;
import org.apache.james.util.mail.dsn.DSNStatus;
+/**
+ * Check if a configured JunkScore is reached and perform an action. Valid
actions are: reject, compose, header.
+ *
+ * -Reject action reject the mail if the limit is reached.
+ * -Compose action stores the junkScore values in the mail attributes
+ * -Header action create headers which holds the junkScore for each check
+ */
public class JunkScoreHandler extends AbstractLogEnabled implements
ConnectHandler, MessageHandler,Configurable {
private double maxScore = 0;
@@ -66,10 +73,21 @@
}
}
+ /**
+ * Set the max JunkScore
+ *
+ * @param maxScore the score
+ */
public void setMaxScore(double maxScore) {
this.maxScore = maxScore;
}
+ /**
+ * Set the action to perform if the JunkScore limit is reached
+ *
+ * @param action the action
+ * @throws ConfigurationException if invalid action is used
+ */
public void setAction(String action) throws ConfigurationException {
if (!action.equals(REJECT_ACTION) && !action.equals(COMPOSE_ACTION) &&
!action.equals(HEADER_ACTION))
throw new ConfigurationException("Illegal action: " + action);
@@ -85,7 +103,9 @@
}
/**
+ * Check if the JunkScore limit is reached and perform the configured
action
*
+ * @param session the SMTPSession
*/
private void checkScore(SMTPSession session) {
JunkScore score1 = (JunkScore)
session.getConnectionState().get(JunkScore.JUNK_SCORE_SESSION);
Modified:
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java?view=diff&rev=476193&r1=476192&r2=476193
==============================================================================
---
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
(original)
+++
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/URIRBLHandler.java
Fri Nov 17 08:22:47 2006
@@ -35,10 +35,9 @@
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;
-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.logger.AbstractLogEnabled;
+
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
@@ -51,8 +50,8 @@
/**
* Extract domains from message and check against URIRBLServer. For more
informations see http://www.surbl.org
*/
-public class URIRBLHandler extends AbstractLogEnabled implements
MessageHandler,
- Serviceable, Configurable {
+public class URIRBLHandler extends AbstractJunkHandler implements
MessageHandler,
+ Serviceable {
private DNSServer dnsServer;
@@ -62,6 +61,10 @@
private boolean checkAuthNetworks = false;
+ private final static String LISTED_DOMAIN ="LISTED_DOMAIN";
+
+ private final static String URBLSERVER = "URBL_SERVER";
+
/**
* @see
org.apache.avalon.framework.service.Serviceable#service(ServiceManager)
*/
@@ -110,6 +113,8 @@
if (configRelay != null) {
setCheckAuthNetworks(configRelay.getValueAsBoolean(false));
}
+
+ super.configure(arg0);
}
@@ -155,11 +160,57 @@
* @see org.apache.james.smtpserver.MessageHandler#onMessage(SMTPSession)
*/
public void onMessage(SMTPSession session) {
+ doProcessing(session);
+ }
+
+ /**
+ * Recursively scans all MimeParts of an email for domain strings. Domain
+ * strings that are found are added to the supplied HashSet.
+ *
+ * @param domains HashSet for accumulating domain strings
+ * @param part MimePart to scan
+ * @return domains The HashSet that contains the domains which were
extracted
+ */
+ private HashSet scanMailForDomains(MimePart part) throws
MessagingException, IOException {
+ HashSet domains = new HashSet();
+ getLogger().debug("mime type is: \"" + part.getContentType() + "\"");
+
+ if (part.isMimeType("text/plain") || part.isMimeType("text/html")) {
+ getLogger().debug("scanning: \"" + part.getContent().toString() +
"\"");
+ HashSet newDom = URIScanner.scanContentForDomains(domains,
part.getContent().toString());
+
+ // Check if new domains are found and add the domains
+ if (newDom != null && newDom.size() > 0) {
+ domains.addAll(newDom);
+ }
+ } else if (part.isMimeType("multipart/*")) {
+ MimeMultipart multipart = (MimeMultipart) part.getContent();
+ int count = multipart.getCount();
+ getLogger().debug("multipart count is: " + count);
+
+ for (int index = 0; index < count; index++) {
+ getLogger().debug("recursing index: " + index);
+ MimeBodyPart mimeBodyPart = (MimeBodyPart)
multipart.getBodyPart(index);
+ HashSet newDomains = scanMailForDomains(mimeBodyPart);
+
+ // Check if new domains are found and add the domains
+ if(newDomains != null && newDomains.size() > 0) {
+ domains.addAll(newDomains);
+ }
+ }
+ }
+ return domains;
+ }
+
+ /**
+ * @see
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
+ */
+ protected boolean check(SMTPSession session) {
MimeMessage message;
-
+
// Not scan the message if relaying allowed
if (session.isRelayingAllowed() && !checkAuthNetworks) {
- return;
+ return false;
}
try {
@@ -175,8 +226,6 @@
while (uRbl.hasNext()) {
try {
- String responseString = null;
- String detail = null;
String uRblServer = uRbl.next().toString();
String address = target + "." + uRblServer;
@@ -186,38 +235,12 @@
dnsServer.getByName(address);
- if (getLogger().isInfoEnabled()) {
- getLogger().info("Message sent by " +
session.getRemoteIPAddress() + " restricted by " + uRblServer + " because " +
target + " is listed");
- }
-
- // we should try to retrieve details
- if (getDetail) {
- Collection txt = dnsServer.findTXTRecords(address);
-
- // Check if we found a txt record
- if (!txt.isEmpty()) {
- // Set the detail
- detail = txt.iterator().next().toString();
-
- }
- }
-
- if (detail != null) {
-
- responseString = "554 "
- + DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.SECURITY_OTHER)
- + " Rejected: message contains domain " +
target + " listed by " + uRblServer + " . Details: "
- + detail;
- } else {
- responseString = "554 "
- + DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.SECURITY_OTHER)
- + " Rejected: message contains domain " +
target + " listed by " + uRblServer;
- }
+ // store server name for later use
+ session.getState().put(URBLSERVER, uRblServer);
+ session.getState().put(LISTED_DOMAIN,target);
- session.writeResponse(responseString);
- session.setStopHandlerProcessing(true);
session.abortMessage();
- return;
+ return true;
} catch (UnknownHostException uhe) {
// domain not found. keep processing
@@ -229,45 +252,67 @@
} catch (IOException e) {
getLogger().error(e.getMessage());
}
+ return false;
}
/**
- * Recursively scans all MimeParts of an email for domain strings. Domain
- * strings that are found are added to the supplied HashSet.
- *
- * @param domains HashSet for accumulating domain strings
- * @param part MimePart to scan
- * @return domains The HashSet that contains the domains which were
extracted
+ * @see
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkScoreLogString(org.apache.james.smtpserver.SMTPSession)
*/
- private HashSet scanMailForDomains(MimePart part) throws
MessagingException, IOException {
- HashSet domains = new HashSet();
- getLogger().debug("mime type is: \"" + part.getContentType() + "\"");
-
- if (part.isMimeType("text/plain") || part.isMimeType("text/html")) {
- getLogger().debug("scanning: \"" + part.getContent().toString() +
"\"");
- HashSet newDom = URIScanner.scanContentForDomains(domains,
part.getContent().toString());
-
- // Check if new domains are found and add the domains
- if (newDom != null && newDom.size() > 0) {
- domains.addAll(newDom);
- }
- } else if (part.isMimeType("multipart/*")) {
- MimeMultipart multipart = (MimeMultipart) part.getContent();
- int count = multipart.getCount();
- getLogger().debug("multipart count is: " + count);
-
- for (int index = 0; index < count; index++) {
- getLogger().debug("recursing index: " + index);
- MimeBodyPart mimeBodyPart = (MimeBodyPart)
multipart.getBodyPart(index);
- HashSet newDomains = scanMailForDomains(mimeBodyPart);
-
- // Check if new domains are found and add the domains
- if(newDomains != null && newDomains.size() > 0) {
- domains.addAll(newDomains);
- }
+ protected String getJunkScoreLogString(SMTPSession session) {
+ String uRblServer = (String) session.getState().get(URBLSERVER);
+ String target = (String) session.getState().get(LISTED_DOMAIN);
+ return "Message sent by " + session.getRemoteIPAddress() + "
restricted by " + uRblServer + " because " + target + " is listed. Add
junkScore: " + getScore();
+ }
+
+ /**
+ * @see
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getRejectLogString(org.apache.james.smtpserver.SMTPSession)
+ */
+ protected String getRejectLogString(SMTPSession session) {
+ String uRblServer = (String) session.getState().get(URBLSERVER);
+ String target = (String) session.getState().get(LISTED_DOMAIN);
+ return "Rejected: message contains domain " + target + " listed by " +
uRblServer;
+ }
+
+ /**
+ * @see
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getResponseString(org.apache.james.smtpserver.SMTPSession)
+ */
+ protected String getResponseString(SMTPSession session) {
+ String uRblServer = (String) session.getState().get(URBLSERVER);
+ String target = (String) session.getState().get(LISTED_DOMAIN);
+ String detail = null;
+ String responseString = null;
+
+ // we should try to retrieve details
+ if (getDetail) {
+ Collection txt = dnsServer.findTXTRecords(target+ "." +
uRblServer);
+
+ // Check if we found a txt record
+ if (!txt.isEmpty()) {
+ // Set the detail
+ detail = txt.iterator().next().toString();
+
}
}
- return domains;
+
+ if (detail != null) {
+
+ responseString = "554 "
+ + DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.SECURITY_OTHER)
+ + getRejectLogString(session) +" . Details: "
+ + detail;
+ } else {
+ responseString = "554 "
+ + DSNStatus.getStatus(DSNStatus.PERMANENT,
DSNStatus.SECURITY_OTHER)
+ + getRejectLogString(session);
+ }
+ return responseString;
+ }
+
+ /**
+ * @see
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getScoreName()
+ */
+ protected String getScoreName() {
+ return "UriRBLCheck";
}
}
Modified:
james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
URL:
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java?view=diff&rev=476193&r1=476192&r2=476193
==============================================================================
---
james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
(original)
+++
james/server/trunk/src/test/org/apache/james/smtpserver/URIRBLHandlerTest.java
Fri Nov 17 08:22:47 2006
@@ -45,6 +45,8 @@
import org.apache.james.test.mock.avalon.MockLogger;
import org.apache.james.test.mock.javaxmail.MockMimeMessage;
import org.apache.james.test.mock.mailet.MockMail;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreImpl;
import org.apache.mailet.Mail;
public class URIRBLHandlerTest extends TestCase {
@@ -243,5 +245,27 @@
assertTrue("Stop handler processing",
session.getStopHandlerProcessing());
assertNotNull("Email was rejected", getResponse());
+ }
+
+ public void testAddJunkScore() throws IOException, MessagingException {
+
+ ArrayList servers = new ArrayList();
+ servers.add(URISERVER);
+
+ SMTPSession session =
setupMockedSMTPSession(setupMockedMail(setupMockedMimeMessage("http://" +
BAD_DOMAIN1 + "/")));
+ session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
+
+ URIRBLHandler handler = new URIRBLHandler();
+
+ ContainerUtil.enableLogging(handler, new MockLogger());
+ handler.setDnsServer(setupMockedDnsServer());
+ handler.setUriRblServer(servers);
+ handler.setAction("junkScore");
+ handler.setScore(20);
+ handler.onMessage(session);
+
+ assertFalse("Not stop handler processing",
session.getStopHandlerProcessing());
+ assertNull("Email was not rejected", getResponse());
+ assertEquals("JunkScore added", ((JunkScore)
session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("UriRBLCheck"),
20.0, 0d);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]