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
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 * A Directive is a mechanism with a resulting qualifier.
35 */
36 public class Directive implements SPFChecker {
37
38 private final class MechanismResultChecker implements SPFChecker {
39
40 /**
41 * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
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 * Construct Directive
75 *
76 * @param qualifier The qualifier to use. Valid qualifier are: +, -, ~, ?
77 * @param mechanism The Mechanism
78 * @throws PermErrorException Get thrown if a PermError should returned
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 * Run the Directive
97 *
98 * @param spfSession The SPFSession to use
99 * @return The qualifier which was returned
100 * @throws PermErrorException get thrown if a PermError should returned
101 * @throws TempErrorException get thrown if a TempError should returned
102 * @throws NoneException get thrown if a NoneException should returned;
103 * @throws NeutralException
104 */
105 public DNSLookupContinuation checkSPF(SPFSession spfSession) throws PermErrorException,
106 TempErrorException, NoneException, NeutralException {
107 // if already have a current result we don't run this
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 * Return the Mechanism which should be run
122 *
123 * @return the Mechanism
124 */
125 public Mechanism getMechanism() {
126 return mechanism;
127 }
128
129 /**
130 * Return the Qualifier
131 *
132 * @return the qualifier
133 */
134 public String getQualifier() {
135 return qualifier;
136 }
137
138 public String toString() {
139 return qualifier + mechanism;
140 }
141
142 }