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.terms;
22  
23  import org.apache.james.jspf.core.DNSLookupContinuation;
24  import org.apache.james.jspf.core.DNSRequest;
25  import org.apache.james.jspf.core.DNSResponse;
26  import org.apache.james.jspf.core.MacroExpand;
27  import org.apache.james.jspf.core.SPFChecker;
28  import org.apache.james.jspf.core.SPFCheckerDNSResponseListener;
29  import org.apache.james.jspf.core.SPFSession;
30  import org.apache.james.jspf.core.SPFTermsRegexps;
31  import org.apache.james.jspf.core.exceptions.NeutralException;
32  import org.apache.james.jspf.core.exceptions.NoneException;
33  import org.apache.james.jspf.core.exceptions.PermErrorException;
34  import org.apache.james.jspf.core.exceptions.TempErrorException;
35  import org.apache.james.jspf.core.exceptions.TimeoutException;
36  
37  import java.util.List;
38  
39  /**
40   * This class represent the exists mechanism
41   */
42  public class ExistsMechanism extends GenericMechanism implements SPFCheckerDNSResponseListener {
43  
44      private final class ExpandedChecker implements SPFChecker {
45         
46          /*
47           * (non-Javadoc)
48           * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
49           */
50          public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
51                  TempErrorException, NeutralException, NoneException {
52              String host = expandHost(spfData);
53              return new DNSLookupContinuation(new DNSRequest(host,DNSRequest.A), ExistsMechanism.this);
54          }
55      }
56  
57      /**
58       * ABNF: exists = "exists" ":" domain-spec
59       */
60      public static final String REGEX = "[eE][xX][iI][sS][tT][sS]" + "\\:"
61              + SPFTermsRegexps.DOMAIN_SPEC_REGEX;
62  
63      private SPFChecker expandedChecker = new ExpandedChecker();
64  
65      /**
66       * @see org.apache.james.jspf.core.SPFChecker#checkSPF(org.apache.james.jspf.core.SPFSession)
67       */
68      public DNSLookupContinuation checkSPF(SPFSession spfData) throws PermErrorException,
69              TempErrorException, NeutralException, NoneException {
70          // update currentDepth
71          spfData.increaseCurrentDepth();
72  
73          spfData.pushChecker(expandedChecker);
74          return macroExpand.checkExpand(getDomain(), spfData, MacroExpand.DOMAIN);
75      }
76  
77      /**
78       * @see org.apache.james.jspf.core.SPFCheckerDNSResponseListener#onDNSResponse(org.apache.james.jspf.core.DNSResponse, org.apache.james.jspf.core.SPFSession)
79       */
80      public DNSLookupContinuation onDNSResponse(DNSResponse response, SPFSession spfSession) throws PermErrorException, TempErrorException {
81          List aRecords;
82          
83          try {
84              aRecords = response.getResponse();
85          } catch (TimeoutException e) {
86              spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
87              return null;
88          }
89          
90          if (aRecords != null && aRecords.size() > 0) {
91              spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.TRUE);
92              return null;
93          }
94          
95          // No match found
96          spfSession.setAttribute(Directive.ATTRIBUTE_MECHANISM_RESULT, Boolean.FALSE);
97          return null;
98      }
99  
100     /**
101      * @see java.lang.Object#toString()
102      */
103     public String toString() {
104         return "exists:"+getDomain();
105     }
106 
107 }