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.terms; 022 023 import org.apache.james.jspf.core.LogEnabled; 024 import org.apache.james.jspf.core.Logger; 025 import org.apache.james.jspf.core.MacroExpand; 026 import org.apache.james.jspf.core.MacroExpandEnabled; 027 import org.apache.james.jspf.core.SPFSession; 028 import org.apache.james.jspf.core.exceptions.PermErrorException; 029 030 /** 031 * This abstract class represent a gerneric mechanism 032 * 033 */ 034 public abstract class GenericMechanism implements Mechanism, ConfigurationEnabled, LogEnabled, MacroExpandEnabled { 035 036 /** 037 * ABNF: ip4-cidr-length = "/" 1*DIGIT 038 */ 039 protected static final String IP4_CIDR_LENGTH_REGEX = "/(\\d+)"; 040 041 /** 042 * ABNF: ip6-cidr-length = "/" 1*DIGIT 043 */ 044 protected static final String IP6_CIDR_LENGTH_REGEX = "/(\\d+)"; 045 046 /** 047 * ABNF: dual-cidr-length = [ ip4-cidr-length ] [ "/" ip6-cidr-length ] 048 */ 049 protected static final String DUAL_CIDR_LENGTH_REGEX = "(?:" 050 + IP4_CIDR_LENGTH_REGEX + ")?" + "(?:/" + IP6_CIDR_LENGTH_REGEX 051 + ")?"; 052 053 private String domain; 054 055 protected Logger log; 056 057 protected MacroExpand macroExpand; 058 059 /** 060 * Expand the hostname 061 * 062 * @param spfData The SPF1Data to use 063 * @throws PermErrorException get Thrown if invalid macros are used 064 */ 065 protected String expandHost(SPFSession spfData) throws PermErrorException { 066 String host = getDomain(); 067 if (host == null) { 068 host = spfData.getCurrentDomain(); 069 } else { 070 // throws a PermErrorException that we cat pass through 071 host = macroExpand.expand(host, spfData, MacroExpand.DOMAIN); 072 } 073 return host; 074 } 075 076 /** 077 * @see org.apache.james.jspf.terms.ConfigurationEnabled#config(Configuration) 078 */ 079 public synchronized void config(Configuration params) throws PermErrorException { 080 if (params.groupCount() >= 1 && params.group(1) != null) { 081 domain = params.group(1); 082 } else { 083 domain = null; 084 } 085 } 086 087 /** 088 * @return Returns the domain. 089 */ 090 protected synchronized String getDomain() { 091 return domain; 092 } 093 094 /** 095 * @see org.apache.james.jspf.core.LogEnabled#enableLogging(org.apache.james.jspf.core.Logger) 096 */ 097 public void enableLogging(Logger logger) { 098 this.log = logger; 099 } 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 }