1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.james.jspf.terms;
22
23 import org.apache.james.jspf.core.DNSLookupContinuation;
24 import org.apache.james.jspf.core.DNSRequest;
25 import org.apache.james.jspf.core.DNSResponse;
26 import org.apache.james.jspf.core.MacroExpand;
27 import org.apache.james.jspf.core.SPFChecker;
28 import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
29 import org.apache.james.jspf.core.SPFSession;
30 import org.apache.james.jspf.core.SPFTermsRegexps;
31 import org.apache.james.jspf.core.exceptions.NeutralException;
32 import org.apache.james.jspf.core.exceptions.NoneException;
33 import org.apache.james.jspf.core.exceptions.PermErrorException;
34 import org.apache.james.jspf.core.exceptions.TempErrorException;
35 import org.apache.james.jspf.core.exceptions.TimeoutException;
36
37 import java.util.List;
38
39
40
41
42 public class ExistsMechanism extends GenericMechanism implements SPFCheckerDNSResponseListener {
43
44 private final class ExpandedChecker implements SPFChecker {
45
46
47
48
49
50 public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
51 TempErrorException, NeutralException, NoneException {
52 String host = expandHost(spfData);
53 return new DNSLookupContinuation(new DNSRequest(host,DNSRequest.A), ExistsMechanism.this);
54 }
55 }
56
57
58
59
60 public static final String REGEX = "[eE][xX][iI][sS][tT][sS]" + "\\:"
61 + SPFTermsRegexps.DOMAIN_SPEC_REGEX;
62
63 private SPFChecker expandedChecker = new ExpandedChecker();
64
65
66
67
68 public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
69 TempErrorException, NeutralException, NoneException {
70
71 spfData.increaseCurrentDepth();
72
73 spfData.pushChecker(expandedChecker);
74 return macroExpand.checkExpand(getDomain(), spfData, MacroExpand.DOMAIN);
75 }
76
77
78
79
80 public DNSLookupContinuation onDNSResponse(DNSResponse response, SPFSession spfSession) throws PermErrorException, TempErrorException {
81 List aRecords;
82
83 try {
84 aRecords = response.getResponse();
85 } catch (TimeoutException e) {
86 spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
87 return null;
88 }
89
90 if (aRecords != null && aRecords.size() > 0) {
91 spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.TRUE);
92 return null;
93 }
94
95
96 spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
97 return null;
98 }
99
100
101
102
103 public String toString() {
104 return "exists:"+getDomain();
105 }
106
107 }