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.LogEnabled;
24  import org.apache.james.jspf.core.Logger;
25  import org.apache.james.jspf.core.MacroExpand;
26  import org.apache.james.jspf.core.MacroExpandEnabled;
27  import org.apache.james.jspf.core.SPFSession;
28  import org.apache.james.jspf.core.exceptions.PermErrorException;
29  
30  /**
31   * This abstract class represent a gerneric mechanism
32   *  
33   */
34  public abstract class GenericMechanism implements Mechanism, ConfigurationEnabled, LogEnabled, MacroExpandEnabled {
35  
36      /**
37       * ABNF: ip4-cidr-length = "/" 1*DIGIT
38       */
39      protected static final String IP4_CIDR_LENGTH_REGEX = "/(\\d+)";
40  
41      /**
42       * ABNF: ip6-cidr-length = "/" 1*DIGIT
43       */
44      protected static final String IP6_CIDR_LENGTH_REGEX = "/(\\d+)";
45  
46      /**
47       * ABNF: dual-cidr-length = [ ip4-cidr-length ] [ "/" ip6-cidr-length ]
48       */
49      protected static final String DUAL_CIDR_LENGTH_REGEX = "(?:"
50              + IP4_CIDR_LENGTH_REGEX + ")?" + "(?:/" + IP6_CIDR_LENGTH_REGEX
51              + ")?";
52  
53      private String domain;
54  
55      protected Logger log;
56  
57      protected MacroExpand macroExpand;
58  
59      /**
60       * Expand the hostname
61       * 
62       * @param spfData The SPF1Data to use
63       * @throws PermErrorException get Thrown if invalid macros are used
64       */
65      protected String expandHost(SPFSession spfData) throws PermErrorException {
66          String host = getDomain();
67          if (host == null) {
68              host = spfData.getCurrentDomain();
69          } else {
70              // throws a PermErrorException that we cat pass through
71              host = macroExpand.expand(host, spfData, MacroExpand.DOMAIN);
72          }
73          return host;
74      }
75  
76      /**
77       * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(Configuration)
78       */
79      public synchronized void config(Configuration params) throws PermErrorException {
80          if (params.groupCount() >= 1 && params.group(1) != null) {
81              domain = params.group(1);
82          } else {
83              domain = null;
84          }
85      }
86  
87      /**
88       * @return Returns the domain.
89       */
90      protected synchronized String getDomain() {
91          return domain;
92      }
93  
94      /**
95       * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger)
96       */
97      public void enableLogging(Logger logger) {
98          this.log = logger;
99      }
100 
101     /**
102      * @see org.apache.james.jspf.core.MacroExpandEnabled#enableMacroExpand(org.apache.james.jspf.core.MacroExpand)
103      */
104     public void enableMacroExpand(MacroExpand macroExpand) {
105         this.macroExpand = macroExpand;
106     }
107 }