james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r531202 - in /james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf: SPF.java policies/SPFRetriever.java policies/SPFStrictCheckerRetriever.java
Date Sun, 22 Apr 2007 15:19:14 GMT
Author: bago
Date: Sun Apr 22 08:19:13 2007
New Revision: 531202

URL: http://svn.apache.org/viewvc?view=rev&rev=531202
Log:
Refactored SPFRetriever/SPFStrictCheckerRetriever to implement SPFChecker and support DNSLookupContinuation.
This was probably the last component not yet using DNSLookupContinuation, so it should be
easy to call everything asynchronously now.

Modified:
    james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/SPF.java
    james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java
    james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFStrictCheckerRetriever.java

Modified: james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/SPF.java
URL: http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/SPF.java?view=diff&rev=531202&r1=531201&r2=531202
==============================================================================
--- james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/SPF.java (original)
+++ james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/SPF.java Sun Apr 22
08:19:13 2007
@@ -58,10 +58,8 @@
 import org.apache.james.jspf.wiring.SPFCheckEnabled;
 import org.apache.james.jspf.wiring.WiringServiceTable;
 
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedList;
-import java.util.List;
 
 /**
  * This class is used to generate a SPF-Test and provided all intressting data.
@@ -98,20 +96,7 @@
         public DNSLookupContinuation checkSPF(SPFSession spfData)
                 throws PermErrorException, TempErrorException,
                 NeutralException, NoneException {
-            List policies = getPolicies();
-            List policyFiters = getPolicyFilters();
-            LinkedList resultCheckers = new LinkedList();
-            
-            for (int i = 0; i < policies.size(); i++) {
-                Policy policy = (Policy) policies.get(i);
-                resultCheckers.add(new SPFPolicyChecker(policy));
-            }
-            
-            for (int i = 0; i < policyFiters.size(); i++) {
-                PolicyPostFilter policyPostFilter = (PolicyPostFilter) policyFiters.get(i);
-                resultCheckers.add(new SPFPolicyPostFilterChecker(policyPostFilter));
-            }
-            
+            LinkedList resultCheckers = getPolicies();
 
             while (resultCheckers.size() > 0) {
                 SPFChecker removeLast = (SPFChecker) resultCheckers.removeLast();
@@ -173,7 +158,7 @@
 
     }
 
-    private static final String ATTRIBUTE_SPF1_RECORD = "SPF.SPF1Record";
+    public static final String ATTRIBUTE_SPF1_RECORD = "SPF.SPF1Record";
 
     DNSService dnsProbe;
 
@@ -334,51 +319,44 @@
     /**
      * Return a default policy for SPF
      */
