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  
21  package org.apache.james.jspf.executor;
22  
23  import org.apache.james.jspf.core.SPF1Utils;
24  import org.apache.james.jspf.core.SPFSession;
25  import org.apache.james.jspf.core.exceptions.SPFErrorConstants;
26  
27  
28  /**
29   * This class is used to return the result of an SPF lookup.
30   * 
31   */
32  public class SPFResult  {
33  
34      protected String headerTextAsString = "";
35  
36      protected final static String HEADER_NAME = "Received-SPF";
37      
38      protected String result = null;
39  
40      protected String explanation = null;
41      
42      protected SPFResult() {
43          
44      }
45      
46      /**
47       * Construct SPFResult
48       * 
49       * @param spfSession the SPFSession
50       */
51      public SPFResult(SPFSession spfSession) {
52          setSPFSession(spfSession);
53      }
54      
55      /**
56       * Initialize the result.
57       * 
58       * @param spfSession
59       */
60      protected void setSPFSession(SPFSession spfSession) {
61          this.explanation = spfSession.getExplanation();
62          this.result = spfSession.getCurrentResultExpanded();
63          this.headerTextAsString = generateHeader(result, spfSession);
64      }
65  
66      /**
67       * Get the full SPF-Header (headername and headertext)
68       * 
69       * @return SPF-Header
70       */
71      public String getHeader() {
72          return HEADER_NAME+": "+getHeaderText();
73      }
74  
75      /**
76       * Get the SPF-Headername
77       * 
78       * @return headername
79       */
80      public String getHeaderName() {
81          return HEADER_NAME;
82      }
83  
84      /**
85       * Get SPF-Headertext
86       * 
87       * @return headertext
88       */
89      public String getHeaderText() {
90          return headerTextAsString != null ? headerTextAsString : "";
91      }
92  
93      /**
94       * Generate a SPF-Result header
95       * 
96       * @param result The result we should use to generate the header
97       */
98      private String generateHeader(String result, SPFSession spfData) {
99  
100         StringBuffer headerText = new StringBuffer();
101 
102         if (result.equals(SPFErrorConstants.PASS_CONV)) {
103             headerText.append(result + " (spfCheck: domain of "
104                     + spfData.getCurrentDomain() + " designates "
105                     + spfData.getIpAddress() + " as permitted sender) ");
106         } else if (result.equals(SPFErrorConstants.FAIL_CONV)) {
107             headerText.append(result + " (spfCheck: domain of "
108                     + spfData.getCurrentDomain() + " does not designate "
109                     + spfData.getIpAddress() + " as permitted sender) ");
110         } else if (result.equals(SPFErrorConstants.NEUTRAL_CONV)
111                 || result.equals(SPFErrorConstants.NONE_CONV)) {
112             headerText.append(result + " (spfCheck: " + spfData.getIpAddress()
113                     + " is neither permitted nor denied by domain of "
114                     + spfData.getCurrentDomain() + ") ");
115 
116         } else if (result.equals(SPFErrorConstants.SOFTFAIL_CONV)) {
117             headerText.append(result + " (spfCheck: transitioning domain of "
118                     + spfData.getCurrentDomain() + " does not designate "
119                     + spfData.getIpAddress() + " as permitted sender) ");
120         } else if (result.equals(SPFErrorConstants.PERM_ERROR_CONV)) {
121             headerText.append(result
122                     + " (spfCheck: Error in processing SPF Record) ");
123 
124         } else if (result.equals(SPFErrorConstants.TEMP_ERROR_CONV)) {
125             headerText.append(result
126                     + " (spfCheck: Error in retrieving data from DNS) ");
127 
128         }
129 
130         String headerTextAsString;
131         if (headerText.length() > 0) {
132             headerText.append("client-ip=" + spfData.getIpAddress()
133                     + "; envelope-from=" + spfData.getMailFrom() + "; helo="
134                     + spfData.getHostName() + ";");
135             headerTextAsString = headerText.toString();
136         } else {
137             headerTextAsString = "";
138         }
139         return headerTextAsString;
140     }
141 
142     /**
143      * Get the result string
144      * 
145      * @see SPF1Utils
146      * @return result
147      */
148     public String getResult() {
149         return result;
150     }
151 
152     /**
153      * Get the explanation string
154      * If no explanation exists return the empty string
155      * 
156      * @return explanation
157      */
158     public String getExplanation() {
159         return explanation != null ? explanation : "";
160     }
161     
162     
163 }