Index: AbstractVirtualUserTable.java =================================================================== --- AbstractVirtualUserTable.java (.../branches/next-minor/src/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java) (revision 727822) +++ AbstractVirtualUserTable.java (.../trunk/mailets-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java) (revision 727822) @@ -1,31 +1,33 @@ -/*********************************************************************** - * Copyright (c) 2000-2006 The Apache Software Foundation. * - * All rights reserved. * - * ------------------------------------------------------------------- * - * Licensed under the Apache License, Version 2.0 (the "License"); you * - * may not use this file except in compliance with the License. You * - * may obtain a copy of the License at: * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, software * - * distributed under the License is distributed on an "AS IS" BASIS, * - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * - * implied. See the License for the specific language governing * - * permissions and limitations under the License. * - ***********************************************************************/ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + + package org.apache.james.transport.mailets; +import org.apache.james.Constants; import org.apache.james.core.MailImpl; -import org.apache.james.util.XMLResources; -import org.apache.mailet.GenericMailet; +import org.apache.james.impl.vut.VirtualUserTableUtil; +import org.apache.mailet.base.GenericMailet; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; -import org.apache.oro.text.regex.MatchResult; -import org.apache.oro.text.regex.Pattern; -import org.apache.oro.text.regex.Perl5Compiler; -import org.apache.oro.text.regex.Perl5Matcher; +import org.apache.oro.text.regex.MalformedPatternException; import javax.mail.MessagingException; import javax.mail.internet.ParseException; @@ -96,12 +98,16 @@ // log("Attempting to map from " + source + " to " + targetAddress); if (targetAddress.startsWith("regex:")) { - targetAddress = regexMap(mail, source, targetAddress); + try { + targetAddress = VirtualUserTableUtil.regexMap(source, targetAddress); + } catch (MalformedPatternException e) { + log("Exception during regexMap processing: ", e); + } if (targetAddress == null) continue; } try { - MailAddress target = (targetAddress.indexOf('@') < 0) ? new MailAddress(targetAddress, "localhost") + MailAddress target = (targetAddress.indexOf('@') < 0) ? new MailAddress(targetAddress, (String) getMailetContext().getAttribute(Constants.DEFAULT_DOMAIN)) : new MailAddress(targetAddress); //Mark this source address as an address to remove from the recipient list @@ -157,15 +163,11 @@ // getMailetContext().sendMail(mail.getSender(), recipientsToAddForward, mail.getMessage()); // duplicates the Mail object, to be able to modify the new mail keeping the original untouched - MailImpl newMail = new MailImpl(mail,newName(mail)); + MailImpl newMail = new MailImpl(mail); try { - try { - newMail.setRemoteAddr(java.net.InetAddress.getLocalHost().getHostAddress()); - newMail.setRemoteHost(java.net.InetAddress.getLocalHost().getHostName()); - } catch (java.net.UnknownHostException _) { - newMail.setRemoteAddr("127.0.0.1"); - newMail.setRemoteHost("localhost"); - } + newMail.setRemoteAddr(getMailetContext().getAttribute(Constants.HOSTADDRESS).toString()); + newMail.setRemoteHost(getMailetContext().getAttribute(Constants.HOSTNAME).toString()); + newMail.setRecipients(recipientsToAddForward); newMail.setAttribute(MARKER, Boolean.TRUE); getMailetContext().sendMail(newMail); @@ -222,49 +224,6 @@ } /** - * Processes regex virtual user mapping - * - * If a mapped target string begins with the prefix regex:, it must be - * formatted as regex::, - * e.g., regex:(.*)@(.*):${1}@tld - * - * @param mail the Mail instance being processed - * @param address the MailAddress to be mapped - * @param targetString a String specifying the mapping - */ - private String regexMap(Mail mail, MailAddress address, String targetString) { - String result = null; - - try { - int msgPos = targetString.indexOf(':', "regex:".length() + 1); - - // log("regex: targetString = " + targetString); - // log("regex: msgPos = " + msgPos); - // log("regex: compile " + targetString.substring("regex:".length(), msgPos)); - // log("regex: address = " + address.toString()); - // log("regex: replace = " + targetString.substring(msgPos + 1)); - - Pattern pattern = new Perl5Compiler().compile(targetString.substring("regex:".length(), msgPos)); - Perl5Matcher matcher = new Perl5Matcher(); - - if (matcher.matches(address.toString(), pattern)) { - MatchResult match = matcher.getMatch(); - Map parameters = new HashMap(match.groups()); - for (int i = 1; i < match.groups(); i++) { - parameters.put(Integer.toString(i), match.group(i)); - } - result = XMLResources.replaceParameters(targetString.substring(msgPos + 1), parameters); - } - } - catch (Exception e) { - log("Exception during regexMap processing: ", e); - } - - // log("regex: result = " + result); - return result; - } - - /** * Returns the character used to delineate multiple addresses. * * @param targetString the string to parse @@ -274,41 +233,4 @@ return (targetString.indexOf(',') > -1 ? "," : (targetString.indexOf(';') > -1 ? ";" : (targetString.indexOf("regex:") > -1? "" : ":" ))); } - private static final java.util.Random random = new java.util.Random(); // Used to generate new mail names - - /** - * Create a unique new primary key name. - * - * @param mail the mail to use as the basis for the new mail name - * @return a new name - */ - private String newName(Mail mail) throws MessagingException { - String oldName = mail.getName(); - - // Checking if the original mail name is too long, perhaps because of a - // loop caused by a configuration error. - // it could cause a "null pointer exception" in AvalonMailRepository much - // harder to understand. - if (oldName.length() > 76) { - int count = 0; - int index = 0; - while ((index = oldName.indexOf('!', index + 1)) >= 0) { - count++; - } - // It looks like a configuration loop. It's better to stop. - if (count > 7) { - throw new MessagingException("Unable to create a new message name: too long. Possible loop in config.xml."); - } - else { - oldName = oldName.substring(0, 76); - } - } - - StringBuffer nameBuffer = - new StringBuffer(64) - .append(oldName) - .append("-!") - .append(random.nextInt(1048576)); - return nameBuffer.toString(); - } }