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.Logger;
25 import org.apache.james.jspf.core.SPF1Constants;
26 import org.apache.james.jspf.core.SPFChecker;
27 import org.apache.james.jspf.core.SPFSession;
28 import org.apache.james.jspf.core.exceptions.NeutralException;
29 import org.apache.james.jspf.core.exceptions.NoneException;
30 import org.apache.james.jspf.core.exceptions.PermErrorException;
31 import org.apache.james.jspf.core.exceptions.TempErrorException;
32
33
34
35
36 public class Directive implements SPFChecker {
37
38 private final class MechanismResultChecker implements SPFChecker {
39
40
41
42
43 public DNSLookupContinuation checkSPF(SPFSession spfData)
44 throws PermErrorException, TempErrorException,
45 NeutralException, NoneException {
46 Boolean res = (Boolean) spfData.getAttribute(ATTRIBUTE_MECHANISM_RESULT);
47 if (res != null ? res.booleanValue() : true) {
48 if (qualifier.equals("")) {
49 spfData.setCurrentResult(SPF1Constants.PASS);
50 } else {
51 spfData.setCurrentResult(qualifier);
52 }
53
54 log.info("Processed directive matched: " + Directive.this + " returned " + spfData.getCurrentResult());
55 } else {
56 log.debug("Processed directive NOT matched: " + this);
57 }
58 return null;
59 }
60
61 }
62
63 public static final String ATTRIBUTE_MECHANISM_RESULT = "Mechanism.result";
64
65 protected String qualifier = "+";
66
67 private Mechanism mechanism = null;
68
69 private Logger log;
70
71 private MechanismResultChecker resultChecker;
72
73
74
75
76
77
78
79
80 public Directive(String qualifier, Mechanism mechanism, Logger logger)
81 throws PermErrorException {
82 super();
83 this.log = logger;
84 if (qualifier == null) {
85 throw new PermErrorException("Qualifier cannot be null");
86 }
87 this.qualifier = qualifier;
88 if (mechanism == null) {
89 throw new PermErrorException("Mechanism cannot be null");
90 }
91 this.resultChecker = new MechanismResultChecker();
92 this.mechanism = mechanism;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public DNSLookupContinuation checkSPF(SPFSession spfSession) throws PermErrorException,
106 TempErrorException, NoneException, NeutralException {
107
108 if (spfSession.getCurrentResult() == null && spfSession.getCurrentResultExpanded() == null) {
109
110 spfSession.removeAttribute(ATTRIBUTE_MECHANISM_RESULT);
111
112 spfSession.pushChecker(resultChecker);
113
114 spfSession.pushChecker(mechanism);
115
116 }
117 return null;
118 }
119
120
121
122
123
124
125 public Mechanism getMechanism() {
126 return mechanism;
127 }
128
129
130
131
132
133
134 public String getQualifier() {
135 return qualifier;
136 }
137
138 public String toString() {
139 return qualifier + mechanism;
140 }
141
142 }