Author: bago
Date: Sat Apr 21 13:48:30 2007
New Revision: 531105
URL: http://svn.apache.org/viewvc?view=rev&rev=531105
Log:
Completed previous refactoring: no more compilation errors and tests pass again.
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java?view=diff&rev=531105&r1=531104&r2=531105
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExistsMechanism.java
Sat Apr 21 13:48:30 2007
@@ -20,10 +20,13 @@
package org.apache.james.jspf.terms;
+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.SPFSession;
import org.apache.james.jspf.exceptions.PermErrorException;
import org.apache.james.jspf.exceptions.TempErrorException;
+import org.apache.james.jspf.util.DNSResolver;
import org.apache.james.jspf.util.SPFTermsRegexps;
import org.apache.james.jspf.wiring.DNSServiceEnabled;
@@ -31,7 +34,6 @@
/**
* This class represent the exists mechanism
- *
*/
public class ExistsMechanism extends GenericMechanism implements
DNSServiceEnabled {
@@ -49,27 +51,34 @@
*/
public boolean run(SPFSession spfData) throws PermErrorException,
TempErrorException {
- List aRecords;
-
// update currentDepth
spfData.increaseCurrentDepth();
String host = expandHost(spfData);
+ return this.onDNSResponse(DNSResolver.lookup(dnsService, new
DNSRequest(host,DNSService.A)), spfData);
+ }
+
+ /**
+ * @see
org.apache.james.jspf.core.Mechanism#onDNSResponse(org.apache.james.jspf.core.DNSResponse,
org.apache.james.jspf.core.SPFSession)
+ */
+ public boolean onDNSResponse(DNSResponse response, SPFSession spfSession)
throws PermErrorException, TempErrorException {
+ List aRecords;
+
try {
- aRecords = dnsService.getRecords(host,DNSService.A);
+ aRecords = response.getResponse();
} catch (DNSService.TimeoutException e) {
return false;
}
-
+
if (aRecords != null && aRecords.size() > 0) {
return true;
}
-
+
// No match found
return false;
}
-
+
/**
* @see java.lang.Object#toString()
*/
@@ -83,6 +92,5 @@
public void enableDNSService(DNSService service) {
this.dnsService = service;
}
-
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java?view=diff&rev=531105&r1=531104&r2=531105
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IP4Mechanism.java
Sat Apr 21 13:48:30 2007
@@ -21,9 +21,12 @@
package org.apache.james.jspf.terms;
import org.apache.james.jspf.core.Configuration;
+import org.apache.james.jspf.core.DNSResponse;
import org.apache.james.jspf.core.IPAddr;
import org.apache.james.jspf.core.SPFSession;
+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.util.Inet6Util;
/**
@@ -113,5 +116,14 @@
} else {
return "ip4:"+getIp().getIPAddress()+"/"+getIp().getMaskLength();
}
+ }
+
+ /**
+ * @see
org.apache.james.jspf.core.Mechanism#onDNSResponse(org.apache.james.jspf.core.DNSResponse,
org.apache.james.jspf.core.SPFSession)
+ */
+ public boolean onDNSResponse(DNSResponse response, SPFSession spfSession)
+ throws PermErrorException, TempErrorException, NoneException {
+ // not needed, IP4Mechanism does not require further lookups
+ return false;
}
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java?view=diff&rev=531105&r1=531104&r2=531105
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/IncludeMechanism.java
Sat Apr 21 13:48:30 2007
@@ -22,6 +22,7 @@
import org.apache.james.jspf.core.Configurable;
import org.apache.james.jspf.core.Configuration;
+import org.apache.james.jspf.core.DNSResponse;
import org.apache.james.jspf.core.Logger;
import org.apache.james.jspf.core.Mechanism;
import org.apache.james.jspf.core.SPF1Constants;
@@ -159,5 +160,14 @@
*/
public void enableMacroExpand(MacroExpand macroExpand) {
this.macroExpand = macroExpand;
+ }
+
+ /**
+ * @see
org.apache.james.jspf.core.Mechanism#onDNSResponse(org.apache.james.jspf.core.DNSResponse,
org.apache.james.jspf.core.SPFSession)
+ */
+ public boolean onDNSResponse(DNSResponse response, SPFSession spfSession)
+ throws PermErrorException, TempErrorException, NoneException {
+ // not called yet.
+ return false;
}
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java?view=diff&rev=531105&r1=531104&r2=531105
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
Sat Apr 21 13:48:30 2007
@@ -20,15 +20,17 @@
package org.apache.james.jspf.terms;
+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.IPAddr;
import org.apache.james.jspf.core.SPFSession;
import org.apache.james.jspf.exceptions.PermErrorException;
import org.apache.james.jspf.exceptions.TempErrorException;
+import org.apache.james.jspf.util.DNSResolver;
import org.apache.james.jspf.util.SPFTermsRegexps;
import org.apache.james.jspf.wiring.DNSServiceEnabled;
-import java.util.ArrayList;
import java.util.List;
/**
@@ -37,6 +39,12 @@
*/
public class PTRMechanism extends GenericMechanism implements
DNSServiceEnabled {
+ private static final String ATTRIBUTE_CURRENT_DOMAIN =
"PTRMechanism.currentDomain";
+
+ private static final String ATTRIBUTE_EXPANDED_HOST =
"PTRMechanism.expandedHost";
+
+ private static final String ATTRIBUTE_DOMAIN_LIST =
"PTRMechanism.domainListCheck";
+
/**
* ABNF: PTR = "ptr" [ ":" domain-spec ]
*/
@@ -50,75 +58,108 @@
*/
public boolean run(SPFSession spfData) throws PermErrorException,
TempErrorException {
- String compareDomain;
- ArrayList validatedHosts = new ArrayList();
-
// update currentDepth
spfData.increaseCurrentDepth();
- // Get the right host.
- String host = expandHost(spfData);
+ // Get PTR Records for the ipAddress which is provided by SPF1Data
+ IPAddr ip = IPAddr.getAddress(spfData.getIpAddress());
+
+ DNSResponse response = DNSResolver.lookup(dnsService, new
DNSRequest(ip.getReverseIP(), DNSService.PTR));
+ return this.onDNSResponse(response, spfData);
+ }
+
+ /**
+ * @see
org.apache.james.jspf.wiring.DNSServiceEnabled#enableDNSService(org.apache.james.jspf.core.DNSService)
+ */
+ public void enableDNSService(DNSService service) {
+ this.dnsService = service;
+ }
+ /**
+ * @see
org.apache.james.jspf.core.Mechanism#onDNSResponse(org.apache.james.jspf.core.DNSResponse,
org.apache.james.jspf.core.SPFSession)
+ */
+ public boolean onDNSResponse(DNSResponse response, SPFSession spfSession)
+ throws PermErrorException, TempErrorException {
+
+ List domainList = (List)
spfSession.getAttribute(ATTRIBUTE_DOMAIN_LIST);
try {
- // Get PTR Records for the ipAddress which is provided by SPF1Data
- IPAddr ip = IPAddr.getAddress(spfData.getIpAddress());
- List domainList = dnsService.getRecords(ip.getReverseIP(),
DNSService.PTR);
-
- // No PTR records found
- if (domainList == null) return false;
-
- // check if the maximum lookup count is reached
- if (dnsService.getRecordLimit() > 0 && domainList.size() >
dnsService.getRecordLimit()) {
- // Truncate the PTR list to getRecordLimit.
- // See #ptr-limit rfc4408 test
- domainList = domainList.subList(0,
dnsService.getRecordLimit()-1);
- // throw new PermErrorException("Maximum PTR lookup count
reached");
- }
-
- for (int i = 0; i < domainList.size(); i++) {
- List aList = null;
+ if (domainList == null) {
+
+ domainList = response.getResponse();
- // check if the connecting ip is ip6. If so lookup AAAA record
- if (IPAddr.isIPV6(spfData.getIpAddress())) {
- // Get aaaa record for this
- aList = dnsService.getRecords(
- (String) domainList.get(i), DNSService.AAAA);
- } else {
- // Get a record for this
- aList = dnsService.getRecords(
- (String) domainList.get(i), DNSService.A);
+ // No PTR records found
+ if (domainList == null) return false;
+
+ // check if the maximum lookup count is reached
+ if (dnsService.getRecordLimit() > 0 && domainList.size() >
dnsService.getRecordLimit()) {
+ // Truncate the PTR list to getRecordLimit.
+ // See #ptr-limit rfc4408 test
+ domainList = domainList.subList(0,
dnsService.getRecordLimit()-1);
+ // throw new PermErrorException("Maximum PTR lookup count
reached");
}
+
+ spfSession.setAttribute(ATTRIBUTE_DOMAIN_LIST, domainList);
+
+ // Get the right host.
+ String host = expandHost(spfSession);
+
+ spfSession.setAttribute(ATTRIBUTE_EXPANDED_HOST, host);
+
+ } else {
+
+ String compareDomain = (String)
spfSession.getAttribute(ATTRIBUTE_CURRENT_DOMAIN);
+ String host = (String)
spfSession.getAttribute(ATTRIBUTE_EXPANDED_HOST);
+
+ List aList = response.getResponse();
+
+
if (aList != null) {
for (int j = 0; j < aList.size(); j++) {
- if (aList.get(j).equals(spfData.getIpAddress())) {
- validatedHosts.add(domainList.get(i));
+ if (aList.get(j).equals(spfSession.getIpAddress())) {
+
+ if (compareDomain.equals(host)
+ || compareDomain.endsWith("." + host)) {
+ return true;
+ }
+
}
}
}
- }
-
- // Check if we match one of this ptr!
- for (int j = 0; j < validatedHosts.size(); j++) {
- compareDomain = (String) validatedHosts.get(j);
-
- if (compareDomain.equals(host)
- || compareDomain.endsWith("." + host)) {
- return true;
- }
- }
- return false;
+ }
} catch (DNSService.TimeoutException e) {
throw new TempErrorException("Timeout querying the dns server");
}
+
- }
+ try {
+
+ if (domainList.size() > 0) {
+ String currentDomain = (String) domainList.remove(0);
+
+ DNSRequest dnsRequest;
+ // check if the connecting ip is ip6. If so lookup AAAA record
+ if (IPAddr.isIPV6(spfSession.getIpAddress())) {
+ // Get aaaa record for this
+ dnsRequest = new DNSRequest(currentDomain,
DNSService.AAAA);
+ } else {
+ // Get a record for this
+ dnsRequest = new DNSRequest(currentDomain, DNSService.A);
+ }
+
+ spfSession.setAttribute(ATTRIBUTE_CURRENT_DOMAIN,
currentDomain);
+
+ return this.onDNSResponse(DNSResolver.lookup(dnsService,
dnsRequest), spfSession);
+
+ } else {
+ return false;
+ }
+
+ } finally {
+ spfSession.setAttribute(ATTRIBUTE_DOMAIN_LIST, null);
+ spfSession.setAttribute(ATTRIBUTE_CURRENT_DOMAIN, null);
+ }
- /**
- * @see
org.apache.james.jspf.wiring.DNSServiceEnabled#enableDNSService(org.apache.james.jspf.core.DNSService)
- */
- public void enableDNSService(DNSService service) {
- this.dnsService = service;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]