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 ****************************************************************/ 019 020 021 package org.apache.james.jspf.terms; 022 023 import org.apache.james.jspf.core.DNSLookupContinuation; 024 import org.apache.james.jspf.core.Logger; 025 import org.apache.james.jspf.core.SPF1Constants; 026 import org.apache.james.jspf.core.SPFChecker; 027 import org.apache.james.jspf.core.SPFSession; 028 import org.apache.james.jspf.core.exceptions.NeutralException; 029 import org.apache.james.jspf.core.exceptions.NoneException; 030 import org.apache.james.jspf.core.exceptions.PermErrorException; 031 import org.apache.james.jspf.core.exceptions.TempErrorException; 032 033 /** 034 * A Directive is a mechanism with a resulting qualifier. 035 */ 036 public class Directive implements SPFChecker { 037 038 private final class MechanismResultChecker implements SPFChecker { 039 040 /** 041 * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession) 042 */ 043 public DNSLookupContinuation checkSPF(SPFSession spfData) 044 throws PermErrorException, TempErrorException, 045 NeutralException, NoneException { 046 Boolean res = (Boolean) spfData.getAttribute(ATTRIBUTE_MECHANISM_RESULT); 047 if (res != null ? res.booleanValue() : true) { 048 if (qualifier.equals("")) { 049 spfData.setCurrentResult(SPF1Constants.PASS); 050 } else { 051 spfData.setCurrentResult(qualifier); 052 } 053 054 log.info("Processed directive matched: " + Directive.this + " returned " + 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, Logger logger) 081 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) 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 }