View Javadoc

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.local;
21  
22  import org.apache.james.jspf.core.DNSLookupContinuation;
23  import org.apache.james.jspf.core.Logger;
24  import org.apache.james.jspf.core.MacroExpand;
25  import org.apache.james.jspf.core.SPF1Constants;
26  import org.apache.james.jspf.core.SPF1Record;
27  import org.apache.james.jspf.core.SPF1Utils;
28  import org.apache.james.jspf.core.SPFChecker;
29  import org.apache.james.jspf.core.SPFSession;
30  import org.apache.james.jspf.core.exceptions.NeutralException;
31  import org.apache.james.jspf.core.exceptions.NoneException;
32  import org.apache.james.jspf.core.exceptions.PermErrorException;
33  import org.apache.james.jspf.core.exceptions.TempErrorException;
34  import org.apache.james.jspf.policies.PolicyPostFilter;
35  
36  /**
37   * Policy to add a default explanation
38   */
39  public final class DefaultExplanationPolicy implements PolicyPostFilter {
40  
41      
42      private final class ExplanationChecker implements SPFChecker {
43          
44          /**
45           * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
46           */
47          public DNSLookupContinuation checkSPF(SPFSession spfData)
48                  throws PermErrorException,
49                  NoneException, TempErrorException,
50                  NeutralException {
51              String attExplanation = (String) spfData.getAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION);
52              try {
53                  String explanation = macroExpand.expand(attExplanation, spfData, MacroExpand.EXPLANATION);
54                  
55                  spfData.setExplanation(explanation);
56              } catch (PermErrorException e) {
57                  // Should never happen !
58                  log.debug("Invalid defaulfExplanation: " + attExplanation);
59              }
60              return null;
61          }
62      }
63  
64      private final class DefaultExplanationChecker implements SPFChecker {
65          
66          private SPFChecker explanationCheckr = new ExplanationChecker();
67          
68          /**
69           * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
70           */
71          public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException, NoneException, TempErrorException, NeutralException {
72              
73              if (SPF1Constants.FAIL.equals(spfData.getCurrentResult())) {  
74                  if (spfData.getExplanation()==null || spfData.getExplanation().equals("")) {
75                      String explanation;
76                      if (defExplanation == null) {
77                          explanation = SPF1Utils.DEFAULT_EXPLANATION;
78                      } else {
79                          explanation = defExplanation;
80                      }
81                      spfData.setAttribute(ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION, explanation);
82                      spfData.pushChecker(explanationCheckr);
83                      return macroExpand.checkExpand(explanation, spfData, MacroExpand.EXPLANATION);
84                  }
85              }
86              
87              return null;
88          }
89  
90          public String toString() {
91              if (defExplanation == null) {
92                  return "defaultExplanation";
93              } else {
94                  return "defaultExplanation="+defExplanation;
95              }
96          }
97      }
98  
99      private static final String ATTRIBUTE_DEFAULT_EXPLANATION_POLICY_EXPLANATION = "DefaultExplanationPolicy.explanation";
100     
101     /**
102      * log
103      */
104     private Logger log;
105     /**
106      * the default explanation
107      */
108     private String defExplanation;
109     
110     private MacroExpand macroExpand;
111     
112     /**
113      * @param log the logger
114      * @param explanation the default explanation
115      * @param macroExpand the MacroExpand service
116      */
117     public DefaultExplanationPolicy(Logger log, String explanation, MacroExpand macroExpand) {
118         this.log = log;
119         this.defExplanation = explanation;
120         this.macroExpand = macroExpand;
121     }
122 
123     /**
124      * @see org.apache.james.jspf.policies.PolicyPostFilter#getSPFRecord(java.lang.String, org.apache.james.jspf.core.SPF1Record)
125      */
126     public SPF1Record getSPFRecord(String currentDomain, SPF1Record spfRecord) throws PermErrorException, TempErrorException, NoneException, NeutralException {
127         if (spfRecord == null) return null;
128         // Default explanation policy.
129         spfRecord.getModifiers().add(new DefaultExplanationChecker());
130         return spfRecord;
131     }
132 }