Author: bago
Date: Sun Apr 22 05:05:55 2007
New Revision: 531168
URL: http://svn.apache.org/viewvc?view=rev&rev=531168
Log:
Make use of nested classes instead of anonymous classes (simpler to debug, and
reusable between calls).
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/core/Directive.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/AMechanism.java
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/ExpModifier.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/MXMechanism.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/PTRMechanism.java
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/core/Directive.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/core/Directive.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/core/Directive.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/core/Directive.java
Sun Apr 22 05:05:55 2007
@@ -30,6 +30,27 @@
*/
public class Directive implements SPFChecker {
+ private final class MechanismResultChecker implements SPFChecker {
+
+ public void checkSPF(SPFSession spfData)
+ throws PermErrorException, TempErrorException,
+ NeutralException, NoneException {
+ Boolean res = (Boolean)
spfData.getAttribute(ATTRIBUTE_MECHANISM_RESULT);
+ if (res != null ? res.booleanValue() : true) {
+ if (qualifier.equals("")) {
+ spfData.setCurrentResult(SPF1Constants.PASS);
+ } else {
+ spfData.setCurrentResult(qualifier);
+ }
+
+ log.info("Processed directive matched: " + Directive.this + "
returned " + spfData.getCurrentResult());
+ } else {
+ log.debug("Processed directive NOT matched: " + this);
+ }
+ }
+
+ }
+
public static final String ATTRIBUTE_MECHANISM_RESULT = "Mechanism.result";
protected String qualifier = "+";
@@ -38,6 +59,8 @@
private Logger log;
+ private MechanismResultChecker resultChecker;
+
/**
* Construct Directive
*
@@ -56,6 +79,7 @@
if (mechanism == null) {
throw new PermErrorException("Mechanism cannot be null");
}
+ this.resultChecker = new MechanismResultChecker();
this.mechanism = mechanism;
}
@@ -74,37 +98,9 @@
// if already have a current result we don't run this
if (spfData.getCurrentResult() == null) {
- Boolean previous = (Boolean)
spfData.getAttribute(ATTRIBUTE_MECHANISM_RESULT);
spfData.setAttribute(ATTRIBUTE_MECHANISM_RESULT, null);
- spfData.pushChecker(new SPFChecker() {
-
- private Boolean previous;
-
- public void checkSPF(SPFSession spfData)
- throws PermErrorException, TempErrorException,
- NeutralException, NoneException {
- Boolean res = (Boolean)
spfData.getAttribute(ATTRIBUTE_MECHANISM_RESULT);
- if (res != null ? res.booleanValue() : true) {
- if (qualifier.equals("")) {
- spfData.setCurrentResult(SPF1Constants.PASS);
- } else {
- spfData.setCurrentResult(qualifier);
- }
-
- log.info("Processed directive matched: " +
Directive.this + " returned " + spfData.getCurrentResult());
- } else {
- log.debug("Processed directive NOT matched: " + this);
- }
- spfData.setAttribute(ATTRIBUTE_MECHANISM_RESULT, previous);
- }
-
- public SPFChecker setPrevious(Boolean previous) {
- this.previous = previous;
- return this;
- }
-
- }.setPrevious(previous));
+ spfData.pushChecker(resultChecker);
spfData.pushChecker(mechanism);
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/NeutralIfNotMatchPolicy.java
Sun Apr 22 05:05:55 2007
@@ -33,6 +33,19 @@
*/
public class NeutralIfNotMatchPolicy implements PolicyPostFilter {
+ private final class NeutralIfNotMatchModifier implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
TempErrorException, NeutralException {
+ // If no match was found set the result to neutral
+ if (spfData.getCurrentResult() == null) {
+ spfData.setCurrentResult(SPF1Constants.NEUTRAL);
+ }
+ }
+
+ public String toString() {
+ return "defaultresult";
+ }
+ }
+
/**
* @see
org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String,
org.apache.james.jspf.core.SPF1Record)
*/
@@ -41,18 +54,7 @@
// Set the result to NEUTRAL if at least a directive is present and it
didn't match
// Maybe we should simply append a "?all" at the end, as modifier
if (spfRecord.getDirectives().size() > 0) {
- spfRecord.getModifiers().add(new SPFChecker() {
- public void checkSPF(SPFSession spfData) throws
PermErrorException, TempErrorException, NeutralException {
- // If no match was found set the result to neutral
- if (spfData.getCurrentResult() == null) {
- spfData.setCurrentResult(SPF1Constants.NEUTRAL);
- }
- }
-
- public String toString() {
- return "defaultresult";
- }
- });
+ spfRecord.getModifiers().add(new NeutralIfNotMatchModifier());
}
return spfRecord;
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/policies/local/DefaultExplanationPolicy.java
Sun Apr 22 05:05:55 2007
@@ -38,6 +38,58 @@
* Policy to add a default explanation
*/
public final class DefaultExplanationPolicy implements PolicyPostFilter {
+
+ private final class DefaultExplanationChecker implements SPFChecker {
+
+ private SPFChecker explanationCheckr;
+
+ public DefaultExplanationChecker() {
+ this.explanationCheckr = new ExplanationChecker();
+ }
+
+ private final class ExplanationChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData)
+ throws PermErrorException,
+ NoneException, TempErrorException,
+ NeutralException {
+ String attExplanation = (String)
spfData.getAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION);
+ try {
+ String explanation = macroExpand.expand(attExplanation,
spfData, MacroExpand.EXPLANATION);
+
+ spfData.setExplanation(explanation);
+ } catch (PermErrorException e) {
+ // Should never happen !
+ log.debug("Invalid defaulfExplanation: " + attExplanation);
+ }
+ }
+ }
+
+ public void checkSPF(SPFSession 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;
+ }
+
spfData.setAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION,
explanation);
+ spfData.pushChecker(explanationCheckr);
+ DNSResolver.hostExpand(dnsService, macroExpand,
explanation, spfData, MacroExpand.EXPLANATION);
+ }
+ }
+ }
+
+ public String toString() {
+ if (defExplanation == null) {
+ return "defaultExplanation";
+ } else {
+ return "defaultExplanation="+defExplanation;
+ }
+ }
+ }
+
private static final String
ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION =
"DefaultExplanationPolicy.explanation";
/**
@@ -70,49 +122,7 @@
public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord)
throws PermErrorException, TempErrorException, NoneException, NeutralException {
if (spfRecord == null) return null;
// Default explanation policy.
- spfRecord.getModifiers().add(new SPFChecker() {
- public void checkSPF(SPFSession 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;
- }
-
spfData.setAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION,
explanation);
- spfData.pushChecker(new SPFChecker() {
-
- public void checkSPF(SPFSession spfData)
- throws PermErrorException,
- NoneException, TempErrorException,
- NeutralException {
- String attExplanation = (String)
spfData.getAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION);
- try {
- String explanation =
macroExpand.expand(attExplanation, spfData, MacroExpand.EXPLANATION);
-
- spfData.setExplanation(explanation);
- } catch (PermErrorException e) {
- // Should never happen !
- log.debug("Invalid defaulfExplanation: " +
attExplanation);
- }
- }
-
- });
- DNSResolver.hostExpand(dnsService, macroExpand,
explanation, spfData, MacroExpand.EXPLANATION);
- }
- }
- }
-
- public String toString() {
- if (defExplanation == null) {
- return "defaultExplanation";
- } else {
- return "defaultExplanation="+defExplanation;
- }
- }
- });
+ spfRecord.getModifiers().add(new DefaultExplanationChecker());
return spfRecord;
}
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/AMechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/AMechanism.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/AMechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/AMechanism.java
Sun Apr 22 05:05:55 2007
@@ -62,6 +62,48 @@
private int ip6cidr;
protected DNSService dnsService;
+
+ private SPFChecker expandedChecker = new ExpandedChecker();
+
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+ // Get the right host.
+ String host = expandHost(spfData);
+
+ // get the ipAddress
+ try {
+ boolean validIPV4Address =
Inet6Util.isValidIPV4Address(spfData.getIpAddress());
+ spfData.setAttribute(ATTRIBUTE_AMECHANISM_IPV4CHECK,
Boolean.valueOf(validIPV4Address));
+ if (validIPV4Address) {
+
+ List aRecords = getARecords(dnsService,host);
+ if (aRecords == null) {
+ DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.A), spfData, AMechanism.this);
+ } else {
+ onDNSResponse(new DNSResponse(aRecords), spfData);
+ }
+
+ } else {
+
+ List aaaaRecords = getAAAARecords(dnsService, host);
+ if (aaaaRecords == null) {
+ DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.AAAA), spfData, AMechanism.this);
+ } else {
+ onDNSResponse(new DNSResponse(aaaaRecords), spfData);
+ }
+
+ }
+ // PermError / TempError
+ // TODO: Should we replace this with the "right" Exceptions ?
+ } catch (Exception e) {
+ log.debug("No valid ipAddress: ",e);
+ throw new PermErrorException("No valid ipAddress: "
+ + spfData.getIpAddress());
+ }
+
+ }
+ }
/**
* @see
org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
@@ -70,48 +112,8 @@
// update currentDepth
spfData.increaseCurrentDepth();
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
- // Get the right host.
- String host = expandHost(spfData);
-
- // get the ipAddress
- try {
- boolean validIPV4Address =
Inet6Util.isValidIPV4Address(spfData.getIpAddress());
- spfData.setAttribute(ATTRIBUTE_AMECHANISM_IPV4CHECK,
Boolean.valueOf(validIPV4Address));
- if (validIPV4Address) {
-
- List aRecords = getARecords(dnsService,host);
- if (aRecords == null) {
- DNSResolver.lookup(dnsService, new
DNSRequest(host, DNSService.A), spfData, AMechanism.this);
- } else {
- onDNSResponse(new DNSResponse(aRecords), spfData);
- }
-
- } else {
-
- List aaaaRecords = getAAAARecords(dnsService, host);
- if (aaaaRecords == null) {
- DNSResolver.lookup(dnsService, new
DNSRequest(host, DNSService.AAAA), spfData, AMechanism.this);
- } else {
- onDNSResponse(new DNSResponse(aaaaRecords),
spfData);
- }
-
- }
- // PermError / TempError
- // TODO: Should we replace this with the "right" Exceptions ?
- } catch (Exception e) {
- log.debug("No valid ipAddress: ",e);
- throw new PermErrorException("No valid ipAddress: "
- + spfData.getIpAddress());
- }
-
- }
-
- };
- spfData.pushChecker(checker);
+ spfData.pushChecker(expandedChecker);
+
DNSResolver.hostExpand(dnsService, macroExpand, getDomain(), spfData,
MacroExpand.DOMAIN);
}
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=531168&r1=531167&r2=531168
==============================================================================
---
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
Sun Apr 22 05:05:55 2007
@@ -43,6 +43,14 @@
*/
public class ExistsMechanism extends GenericMechanism implements
DNSServiceEnabled, SPFCheckerDNSResponseListener {
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+ String host = expandHost(spfData);
+ DNSResolver.lookup(dnsService, new DNSRequest(host,DNSService.A),
spfData, ExistsMechanism.this);
+ }
+ }
+
/**
* ABNF: exists = "exists" ":" domain-spec
*/
@@ -51,6 +59,8 @@
private DNSService dnsService;
+ private SPFChecker expandedChecker = new ExpandedChecker();
+
/**
* @see
org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
*/
@@ -59,16 +69,7 @@
// update currentDepth
spfData.increaseCurrentDepth();
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
- String host = expandHost(spfData);
- DNSResolver.lookup(dnsService, new
DNSRequest(host,DNSService.A), spfData, ExistsMechanism.this);
- }
-
- };
- spfData.pushChecker(checker);
+ spfData.pushChecker(expandedChecker);
DNSResolver.hostExpand(dnsService, macroExpand, getDomain(), spfData,
MacroExpand.DOMAIN);
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExpModifier.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/ExpModifier.java
Sun Apr 22 05:05:55 2007
@@ -45,6 +45,31 @@
*/
public class ExpModifier extends GenericModifier implements DNSServiceEnabled,
MacroExpandEnabled, SPFCheckerDNSResponseListener {
+ private final class ExpandedExplanationChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData)
+ throws PermErrorException, NoneException,
+ TempErrorException, NeutralException {
+ try {
+ String exp = (String)
spfData.getAttribute(ATTRIBUTE_EXPAND_EXPLANATION);
+ String expandedExplanation = macroExpand.expand(exp, spfData,
MacroExpand.EXPLANATION);
+ spfData.setExplanation(expandedExplanation);
+ } catch (PermErrorException e) {
+ // ignore syntax error on explanation expansion
+ }
+ }
+ }
+
+
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ NoneException, TempErrorException, NeutralException {
+ String host = macroExpand.expand(getHost(), spfData,
MacroExpand.DOMAIN);
+
+ DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.TXT), spfData, ExpModifier.this);
+ }
+ }
+
+
private static final String ATTRIBUTE_EXPAND_EXPLANATION =
"ExpModifier.ExpandExplanation";
/**
@@ -61,6 +86,10 @@
private MacroExpand macroExpand;
+ private ExpandedChecker expandedChecker = new ExpandedChecker();
+
+ private ExpandedExplanationChecker expandedExplanationChecker = new
ExpandedExplanationChecker();
+
/**
* Generate the explanation and set it in SPF1Data so it can be accessed
* easy later if needed
@@ -89,16 +118,7 @@
if (spfData.getCurrentResult()== null ||
!spfData.getCurrentResult().equals(SPF1Constants.FAIL))
return;
- spfData.pushChecker(new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- NoneException, TempErrorException, NeutralException {
- String host = macroExpand.expand(getHost(), spfData,
MacroExpand.DOMAIN);
-
- DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.TXT), spfData, ExpModifier.this);
- }
-
- });
+ spfData.pushChecker(expandedChecker);
DNSResolver.hostExpand(dnsService, macroExpand, host, spfData,
MacroExpand.DOMAIN);
}
@@ -148,21 +168,7 @@
if ((exp != null) && (!exp.equals(""))) {
try {
- spfData.pushChecker(new SPFChecker() {
-
- public void checkSPF(SPFSession spfData)
- throws PermErrorException, NoneException,
- TempErrorException, NeutralException {
- try {
- String exp = (String)
spfData.getAttribute(ATTRIBUTE_EXPAND_EXPLANATION);
- String expandedExplanation =
macroExpand.expand(exp, spfData, MacroExpand.EXPLANATION);
-
spfData.setExplanation(expandedExplanation);
- } catch (PermErrorException e) {
- // ignore syntax error on explanation
expansion
- }
- }
-
- });
+ spfData.pushChecker(expandedExplanationChecker);
DNSResolver.hostExpand(dnsService, macroExpand, exp,
spfData, MacroExpand.EXPLANATION);
} catch (PermErrorException e) {
// ignore syntax error on explanation expansion
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=531168&r1=531167&r2=531168
==============================================================================
---
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
Sun Apr 22 05:05:55 2007
@@ -49,6 +49,123 @@
*/
public class IncludeMechanism implements Mechanism, Configurable, LogEnabled,
SPFCheckEnabled, MacroExpandEnabled, DNSServiceEnabled {
+ private final class ExceptionCatcher implements SPFCheckerExceptionCatcher
{
+ private SPFChecker spfChecker;
+
+ private SPFChecker finallyChecker;
+
+ public void onException(Exception exception, SPFSession session)
+ throws PermErrorException, NoneException,
+ TempErrorException, NeutralException {
+
+ // remove every checker until the initialized one
+ SPFChecker checker;
+ while ((checker = session.popChecker())!=spfChecker) {
+ log.debug("Redurect resulted in exception. Removing checker:
"+checker);
+ }
+
+ finallyChecker.checkSPF(session);
+
+ if (exception instanceof NeutralException) {
+ throw new PermErrorException("included checkSPF returned
NeutralException");
+ } else if (exception instanceof NoneException) {
+ throw new PermErrorException("included checkSPF returned
NoneException");
+ } else if (exception instanceof PermErrorException){
+ throw (PermErrorException) exception;
+ } else if (exception instanceof TempErrorException){
+ throw (TempErrorException) exception;
+ } else if (exception instanceof RuntimeException){
+ throw (RuntimeException) exception;
+ } else {
+ throw new IllegalStateException(exception);
+ }
+ }
+
+ public SPFCheckerExceptionCatcher setExceptionHandlerChecker(
+ SPFChecker checker, SPFChecker finallyChecker) {
+ this.spfChecker = checker;
+ this.finallyChecker = finallyChecker;
+ return this;
+ }
+ }
+
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException {
+
+ // throws a PermErrorException that we can pass through
+ String host = macroExpand.expand(getHost(), spfData,
MacroExpand.DOMAIN);
+
+ spfData.setCurrentDomain(host);
+
+ // On includes we should not use the explanation of the included
domain
+ spfData.setIgnoreExplanation(true);
+ // set a null current result
+ spfData.setCurrentResult(null);
+
+ spfData.pushChecker(spfChecker);
+ }
+ }
+
+ private final class FinallyChecker implements SPFChecker {
+ private String previousResult;
+
+ private String previousDomain;
+
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+
+ spfData.setIgnoreExplanation(false);
+ spfData.setCurrentDomain(previousDomain);
+ spfData.setCurrentResult(previousResult);
+
+ spfData.popExceptionCatcher();
+
+ }
+
+ public SPFChecker init(SPFSession spfSession) {
+
+ // TODO understand what exactly we have to do now that spfData is
a session
+ // and contains much more than the input data.
+ // do we need to create a new session at all?
+ // do we need to backup the session attributes and restore them?
+ this.previousResult = spfSession.getCurrentResult();
+ this.previousDomain = spfSession.getCurrentDomain();
+ return this;
+ }
+ }
+
+ private final class CleanupAndResultChecker implements SPFChecker {
+ private SPFChecker finallyChecker;
+
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+
+ String currentResult = spfData.getCurrentResult();
+
+ finallyChecker.checkSPF(spfData);
+
+ if (currentResult == null) {
+ throw new TempErrorException("included checkSPF returned
null");
+ } else if (currentResult.equals(SPF1Constants.PASS)) {
+ // TODO this won't work asynchronously
+ spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT,
Boolean.TRUE);
+ } else if (currentResult.equals(SPF1Constants.FAIL) ||
currentResult.equals(SPF1Constants.SOFTFAIL) ||
currentResult.equals(SPF1Constants.NEUTRAL)) {
+ // TODO this won't work asynchronously
+ spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT,
Boolean.FALSE);
+ } else {
+ throw new TempErrorException("included checkSPF returned an
Illegal result");
+ }
+
+
+ }
+
+ public SPFChecker init(SPFChecker finallyChecker) {
+ this.finallyChecker = finallyChecker;
+ return this;
+ }
+ }
+
/**
* ABNF: include = "include" ":" domain-spec
*/
@@ -72,139 +189,15 @@
// update currentDepth
spfData.increaseCurrentDepth();
- SPFChecker finallyChecker = new SPFChecker() {
-
- private String previousResult;
- private String previousDomain;
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
-
- spfData.setIgnoreExplanation(false);
- spfData.setCurrentDomain(previousDomain);
- spfData.setCurrentResult(previousResult);
-
- spfData.popExceptionCatcher();
-
- }
-
-
- public SPFChecker init(SPFSession spfSession) {
-
- // TODO understand what exactly we have to do now that spfData
is a session
- // and contains much more than the input data.
- // do we need to create a new session at all?
- // do we need to backup the session attributes and restore
them?
- this.previousResult = spfSession.getCurrentResult();
- this.previousDomain = spfSession.getCurrentDomain();
- return this;
- }
-
- }.init(spfData);
-
- SPFChecker cleanupAndResultHandler = new SPFChecker() {
-
- private SPFChecker finallyChecker;
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
-
- String currentResult = spfData.getCurrentResult();
-
- finallyChecker.checkSPF(spfData);
-
- if (currentResult == null) {
- throw new TempErrorException("included checkSPF returned
null");
- } else if (currentResult.equals(SPF1Constants.PASS)) {
- // TODO this won't work asynchronously
- spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT,
Boolean.TRUE);
- } else if (currentResult.equals(SPF1Constants.FAIL) ||
currentResult.equals(SPF1Constants.SOFTFAIL) ||
currentResult.equals(SPF1Constants.NEUTRAL)) {
- // TODO this won't work asynchronously
- spfData.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT,
Boolean.FALSE);
- } else {
- throw new TempErrorException("included checkSPF returned
an Illegal result");
- }
-
-
- }
-
- public SPFChecker init(SPFChecker finallyChecker) {
- this.finallyChecker = finallyChecker;
- return this;
- }
-
- }.init(finallyChecker);
+ SPFChecker finallyChecker = new FinallyChecker().init(spfData);
+ SPFChecker cleanupAndResultHandler = new
CleanupAndResultChecker().init(finallyChecker);
spfData.pushChecker(cleanupAndResultHandler);
+ spfData.pushExceptionCatcher(new
ExceptionCatcher().setExceptionHandlerChecker(cleanupAndResultHandler,
finallyChecker));
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException {
-
- // throws a PermErrorException that we can pass through
- String host = macroExpand.expand(getHost(), spfData,
MacroExpand.DOMAIN);
-
- spfData.setCurrentDomain(host);
-
- // On includes we should not use the explanation of the
included domain
- spfData.setIgnoreExplanation(true);
- // set a null current result
- spfData.setCurrentResult(null);
-
- spfData.pushChecker(spfChecker);
- }
-
- };
-
- spfData.pushExceptionCatcher(new SPFCheckerExceptionCatcher() {
-
- private SPFChecker spfChecker;
- private SPFChecker finallyChecker;
-
- public void onException(Exception exception, SPFSession session)
- throws PermErrorException, NoneException,
- TempErrorException, NeutralException {
-
- // remove every checker until the initialized one
- SPFChecker checker;
- while ((checker = session.popChecker())!=spfChecker) {
- log.debug("Redurect resulted in exception. Removing
checker: "+checker);
- }
-
- finallyChecker.checkSPF(session);
-
- if (exception instanceof NeutralException) {
- throw new PermErrorException("included checkSPF returned
NeutralException");
- } else if (exception instanceof NoneException) {
- throw new PermErrorException("included checkSPF returned
NoneException");
- } else if (exception instanceof PermErrorException){
- throw (PermErrorException) exception;
- } else if (exception instanceof TempErrorException){
- throw (TempErrorException) exception;
- } else if (exception instanceof RuntimeException){
- throw (RuntimeException) exception;
- } else {
- throw new IllegalStateException(exception);
- }
- }
-
- public SPFCheckerExceptionCatcher setExceptionHandlerChecker(
- SPFChecker checker, SPFChecker finallyChecker) {
- this.spfChecker = checker;
- this.finallyChecker = finallyChecker;
- return this;
- }
-
- }.setExceptionHandlerChecker(cleanupAndResultHandler, finallyChecker));
-
- // TODO check if this is ok. I removed the catch and all tests still
pass.
-// try {
- spfData.pushChecker(checker);
+ spfData.pushChecker(new ExpandedChecker());
DNSResolver.hostExpand(dnsService, macroExpand, getHost(), spfData,
MacroExpand.DOMAIN);
-// } catch (NeutralException e) {
-// // catch neutral exception.
-// }
+
}
/**
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/MXMechanism.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/MXMechanism.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/MXMechanism.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/MXMechanism.java
Sun Apr 22 05:05:55 2007
@@ -45,6 +45,17 @@
*/
public class MXMechanism extends AMechanism implements
SPFCheckerDNSResponseListener {
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+
+ // Get the right host.
+ String host = expandHost(spfData);
+
+ DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.MX), spfData, MXMechanism.this);
+ }
+ }
+
private static final String ATTRIBUTE_MX_RECORDS = "MXMechanism.mxRecords";
private static final String ATTRIBUTE_CHECK_RECORDS =
"MXMechanism.checkRecords";
/**
@@ -54,6 +65,8 @@
+ SPFTermsRegexps.DOMAIN_SPEC_REGEX + ")?" + "(?:"
+ DUAL_CIDR_LENGTH_REGEX + ")?";
+ private SPFChecker expandedChecker = new ExpandedChecker();
+
/**
* @see
org.apache.james.jspf.terms.AMechanism#checkSPF(org.apache.james.jspf.core.SPFSession)
*/
@@ -63,20 +76,7 @@
// update currentDepth
spfData.increaseCurrentDepth();
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
-
- // Get the right host.
- String host = expandHost(spfData);
-
- DNSResolver.lookup(dnsService, new DNSRequest(host,
DNSService.MX), spfData, MXMechanism.this);
- }
-
- };
-
- spfData.pushChecker(checker);
+ spfData.pushChecker(expandedChecker);
DNSResolver.hostExpand(dnsService, macroExpand, getDomain(), spfData,
MacroExpand.DOMAIN);
}
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=531168&r1=531167&r2=531168
==============================================================================
---
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
Sun Apr 22 05:05:55 2007
@@ -45,6 +45,22 @@
*/
public class PTRMechanism extends GenericMechanism implements
DNSServiceEnabled, SPFCheckerDNSResponseListener {
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData) throws PermErrorException,
+ TempErrorException, NeutralException, NoneException {
+
+ // Get PTR Records for the ipAddress which is provided by SPF1Data
+ IPAddr ip = IPAddr.getAddress(spfData.getIpAddress());
+
+ // Get the right host.
+ String host = expandHost(spfData);
+
+ spfData.setAttribute(ATTRIBUTE_EXPANDED_HOST, host);
+
+ DNSResolver.lookup(dnsService, new DNSRequest(ip.getReverseIP(),
DNSService.PTR), spfData, PTRMechanism.this);
+ }
+ }
+
private static final String ATTRIBUTE_CURRENT_DOMAIN =
"PTRMechanism.currentDomain";
private static final String ATTRIBUTE_EXPANDED_HOST =
"PTRMechanism.expandedHost";
@@ -59,6 +75,8 @@
private DNSService dnsService;
+ private SPFChecker expandedChecker = new ExpandedChecker();
+
/**
* @see
org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
*/
@@ -67,26 +85,7 @@
// update currentDepth
spfData.increaseCurrentDepth();
-
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData) throws PermErrorException,
- TempErrorException, NeutralException, NoneException {
-
- // Get PTR Records for the ipAddress which is provided by
SPF1Data
- IPAddr ip = IPAddr.getAddress(spfData.getIpAddress());
-
- // Get the right host.
- String host = expandHost(spfData);
-
- spfData.setAttribute(ATTRIBUTE_EXPANDED_HOST, host);
-
- DNSResolver.lookup(dnsService, new
DNSRequest(ip.getReverseIP(), DNSService.PTR), spfData, PTRMechanism.this);
- }
-
- };
-
- spfData.pushChecker(checker);
+ spfData.pushChecker(expandedChecker);
DNSResolver.hostExpand(dnsService, macroExpand, getDomain(), spfData,
MacroExpand.DOMAIN);
}
Modified:
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java
URL:
http://svn.apache.org/viewvc/james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java?view=diff&rev=531168&r1=531167&r2=531168
==============================================================================
---
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java
(original)
+++
james/jspf/branches/asynch-jspf/src/main/java/org/apache/james/jspf/terms/RedirectModifier.java
Sun Apr 22 05:05:55 2007
@@ -41,6 +41,79 @@
public class RedirectModifier extends GenericModifier implements
SPFCheckEnabled, MacroExpandEnabled, DNSServiceEnabled {
+ private final class ExceptionCatcher implements SPFCheckerExceptionCatcher
{
+ private SPFChecker spfChecker;
+
+ private SPFChecker finallyChecker;
+
+ public ExceptionCatcher(SPFChecker spfChecker,
+ SPFChecker finallyChecker) {
+ this.spfChecker = spfChecker;
+ this.finallyChecker = finallyChecker;
+ }
+
+ public void onException(Exception exception, SPFSession session)
+ throws PermErrorException, NoneException,
+ TempErrorException, NeutralException {
+
+ finallyChecker.checkSPF(session);
+
+ // remove every checker until the initialized one
+ SPFChecker checker;
+ while ((checker = session.popChecker())!=spfChecker) {
+ log.debug("Redurect resulted in exception. Removing checker:
"+checker);
+ }
+
+ if (exception instanceof NeutralException) {
+ throw new PermErrorException(
+ "included checkSPF returned NeutralException");
+
+ } else if (exception instanceof NoneException) {
+ // no spf record assigned to the redirect domain
+ throw new PermErrorException(
+ "included checkSPF returned NoneException");
+ } else if (exception instanceof PermErrorException){
+ throw (PermErrorException) exception;
+ } else if (exception instanceof TempErrorException){
+ throw (TempErrorException) exception;
+ } else if (exception instanceof RuntimeException){
+ throw (RuntimeException) exception;
+ } else {
+ throw new IllegalStateException(exception);
+ }
+ }
+
+ }
+
+ private final class ExpandedChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData)
+ throws PermErrorException, NoneException,
+ TempErrorException, NeutralException {
+ String host = getHost();
+
+ // throws a PermErrorException that we can pass
+ // through
+ host = macroExpand.expand(host, spfData,
+ MacroExpand.DOMAIN);
+
+ spfData.setCurrentDomain(host);
+
+ spfData.pushChecker(spfChecker);
+ }
+ }
+
+ private final class CleanupChecker implements SPFChecker {
+ public void checkSPF(SPFSession spfData)
+ throws PermErrorException, TempErrorException,
+ NeutralException, NoneException {
+ // After the redirect we should not use the
+ // explanation from the orginal record
+ spfData.setIgnoreExplanation(true);
+
+ spfData.popExceptionCatcher();
+ }
+ }
+
/**
* ABNF: redirect = "redirect" "=" domain-spec
*/
@@ -53,6 +126,12 @@
private DNSService dnsService;
+ private SPFChecker cleanupChecker = new CleanupChecker();
+
+ private SPFChecker expandedChecker = new ExpandedChecker();
+
+ private ExceptionCatcher exceptionCatcher = new
ExceptionCatcher(cleanupChecker, cleanupChecker);
+
/**
* Set the host which should be used for redirection and set it in SPF1Data
* so it can be accessed easy later if needed
@@ -76,88 +155,11 @@
// update currentDepth
spfData.increaseCurrentDepth();
- SPFChecker cleanupHandler = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData)
- throws PermErrorException, TempErrorException,
- NeutralException, NoneException {
- // After the redirect we should not use the
- // explanation from the orginal record
- spfData.setIgnoreExplanation(true);
-
- spfData.popExceptionCatcher();
- }
-
- };
-
- spfData.pushChecker(cleanupHandler);
+ spfData.pushChecker(cleanupChecker);
- SPFChecker checker = new SPFChecker() {
-
- public void checkSPF(SPFSession spfData)
- throws PermErrorException, NoneException,
- TempErrorException, NeutralException {
- String host = getHost();
-
- // throws a PermErrorException that we can pass
- // through
- host = macroExpand.expand(host, spfData,
- MacroExpand.DOMAIN);
-
- spfData.setCurrentDomain(host);
-
- spfData.pushChecker(spfChecker);
- }
-
- };
-
- spfData.pushExceptionCatcher(new SPFCheckerExceptionCatcher() {
-
- private SPFChecker spfChecker;
- private SPFChecker finallyChecker;
-
- public void onException(Exception exception, SPFSession
session)
- throws PermErrorException, NoneException,
- TempErrorException, NeutralException {
-
- finallyChecker.checkSPF(session);
-
- // remove every checker until the initialized one
- SPFChecker checker;
- while ((checker = session.popChecker())!=spfChecker) {
- log.debug("Redurect resulted in exception. Removing
checker: "+checker);
- }
-
- if (exception instanceof NeutralException) {
- throw new PermErrorException(
- "included checkSPF returned NeutralException");
-
- } else if (exception instanceof NoneException) {
- // no spf record assigned to the redirect domain
- throw new PermErrorException(
- "included checkSPF returned NoneException");
- } else if (exception instanceof PermErrorException){
- throw (PermErrorException) exception;
- } else if (exception instanceof TempErrorException){
- throw (TempErrorException) exception;
- } else if (exception instanceof RuntimeException){
- throw (RuntimeException) exception;
- } else {
- throw new IllegalStateException(exception);
- }
- }
-
- public SPFCheckerExceptionCatcher setExceptionHandlerChecker(
- SPFChecker checker, SPFChecker finallyChecker) {
- this.spfChecker = checker;
- this.finallyChecker = finallyChecker;
- return this;
- }
-
- }.setExceptionHandlerChecker(cleanupHandler, cleanupHandler));
-
+ spfData.pushExceptionCatcher(exceptionCatcher);
- spfData.pushChecker(checker);
+ spfData.pushChecker(expandedChecker);
DNSResolver.hostExpand(dnsService, macroExpand, getHost(), spfData,
MacroExpand.DOMAIN);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]