001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.jspf.terms;
019:
020: import org.apache.james.jspf.core.DNSLookupContinuation;
021: import org.apache.james.jspf.core.Logger;
022: import org.apache.james.jspf.core.SPF1Constants;
023: import org.apache.james.jspf.core.SPFChecker;
024: import org.apache.james.jspf.core.SPFSession;
025: import org.apache.james.jspf.core.exceptions.NeutralException;
026: import org.apache.james.jspf.core.exceptions.NoneException;
027: import org.apache.james.jspf.core.exceptions.PermErrorException;
028: import org.apache.james.jspf.core.exceptions.TempErrorException;
029:
030: /**
031: * A Directive is a mechanism with a resulting qualifier.
032: */
033: public class Directive implements SPFChecker {
034:
035: private final class MechanismResultChecker implements SPFChecker {
036:
037: /**
038: * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
039: */
040: public DNSLookupContinuation checkSPF(SPFSession spfData)
041: throws PermErrorException, TempErrorException,
042: NeutralException, NoneException {
043: Boolean res = (Boolean) spfData
044: .getAttribute(ATTRIBUTE_MECHANISM_RESULT);
045: if (res != null ? res.booleanValue() : true) {
046: if (qualifier.equals("")) {
047: spfData.setCurrentResult(SPF1Constants.PASS);
048: } else {
049: spfData.setCurrentResult(qualifier);
050: }
051:
052: log.info("Processed directive matched: "
053: + Directive.this + " returned "
054: + spfData.getCurrentResult());
055: } else {
056: log.debug("Processed directive NOT matched: " + this );
057: }
058: return null;
059: }
060:
061: }
062:
063: public static final String ATTRIBUTE_MECHANISM_RESULT = "Mechanism.result";
064:
065: protected String qualifier = "+";
066:
067: private Mechanism mechanism = null;
068:
069: private Logger log;
070:
071: private MechanismResultChecker resultChecker;
072:
073: /**
074: * Construct Directive
075: *
076: * @param qualifier The qualifier to use. Valid qualifier are: +, -, ~, ?
077: * @param mechanism The Mechanism
078: * @throws PermErrorException Get thrown if a PermError should returned
079: */
080: public Directive(String qualifier, Mechanism mechanism,
081: Logger logger) throws PermErrorException {
082: super ();
083: this .log = logger;
084: if (qualifier == null) {
085: throw new PermErrorException("Qualifier cannot be null");
086: }
087: this .qualifier = qualifier;
088: if (mechanism == null) {
089: throw new PermErrorException("Mechanism cannot be null");
090: }
091: this .resultChecker = new MechanismResultChecker();
092: this .mechanism = mechanism;
093: }
094:
095: /**
096: * Run the Directive
097: *
098: * @param spfSession The SPFSession to use
099: * @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)
106: throws PermErrorException, TempErrorException,
107: NoneException, NeutralException {
108: // if already have a current result we don't run this
109: if (spfSession.getCurrentResult() == null
110: && spfSession.getCurrentResultExpanded() == null) {
111:
112: spfSession.removeAttribute(ATTRIBUTE_MECHANISM_RESULT);
113:
114: spfSession.pushChecker(resultChecker);
115:
116: spfSession.pushChecker(mechanism);
117:
118: }
119: return null;
120: }
121:
122: /**
123: * Return the Mechanism which should be run
124: *
125: * @return the Mechanism
126: */
127: public Mechanism getMechanism() {
128: return mechanism;
129: }
130:
131: /**
132: * Return the Qualifier
133: *
134: * @return the qualifier
135: */
136: public String getQualifier() {
137: return qualifier;
138: }
139:
140: public String toString() {
141: return qualifier + mechanism;
142: }
143:
144: }
|