-    public List getPolicies() {
+    public LinkedList getPolicies() {
 
-        ArrayList policies = new ArrayList();
+        LinkedList policies = new LinkedList();
         
         if (override != null) {
-            policies.add(override);
+            policies.add(new SPFPolicyChecker(override));
         }
         
         if (mustEquals) {
-            policies.add(new SPFStrictCheckerRetriever(dnsProbe));
+            policies.add(new SPFStrictCheckerRetriever());
         } else {
-            policies.add(new SPFRetriever(dnsProbe));
+            policies.add(new SPFRetriever());
         }
 
-        policies.add(new InitialChecksPolicy());
-        
-        return policies;
-    }
-    
-    public List getPolicyFilters() {
-
-        ArrayList policyFilters = new ArrayList();
+        policies.add(new SPFPolicyChecker(new InitialChecksPolicy()));
 
         if (useBestGuess) {
-            policyFilters.add(new BestGuessPolicy());
+            policies.add(new SPFPolicyPostFilterChecker(new BestGuessPolicy()));
         }
         
-        policyFilters.add(new ParseRecordPolicy(parser));
+        policies.add(new SPFPolicyPostFilterChecker(new ParseRecordPolicy(parser)));
         
         if (fallBack != null) {
-            policyFilters.add(fallBack);
+            policies.add(new SPFPolicyPostFilterChecker(fallBack));
         }
 
-        policyFilters.add(new NoSPFRecordFoundPolicy());
+        policies.add(new SPFPolicyPostFilterChecker(new NoSPFRecordFoundPolicy()));
         
         // trustedForwarder support is enabled
         if (useTrustedForwarder) {
-            policyFilters.add(new TrustedForwarderPolicy(log));
+            policies.add(new SPFPolicyPostFilterChecker(new TrustedForwarderPolicy(log)));
         }
 
-        policyFilters.add(new NeutralIfNotMatchPolicy());
+        policies.add(new SPFPolicyPostFilterChecker(new NeutralIfNotMatchPolicy()));
 
-        policyFilters.add(new DefaultExplanationPolicy(log, defaultExplanation, macroExpand));
+        policies.add(new SPFPolicyPostFilterChecker(new DefaultExplanationPolicy(log, defaultExplanation,
macroExpand)));
         
-        return policyFilters;
+        return policies;
     }
     
     /**

Modified: james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java
URL: http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java?view=diff&rev=531202&r1=531201&r2=531202
==============================================================================
--- james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java
(original)
+++ james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFRetriever.java
Sun Apr 22 08:19:13 2007
@@ -1,8 +1,16 @@
 package org.apache.james.jspf.policies;
 
+import org.apache.james.jspf.SPF;
+import org.apache.james.jspf.core.DNSLookupContinuation;
+import org.apache.james.jspf.core.DNSRequest;
+import org.apache.james.jspf.core.DNSResponse;
 import org.apache.james.jspf.core.DNSService;
 import org.apache.james.jspf.core.SPF1Constants;
 import org.apache.james.jspf.core.SPF1Record;
+import org.apache.james.jspf.core.SPFChecker;
+import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
+import org.apache.james.jspf.core.SPFSession;
+import org.apache.james.jspf.core.DNSService.TimeoutException;
 import org.apache.james.jspf.exceptions.NeutralException;
 import org.apache.james.jspf.exceptions.NoneException;
 import org.apache.james.jspf.exceptions.PermErrorException;
@@ -14,75 +22,9 @@
 /**
  * Get the raw dns txt or spf entry which contains a spf entry
  */
-public class SPFRetriever implements Policy {
-    /**
-     * dns service
-     */
-    private final DNSService dns;
-
-
-    /**
-     * A new instance of the SPFRetriever
-     * 
-     * @param dns the dns service
-     */
-    public SPFRetriever(DNSService dns) {
-        this.dns = dns;
-    }
-
+public class SPFRetriever implements SPFChecker {
 
     /**
-     * @see org.apache.james.jspf.policies.Policy#getSPFRecord(java.lang.String)
-     */
-    public SPF1Record getSPFRecord(String currentDomain) throws PermErrorException, TempErrorException,
NoneException, NeutralException {
-        // retrieve the SPFRecord
-        String spfDnsEntry = retrieveSpfRecord(currentDomain);
-        if (spfDnsEntry != null) {
-            return new SPF1Record(spfDnsEntry);
-        } else {
-            return null;
-        }
-    }
-
-    /**
-     * Get the SPF-Record for a server
-     * 
-     * @param dns
-     *            The dns service to query
-     * @param hostname
-     *            The hostname for which we want to retrieve the SPF-Record
-     * @param spfVersion
-     *            The SPF-Version which should used.
-     * @return The SPF-Record if one is found.
-     * @throws PermErrorException
-     *             if more then one SPF-Record was found.
-     * @throws TempErrorException
-     *             if the lookup result was "TRY_AGAIN"
-     */
-    protected String retrieveSpfRecord(String hostname)
-            throws PermErrorException, TempErrorException {
-
-        try {
-            // first check for SPF-Type records
-            List spfR = dns.getRecords(hostname, DNSService.SPF);
-            
-            if (spfR == null || spfR.isEmpty()) {
-                // do DNS lookup for TXT
-                spfR = dns.getRecords(hostname, DNSService.TXT);
-            }
-    
-            // process returned records
-            if (spfR != null && !spfR.isEmpty()) {
-                return extractSPFRecord(spfR);
-            } else {
-                return null;
-            }
-        } catch (DNSService.TimeoutException e) {
-            throw new TempErrorException("Timeout querying dns");
-        }
-    }
-    
-    /**
      * Return the extracted SPF-Record 
      *  
      * @param spfR the List which holds TXT/SPF - Records
@@ -91,7 +33,9 @@
      *                            given List.
      */
     protected String extractSPFRecord(List spfR) throws PermErrorException {
-       String returnValue = null;
+        if (spfR == null || spfR.isEmpty()) return null;
+        
+        String returnValue = null;
         Iterator all = spfR.iterator();
            
         while (all.hasNext()) {
@@ -121,13 +65,68 @@
     }
     
 
-    /**
-     * Return the DNSService
-     * 
-     * @return the dns
-     */
-    protected DNSService getDNSService() {
-        return dns;
+    public DNSLookupContinuation checkSPF(SPFSession spfData)
+            throws PermErrorException, TempErrorException, NeutralException,
+            NoneException {
+        SPF1Record res = (SPF1Record) spfData.getAttribute(SPF.ATTRIBUTE_SPF1_RECORD);
+        if (res == null) {
+            String currentDomain = spfData.getCurrentDomain();
+            return new DNSLookupContinuation(new DNSRequest(currentDomain, DNSService.SPF),
new SPFCheckerDNSResponseListener() {
+
+                public DNSLookupContinuation onDNSResponse(
+                        DNSResponse response, SPFSession session)
+                        throws PermErrorException, NoneException,
+                        TempErrorException, NeutralException {
+                    try {
+                        List spfR = response.getResponse();
+                        
+                        if (spfR == null || spfR.isEmpty()) {
+                            
+                            String currentDomain = session.getCurrentDomain();
+                            return new DNSLookupContinuation(new DNSRequest(currentDomain,
DNSService.TXT), new SPFCheckerDNSResponseListener() {
+
+                                public DNSLookupContinuation onDNSResponse(
+                                        DNSResponse response, SPFSession session)
+                                        throws PermErrorException,
+                                        NoneException, TempErrorException,
+                                        NeutralException {
+                                    
+                                    List spfR;
+                                    try {
+                                        spfR = response.getResponse();
+                                        String record = extractSPFRecord(spfR);
+                                        if (record != null) {
+                                            session.setAttribute(SPF.ATTRIBUTE_SPF1_RECORD,
new SPF1Record(record));
+                                        }
+                                    } catch (TimeoutException e) {
+                                        throw new TempErrorException("Timeout querying dns");
+                                    }
+                                    return null;
+                                    
+                                }
+                                
+                            });
+                            
+                        } else {
+                            
+                            String record = extractSPFRecord(spfR);
+                            if (record != null) {
+                                session.setAttribute(SPF.ATTRIBUTE_SPF1_RECORD, new SPF1Record(record));
+                            }
+                            
+                        }
+                        
+                        return null;
+                        
+                    } catch (DNSService.TimeoutException e) {
+                        throw new TempErrorException("Timeout querying dns");
+                    }
+                }
+                
+            });
+            
+        }
+        return null;
     }
 
 

Modified: james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFStrictCheckerRetriever.java
URL: http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFStrictCheckerRetriever.java?view=diff&rev=531202&r1=531201&r2=531202
==============================================================================
--- james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFStrictCheckerRetriever.java
(original)
+++ james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/SPFStrictCheckerRetriever.java
Sun Apr 22 08:19:13 2007
@@ -1,6 +1,16 @@
 package org.apache.james.jspf.policies;
 
+import org.apache.james.jspf.SPF;
+import org.apache.james.jspf.core.DNSLookupContinuation;
+import org.apache.james.jspf.core.DNSRequest;
+import org.apache.james.jspf.core.DNSResponse;
 import org.apache.james.jspf.core.DNSService;
+import org.apache.james.jspf.core.SPF1Record;
+import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
+import org.apache.james.jspf.core.SPFSession;
+import org.apache.james.jspf.core.DNSService.TimeoutException;
+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;
 
@@ -12,57 +22,87 @@
  */
 public class SPFStrictCheckerRetriever extends SPFRetriever {
 
-    public SPFStrictCheckerRetriever(DNSService dns) {
-        super(dns);
-    }
+    
 
-    /**
-     * Get the SPF-Record for a server
-     * 
-     * 
-     * @param dns
-     *            The dns service to query
-     * @param hostname
-     *            The hostname for which we want to retrieve the SPF-Record
-     * @param spfVersion
-     *            The SPF-Version which should used.
-     * @return The SPF-Record if one is found.
-     * @throws PermErrorException
-     *             if more then one SPF-Record was found, or one SPF-Type SPF-Record 
-     *             and one TXT-Type SPF-Record was published and these are not equals.
-     * @throws TempErrorException
-     *             if the lookup result was "TRY_AGAIN"
-     */
-    protected String retrieveSpfRecord(String hostname)
-            throws PermErrorException, TempErrorException {
-
-        try {
-            String spfR1 = null;
-            String spfR2 = null;
-            // do DNS lookup for SPF-Type
-            List spfR = getDNSService().getRecords(hostname, DNSService.SPF);
+    private static final String ATTRIBUTE_SPFSTRICT_CHECK_SPFRECORDS = "SPFStrictCheck.SPFRecords";
 
-            // do DNS lookup for TXT-Type
-            List spfTxtR = getDNSService().getRecords(hostname, DNSService.TXT);
-            
-            if (spfR != null) spfR1 = extractSPFRecord(spfR);
-            if (spfTxtR != null) spfR2 = extractSPFRecord(spfTxtR);
-            
-            if (spfR1 != null && spfR2 == null) {
-                return spfR1;
-            } else if (spfR1 == null && spfR2 != null) {
-                return spfR2;
-            } else if (spfR1 != null && spfR2 != null) {
-                if (spfR1.toLowerCase().equals(spfR2.toLowerCase()) == false) {
-                    throw new PermErrorException("Published SPF records not equals");
-                } else {
-                    return spfR1;
+
+    public DNSLookupContinuation checkSPF(SPFSession spfData)
+            throws PermErrorException, TempErrorException, NeutralException,
+            NoneException {
+        SPF1Record res = (SPF1Record) spfData.getAttribute(SPF.ATTRIBUTE_SPF1_RECORD);
+        if (res == null) {
+            String currentDomain = spfData.getCurrentDomain();
+            return new DNSLookupContinuation(new DNSRequest(currentDomain, DNSService.SPF),
new SPFCheckerDNSResponseListener() {
+
+                public DNSLookupContinuation onDNSResponse(
+                        DNSResponse response, SPFSession session)
+                        throws PermErrorException, NoneException,
+                        TempErrorException, NeutralException {
+                    try {
+                        List spfR = response.getResponse();
+                        
+                        session.setAttribute(ATTRIBUTE_SPFSTRICT_CHECK_SPFRECORDS, spfR);
+                        
+                        String currentDomain = session.getCurrentDomain();
+                        return new DNSLookupContinuation(new DNSRequest(currentDomain, DNSService.TXT),
new SPFCheckerDNSResponseListener() {
+
+                            public DNSLookupContinuation onDNSResponse(
+                                    DNSResponse response, SPFSession session)
+                                    throws PermErrorException,
+                                    NoneException, TempErrorException,
+                                    NeutralException {
+                                
+                                List spfR = (List) session.getAttribute(ATTRIBUTE_SPFSTRICT_CHECK_SPFRECORDS);
+                                List spfTxtR = null;
+                                try {
+                                    spfTxtR = response.getResponse();
+                                } catch (TimeoutException e) {
+                                    throw new TempErrorException("Timeout querying dns");
+                                }
+
+                                String record = calculateSpfRecord(spfR, spfTxtR);
+                                if (record != null) {
+                                    session.setAttribute(SPF.ATTRIBUTE_SPF1_RECORD, new SPF1Record(record));
+                                }
+
+                                return null;
+                                
+                            }
+                            
+                        });
+                            
+                    } catch (DNSService.TimeoutException e) {
+                        throw new TempErrorException("Timeout querying dns");
+                    }
                 }
+                
+            });
+            
+        }
+        return null;
+    }
+
+
+    private String calculateSpfRecord(List spfR, List spfTxtR)
+            throws PermErrorException {
+        String spfR1 = null;
+        String spfR2 = null;
+        if (spfR != null) spfR1 = extractSPFRecord(spfR);
+        if (spfTxtR != null) spfR2 = extractSPFRecord(spfTxtR);
+        
+        if (spfR1 != null && spfR2 == null) {
+            return spfR1;
+        } else if (spfR1 == null && spfR2 != null) {
+            return spfR2;
+        } else if (spfR1 != null && spfR2 != null) {
+            if (spfR1.toLowerCase().equals(spfR2.toLowerCase()) == false) {
+                throw new PermErrorException("Published SPF records not equals");
             } else {
-                return null;
+                return spfR1;
             }
-        } catch (DNSService.TimeoutException e) {
-            throw new TempErrorException("Timeout querying dns");
+        } else {
+            return null;
         }
     }
 }



---------------------------------------------------------------------
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