james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r453276 - in /james/jspf/trunk/src: main/java/org/apache/james/jspf/ main/java/org/apache/james/jspf/exceptions/ main/java/org/apache/james/jspf/policies/local/ test/java/org/apache/james/jspf/
Date Thu, 05 Oct 2006 15:53:53 GMT
Author: bago
Date: Thu Oct  5 08:53:51 2006
New Revision: 453276

URL: http://svn.apache.org/viewvc?view=rev&rev=453276
Log:
Removed SPFInternalResult (we don't use it anymore)
Moved DefaultExplanationPolicy to policies.local
Introduced a common abstract SPFResultException to be used as the root of our exceptions:
the concrete implementations provide also the string representation of the result to use.

Added:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java
  (with props)
    james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
  (with props)
Removed:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/DefaultExplanationPolicy.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFInternalResult.java
Modified:
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFResult.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NeutralException.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NoneException.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/PermErrorException.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/TempErrorException.java
    james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/FallbackPolicy.java
    james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPF.java Thu Oct  5 08:53:51 2006
@@ -30,6 +30,7 @@
 import org.apache.james.jspf.exceptions.NeutralException;
 import org.apache.james.jspf.exceptions.NoneException;
 import org.apache.james.jspf.exceptions.PermErrorException;
+import org.apache.james.jspf.exceptions.SPFResultException;
 import org.apache.james.jspf.exceptions.TempErrorException;
 import org.apache.james.jspf.parser.DefaultSPF1Parser;
 import org.apache.james.jspf.parser.DefaultTermsFactory;
@@ -41,6 +42,7 @@
 import org.apache.james.jspf.policies.ParseRecordPolicy;
 import org.apache.james.jspf.policies.SPFRetriever;
 import org.apache.james.jspf.policies.local.BestGuessPolicy;
+import org.apache.james.jspf.policies.local.DefaultExplanationPolicy;
 import org.apache.james.jspf.policies.local.FallbackPolicy;
 import org.apache.james.jspf.policies.local.TrustedForwarderPolicy;
 import org.apache.james.jspf.wiring.DNSServiceEnabled;
@@ -54,7 +56,7 @@
 /**
  * This class is used to generate a SPF-Test and provided all intressting data.
  */
-public class SPF implements SPFChecker, Policy {
+public class SPF implements SPFChecker {
 
     DNSService dnsProbe;
 
@@ -139,7 +141,6 @@
     public SPFResult checkSPF(String ipAddress, String mailFrom, String hostName) {
         SPF1Data spfData = null;
         String result = null;
-        String resultChar = null;
         String explanation = null;
 
         try {
@@ -147,21 +148,14 @@
             spfData = new SPF1Data(mailFrom, hostName, ipAddress);
             spfData.enableDNSService(dnsProbe);
             checkSPF(spfData);
-            SPFInternalResult res = new SPFInternalResult(spfData.getCurrentResult(), spfData.getExplanation());
-            resultChar = res.getResultChar();
+            String resultChar = spfData.getCurrentResult() != null ? spfData.getCurrentResult()
: "";
             result = SPF1Utils.resultToName(resultChar);
-            explanation = res.getExplanation();
-        } catch (PermErrorException e) {
-            log.warn(e.getMessage(),e);
-            result = SPF1Utils.PERM_ERROR_CONV;
-        } catch (NoneException e) {
-            log.warn(e.getMessage(),e);
-            result = SPF1Utils.NONE_CONV;
-        } catch (NeutralException e) {
-            result = SPF1Utils.NEUTRAL_CONV;
-        } catch (TempErrorException e) {
-            log.warn(e.getMessage(),e);
-            result = SPF1Utils.TEMP_ERROR_CONV;
+            explanation = spfData.getExplanation();
+        } catch (SPFResultException e) {
+            result = e.getResult();
+            if (!SPF1Utils.NEUTRAL_CONV.equals(result)) {
+                log.warn(e.getMessage(),e);
+            }
         } catch (IllegalStateException e) {
             // this should never happen at all. But anyway we will set the
             // result to neutral. Safety first ..
@@ -169,7 +163,7 @@
             result = SPF1Constants.NEUTRAL;
         }
 
-        SPFResult ret = new SPFResult(result, resultChar, explanation, spfData);
+        SPFResult ret = new SPFResult(result, explanation, spfData);
         
         log.info("[ipAddress=" + ipAddress + "] [mailFrom=" + mailFrom
                 + "] [helo=" + hostName + "] => " + ret.getResult());
@@ -184,8 +178,21 @@
     public void checkSPF(SPF1Data spfData) throws PermErrorException,
             NoneException, TempErrorException, NeutralException {
 
-        SPF1Record spfRecord = getSPFRecord(spfData.getCurrentDomain());
-        
+        SPF1Record spfRecord = getPolicy().getSPFRecord(spfData.getCurrentDomain());
+        checkSPF(spfData, spfRecord);
+    }
+
+    /**
+     * Check a given spfData with the given spfRecord
+     * 
+     * @param spfData spf data
+     * @param spfRecord record
+     * @throws PermErrorException exception
+     * @throws NoneException exception
+     * @throws TempErrorException exception
+     * @throws NeutralException exception
+     */
+    public void checkSPF(SPF1Data spfData, SPF1Record spfRecord) throws PermErrorException,
NoneException, TempErrorException, NeutralException {
         Iterator i = spfRecord.iterator();
         while (i.hasNext()) {
             SPFChecker m = (SPFChecker) i.next();
@@ -193,14 +200,12 @@
             m.checkSPF(spfData);
 
         }
-
-        
     }
 
     /**
-     * @see org.apache.james.jspf.policies.Policy#getSPFRecord(java.lang.String)
+     * Return a default policy for SPF
      */
-    public SPF1Record getSPFRecord(String currentDomain) throws PermErrorException, TempErrorException,
NoneException, NeutralException {
+    public Policy getPolicy() {
 
         ArrayList policies = new ArrayList();
         
@@ -229,7 +234,7 @@
         
         policies.add(new InitialChecksPolicy());
         
-        return new ChainPolicy(policies).getSPFRecord(currentDomain);
+        return new ChainPolicy(policies);
     }
     
     /**

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFResult.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFResult.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFResult.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/SPFResult.java Thu Oct  5 08:53:51
2006
@@ -26,7 +26,7 @@
  * This class is used to return the result of an SPF lookup.
  * 
  */
-public class SPFResult extends SPFInternalResult {
+public class SPFResult  {
 
     private String headerTextAsString = "";
 
@@ -34,16 +34,17 @@
     
     private String result = null;
 
+    protected String explanation = null;
+    
     /**
      * Construct SPFResult
      * 
      * @param result the result 
-     * @param resultChar the rawResult
      * @param explanation the explanation
      * @param spf1data the SPF1Data
      */
-    public SPFResult(String result, String resultChar, String explanation, SPF1Data spf1data)
{
-        super(resultChar, explanation);
+    public SPFResult(String result, String explanation, SPF1Data spf1data) {
+        this.explanation = explanation;
         this.result = result;
         this.headerTextAsString = generateHeader(result, spf1data);
     }

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NeutralException.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NeutralException.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NeutralException.java
(original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NeutralException.java
Thu Oct  5 08:53:51 2006
@@ -20,16 +20,23 @@
 
 package org.apache.james.jspf.exceptions;
 
+import org.apache.james.jspf.SPF1Utils;
+
 /**
  * This exception get thrown if the result should be neutral
  * 
  */
-public class NeutralException extends Exception {
-
-    private static final long serialVersionUID = 1L;
+public class NeutralException extends SPFResultException {
 
     public NeutralException(String strErrorMessage) {
         super(strErrorMessage);
+    }
+
+    /**
+     * @see org.apache.james.jspf.exceptions.SPFResultException#getResult()
+     */
+    public String getResult() {
+        return SPF1Utils.NEUTRAL_CONV;
     }
 
 }

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NoneException.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NoneException.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NoneException.java (original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/NoneException.java Thu
Oct  5 08:53:51 2006
@@ -20,16 +20,23 @@
 
 package org.apache.james.jspf.exceptions;
 
+import org.apache.james.jspf.SPF1Utils;
+
 /**
  * This exception get thrown if the result should be none
  * 
  */
-public class NoneException extends Exception {
-
-    private static final long serialVersionUID = 1L;
+public class NoneException extends SPFResultException {
 
     public NoneException(String strErrorMessage) {
         super(strErrorMessage);
+    }
+
+    /**
+     * @see org.apache.james.jspf.exceptions.SPFResultException#getResult()
+     */
+    public String getResult() {
+        return SPF1Utils.NONE_CONV;
     }
 
 }

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/PermErrorException.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/PermErrorException.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/PermErrorException.java
(original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/PermErrorException.java
Thu Oct  5 08:53:51 2006
@@ -20,16 +20,23 @@
 
 package org.apache.james.jspf.exceptions;
 
+import org.apache.james.jspf.SPF1Utils;
+
 /**
  * This exception get thrown if the result should be PermError
  * 
  */
-public class PermErrorException extends Exception {
-
-    private static final long serialVersionUID = 1L;
+public class PermErrorException extends SPFResultException {
 
     public PermErrorException(String strErrorMessage) {
         super(strErrorMessage);
+    }
+
+    /**
+     * @see org.apache.james.jspf.exceptions.SPFResultException#getResult()
+     */
+    public String getResult() {
+        return SPF1Utils.PERM_ERROR_CONV;
     }
 
 }

Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java?view=auto&rev=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java
(added)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java
Thu Oct  5 08:53:51 2006
@@ -0,0 +1,42 @@
+/****************************************************************
+ * 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.jspf.exceptions;
+
+/**
+ * Root exception for SPF methods
+ */
+public abstract class SPFResultException extends Exception {
+
+    /**
+     * Exception
+     * 
+     * @param strErrorMessage string
+     */
+    public SPFResultException(String strErrorMessage) {
+        super(strErrorMessage);
+    }
+
+    /**
+     * The result for this exception
+     * 
+     * @return the spf result for this exception
+     */
+    public abstract String getResult();
+}

Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/SPFResultException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/TempErrorException.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/TempErrorException.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/TempErrorException.java
(original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/exceptions/TempErrorException.java
Thu Oct  5 08:53:51 2006
@@ -20,16 +20,23 @@
 
 package org.apache.james.jspf.exceptions;
 
+import org.apache.james.jspf.SPF1Utils;
+
 /**
  * This exception get thrown if the result should be TempError
  * 
  */
-public class TempErrorException extends Exception {
-
-    private static final long serialVersionUID = 1L;
+public class TempErrorException extends SPFResultException {
 
     public TempErrorException(String strErrorMessage) {
         super(strErrorMessage);
+    }
+
+    /**
+     * @see org.apache.james.jspf.exceptions.SPFResultException#getResult()
+     */
+    public String getResult() {
+        return SPF1Utils.TEMP_ERROR_CONV;
     }
 
 }

Added: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java?view=auto&rev=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
(added)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
Thu Oct  5 08:53:51 2006
@@ -0,0 +1,93 @@
+/****************************************************************
+ * 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.jspf.policies.local;
+
+import org.apache.james.jspf.SPF1Utils;
+import org.apache.james.jspf.core.Logger;
+import org.apache.james.jspf.core.SPF1Constants;
+import org.apache.james.jspf.core.SPF1Data;
+import org.apache.james.jspf.core.SPF1Record;
+import org.apache.james.jspf.core.SPFChecker;
+import org.apache.james.jspf.exceptions.NeutralException;
+import org.apache.james.jspf.exceptions.NoneException;
+import org.apache.james.jspf.exceptions.PermErrorException;
+import org.apache.james.jspf.exceptions.TempErrorException;
+import org.apache.james.jspf.macro.MacroExpand;
+import org.apache.james.jspf.policies.AbstractNestedPolicy;
+
+/**
+ * Policy to add a default explanation
+ */
+public final class DefaultExplanationPolicy extends AbstractNestedPolicy {
+    /**
+     * log
+     */
+    private Logger log;
+    /**
+     * the default explanation
+     */
+    private String defExplanation;
+
+    /**
+     * @param spf
+     */
+    public DefaultExplanationPolicy(Logger log, String explanation) {
+        this.log = log;
+        this.defExplanation = explanation;
+    }
+
+    /**
+     * @see org.apache.james.jspf.policies.AbstractNestedPolicy#getSPFRecordPostFilter(java.lang.String,
org.apache.james.jspf.core.SPF1Record)
+     */
+    protected SPF1Record getSPFRecordPostFilter(String currentDomain, SPF1Record spfRecord)
throws PermErrorException, TempErrorException, NoneException, NeutralException {
+        // Default explanation policy
+        spfRecord.getModifiers().add(new SPFChecker() {
+            public void checkSPF(SPF1Data spfData) throws PermErrorException, NoneException,
TempErrorException, NeutralException {
+                
+                if (SPF1Constants.FAIL.equals(spfData.getCurrentResult())) {  
+                    if (spfData.getExplanation()==null || spfData.getExplanation().equals(""))
{
+                        String explanation;
+                        if (defExplanation == null) {
+                            explanation = SPF1Utils.DEFAULT_EXPLANATION;
+                        } else {
+                            explanation = defExplanation;
+                        }
+                        try {
+                            spfData.setExplanation(new MacroExpand(spfData, log)
+                                    .expandExplanation(SPF1Utils.DEFAULT_EXPLANATION));
+                        } catch (PermErrorException e) {
+                            // Should never happen !
+                            log.debug("Invalid defaulfExplanation: " + explanation);
+                        }
+                    }
+                }
+            }
+            
+            public String toString() {
+                if (defExplanation == null) {
+                    return "defaultExplanation";
+                } else {
+                    return "defaultExplanation="+defExplanation;
+                }
+            }
+        });
+        return spfRecord;
+    }
+}
\ No newline at end of file

Propchange: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/FallbackPolicy.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/FallbackPolicy.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/FallbackPolicy.java
(original)
+++ james/jspf/trunk/src/main/java/org/apache/james/jspf/policies/local/FallbackPolicy.java
Thu Oct  5 08:53:51 2006
@@ -27,9 +27,7 @@
 import org.apache.james.jspf.core.Logger;
 import org.apache.james.jspf.core.SPF1Record;
 import org.apache.james.jspf.core.SPFRecordParser;
-import org.apache.james.jspf.exceptions.NeutralException;
-import org.apache.james.jspf.exceptions.NoneException;
-import org.apache.james.jspf.exceptions.PermErrorException;
+import org.apache.james.jspf.exceptions.SPFResultException;
 import org.apache.james.jspf.policies.AbstractNestedPolicy;
 
 /**
@@ -79,13 +77,7 @@
             synchronized (fallBackMap) {
                 fallBackMap.put(host, spfRecord);
             }
-        } catch (PermErrorException e) {
-            throw new IllegalArgumentException("Invalid SPF-Record: "
-                    + rawSpfRecord);
-        } catch (NoneException e) {
-            throw new IllegalArgumentException("Invalid SPF-Record: "
-                    + rawSpfRecord);
-        } catch (NeutralException e) {
+        } catch (SPFResultException e) {
             throw new IllegalArgumentException("Invalid SPF-Record: "
                     + rawSpfRecord);
         }

Modified: james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java
URL: http://svn.apache.org/viewvc/james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java?view=diff&rev=453276&r1=453275&r2=453276
==============================================================================
--- james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java (original)
+++ james/jspf/trunk/src/test/java/org/apache/james/jspf/AbstractYamlTest.java Thu Oct  5
08:53:51 2006
@@ -175,7 +175,7 @@
         String resultSPF = res.getResult();
         
         if (currentTest.get("result") instanceof String) {
-            assertEquals("Test "+next+" ("+currentTest.get("description")+") failed. Returned:
"+res.getResult()+" Expected: "+currentTest.get("result")+" [["+res.getResultChar()+"||"+res.getHeaderText()+"]]",
currentTest.get("result"), res.getResult());
+            assertEquals("Test "+next+" ("+currentTest.get("description")+") failed. Returned:
"+res.getResult()+" Expected: "+currentTest.get("result")+" [["+res.getResult()+"||"+res.getHeaderText()+"]]",
currentTest.get("result"), res.getResult());
         } else {
             ArrayList results = (ArrayList) currentTest.get("result");
             boolean match = false;



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message