1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.jspf.policies;
21
22 import org.apache.james.jspf.core.DNSLookupContinuation;
23 import org.apache.james.jspf.core.SPF1Constants;
24 import org.apache.james.jspf.core.SPFSession;
25 import org.apache.james.jspf.core.SPF1Record;
26 import org.apache.james.jspf.core.SPFChecker;
27 import org.apache.james.jspf.core.exceptions.NeutralException;
28 import org.apache.james.jspf.core.exceptions.NoneException;
29 import org.apache.james.jspf.core.exceptions.PermErrorException;
30 import org.apache.james.jspf.core.exceptions.TempErrorException;
31
32 /**
33 * Sets the result to NEUTRAL if no directive is found
34 */
35 public class NeutralIfNotMatchPolicy implements PolicyPostFilter {
36
37 private final class NeutralIfNotMatchModifier implements SPFChecker {
38
39 /**
40 * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
41 */
42 public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, TempErrorException, NeutralException {
43 // If no match was found set the result to neutral
44 if (spfData.getCurrentResult() == null) {
45 spfData.setCurrentResult(SPF1Constants.NEUTRAL);
46 }
47 return null;
48 }
49
50 public String toString() {
51 return "defaultresult";
52 }
53 }
54
55 /**
56 * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record)
57 */
58 public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, TempErrorException, NoneException, NeutralException {
59 if (spfRecord == null) return null;
60 // Set the result to NEUTRAL if at least a directive is present and it didn't match
61 // Maybe we should simply append a "?all" at the end, as modifier
62 if (spfRecord.getDirectives().size() > 0) {
63 spfRecord.getModifiers().add(new NeutralIfNotMatchModifier());
64 }
65 return spfRecord;
66 }
67 }