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.executor;
022    
023    import org.apache.james.jspf.core.SPF1Utils;
024    import org.apache.james.jspf.core.SPFSession;
025    import org.apache.james.jspf.core.exceptions.SPFErrorConstants;
026    
027    
028    /**
029     * This class is used to return the result of an SPF lookup.
030     * 
031     */
032    public class SPFResult  {
033    
034        protected String headerTextAsString = "";
035    
036        protected final static String HEADER_NAME = "Received-SPF";
037        
038        protected String result = null;
039    
040        protected String explanation = null;
041        
042        protected SPFResult() {
043            
044        }
045        
046        /**
047         * Construct SPFResult
048         * 
049         * @param spfSession the SPFSession
050         */
051        public SPFResult(SPFSession spfSession) {
052            setSPFSession(spfSession);
053        }
054        
055        /**
056         * Initialize the result.
057         * 
058         * @param spfSession
059         */
060        protected void setSPFSession(SPFSession spfSession) {
061            this.explanation = spfSession.getExplanation();
062            this.result = spfSession.getCurrentResultExpanded();
063            this.headerTextAsString = generateHeader(result, spfSession);
064        }
065    
066        /**
067         * Get the full SPF-Header (headername and headertext)
068         * 
069         * @return SPF-Header
070         */
071        public String getHeader() {
072            return HEADER_NAME+": "+getHeaderText();
073        }
074    
075        /**
076         * Get the SPF-Headername
077         * 
078         * @return headername
079         */
080        public String getHeaderName() {
081            return HEADER_NAME;
082        }
083    
084        /**
085         * Get SPF-Headertext
086         * 
087         * @return headertext
088         */
089        public String getHeaderText() {
090            return headerTextAsString != null ? headerTextAsString : "";
091        }
092    
093        /**
094         * Generate a SPF-Result header
095         * 
096         * @param result The result we should use to generate the header
097         */
098        private String generateHeader(String result, SPFSession spfData) {
099    
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    }