1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.jspf.policies.local;
21
22 import org.apache.james.jspf.core.Logger;
23 import org.apache.james.jspf.core.SPF1Record;
24 import org.apache.james.jspf.core.SPFRecordParser;
25 import org.apache.james.jspf.core.exceptions.NeutralException;
26 import org.apache.james.jspf.core.exceptions.NoneException;
27 import org.apache.james.jspf.core.exceptions.PermErrorException;
28 import org.apache.james.jspf.core.exceptions.SPFResultException;
29 import org.apache.james.jspf.core.exceptions.TempErrorException;
30 import org.apache.james.jspf.policies.PolicyPostFilter;
31
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.Map;
36
37
38
39
40 public class FallbackPolicy implements PolicyPostFilter {
41
42 private Map entryMap;
43
44 private SPFRecordParser parser;
45
46 private Logger log;
47
48 public FallbackPolicy(Logger log, SPFRecordParser parser) {
49 this.log = log;
50 entryMap = Collections.synchronizedMap(new HashMap());
51 this.parser = parser;
52 }
53
54
55
56
57
58
59
60
61
62
63
64 public void addEntry(String rawHost, String rawSpfRecord)
65 throws IllegalArgumentException {
66 String host;
67 try {
68 log.debug("Start parsing SPF-Record: " + rawSpfRecord);
69 SPF1Record spfRecord = parser.parse(rawSpfRecord);
70 if (rawHost.startsWith("*")) {
71 host = rawHost.substring(1);
72 log.debug("Convert host " + rawHost + " to " + host);
73 } else if (rawHost.endsWith("*")) {
74 int length = rawHost.length();
75 host = rawHost.substring(length - 1, length);
76 log.debug("Convert host " + rawHost + " to " + host);
77 } else {
78 host = rawHost;
79 }
80
81 synchronized (entryMap) {
82 entryMap.put(host, spfRecord);
83 }
84 } catch (SPFResultException e) {
85 throw new IllegalArgumentException("Invalid SPF-Record: "
86 + rawSpfRecord);
87 }
88
89 }
90
91
92
93
94
95 public void clearEntrys() {
96 log.debug("Clear all entries");
97 synchronized (entryMap) {
98 entryMap.clear();
99 }
100 }
101
102
103
104
105
106
107
108 public void removeEntry(String host) {
109 log.debug("Remove fallback entry for host: " + host);
110 synchronized (entryMap) {
111 entryMap.remove(getRawEntry(host));
112 }
113 }
114
115
116
117
118 public SPF1Record getSPFRecord(String currentDomain, SPF1Record res) throws PermErrorException, TempErrorException, NoneException, NeutralException {
119 if (res == null) {
120 return getMySPFRecord(currentDomain);
121 } else {
122 return res;
123 }
124 }
125
126
127
128
129
130
131
132
133
134 protected SPF1Record getMySPFRecord(String host) {
135 Object entry = null;
136
137 synchronized (entryMap) {
138 entry = getRawEntry(host);
139 }
140
141 if (entry != null) {
142 return (SPF1Record) entry;
143 } else {
144 return null;
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157 private Object getRawEntry(String host) {
158 Iterator fallBackIt = entryMap.keySet().iterator();
159
160 while (fallBackIt.hasNext()) {
161 String rawHost = fallBackIt.next().toString();
162
163 if ((rawHost.startsWith(".") && host.startsWith(rawHost))
164 || rawHost.endsWith(".") && host.endsWith(rawHost)) {
165 return entryMap.get(rawHost);
166 }
167 }
168 return null;
169 }
170
171 }