You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2008/12/14 17:58:08 UTC
svn commit: r726495 - in
/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets:
AbstractVirtualUserTable.java UsersRepositoryAliasingForwarding.java
VirtualUserTable.java
Author: norman
Date: Sun Dec 14 08:58:08 2008
New Revision: 726495
URL: http://svn.apache.org/viewvc?rev=726495&view=rev
Log:
Add Mailet for using with VirtualUserTableStore todo the right mappings. See JAMES-882
Added:
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/VirtualUserTable.java
Modified:
james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java
Added: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java?rev=726495&view=auto
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java (added)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/AbstractVirtualUserTable.java Sun Dec 14 08:58:08 2008
@@ -0,0 +1,186 @@
+/****************************************************************
+ * 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 java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Vector;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.james.Constants;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
+import org.apache.mailet.base.GenericMailet;
+import org.apache.mailet.base.RFC2822Headers;
+
+/**
+ * Abstract base class which should get extended by classes which handle mapping
+ * operations based on VirtualUserTable implementations
+ *
+ *
+ */
+public abstract class AbstractVirtualUserTable extends GenericMailet{
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
+ */
+ public void service(Mail mail) throws MessagingException {
+ Collection recipients = mail.getRecipients();
+ Collection errors = new Vector();
+
+ MimeMessage message = mail.getMessage();
+
+ // Set Return-Path and remove all other Return-Path headers from the
+ // message
+ // This only works because there is a placeholder inserted by
+ // MimeMessageWrapper
+ message
+ .setHeader(RFC2822Headers.RETURN_PATH,
+ (mail.getSender() == null ? "<>" : "<"
+ + mail.getSender() + ">"));
+
+ Collection newRecipients = new LinkedList();
+ for (Iterator i = recipients.iterator(); i.hasNext();) {
+ MailAddress recipient = (MailAddress) i.next();
+ try {
+ Collection usernames = processMail(mail.getSender(), recipient,
+ message);
+
+ // if the username is null or changed we remove it from the
+ // remaining recipients
+ if (usernames == null) {
+ i.remove();
+ } else {
+ i.remove();
+ // if the username has been changed we add a new recipient
+ // with the new name.
+ newRecipients.addAll(usernames);
+ }
+
+ } catch (Exception ex) {
+ getMailetContext().log("Error while storing mail.", ex);
+ errors.add(recipient);
+ }
+ }
+
+ if (newRecipients.size() > 0) {
+ recipients.addAll(newRecipients);
+ }
+
+ if (!errors.isEmpty()) {
+ // If there were errors, we redirect the email to the ERROR
+ // processor.
+ // In order for this server to meet the requirements of the SMTP
+ // specification, mails on the ERROR processor must be returned to
+ // the sender. Note that this email doesn't include any details
+ // regarding the details of the failure(s).
+ // In the future we may wish to address this.
+ getMailetContext().sendMail(mail.getSender(), errors, message,
+ Mail.ERROR);
+ }
+
+ if (recipients.size() == 0) {
+ // We always consume this message
+ mail.setState(Mail.GHOST);
+ }
+ }
+
+ /**
+ * Handle the given mappings to map the original recipient to the right one
+ *
+ * @param mappings a collection of mappings for the given recipient
+ * @param sender the sender of the mail
+ * @param recipient the original recipient of the email
+ * @param message the mail message
+ * @return a collection of mapped recpient addresses
+ *
+ * @throws MessagingException
+ */
+ protected Collection handleMappings(Collection mappings, MailAddress sender, MailAddress recipient,
+ MimeMessage message) throws MessagingException {
+ Iterator i = mappings.iterator();
+ Collection remoteRecipients = new ArrayList();
+ Collection localRecipients = new ArrayList();
+ while (i.hasNext()) {
+ String rcpt = (String) i.next();
+
+ if (rcpt.indexOf("@") < 0) {
+ // the mapping contains no domain name, use the default domain
+ rcpt = rcpt + "@" + getMailetContext().getAttribute(Constants.DEFAULT_DOMAIN);
+ }
+
+ MailAddress nextMap = new MailAddress(rcpt);
+ if (getMailetContext().isLocalServer(nextMap.getHost())) {
+ localRecipients.add(nextMap);
+ } else {
+ remoteRecipients.add(nextMap);
+ }
+ }
+
+ if (remoteRecipients.size() > 0) {
+ try {
+ getMailetContext().sendMail(sender, remoteRecipients, message);
+ StringBuffer logBuffer = new StringBuffer(128).append("Mail for ").append(recipient).append(" forwarded to ");
+ for (Iterator j = remoteRecipients.iterator(); j.hasNext();) {
+ logBuffer.append(j.next());
+ if (j.hasNext())
+ logBuffer.append(", ");
+ }
+ getMailetContext().log(logBuffer.toString());
+ return null;
+ } catch (MessagingException me) {
+ StringBuffer logBuffer = new StringBuffer(128).append("Error forwarding mail to ");
+ for (Iterator j = remoteRecipients.iterator(); j.hasNext();) {
+ logBuffer.append(j.next());
+ if (j.hasNext())
+ logBuffer.append(", ");
+ }
+ logBuffer.append("attempting local delivery");
+
+ getMailetContext().log(logBuffer.toString());
+ throw me;
+ }
+ }
+
+ if (localRecipients.size() > 0) {
+ return localRecipients;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Process the mail
+ *
+ * @param sender the sender of the mail
+ * @param recipient the recipient of the mail
+ * @param message the mail message
+ * @return collection of recipients
+ *
+ * @throws MessagingException
+ */
+ public abstract Collection processMail(MailAddress sender, MailAddress recipient,
+ MimeMessage message) throws MessagingException;
+}
Modified: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java?rev=726495&r1=726494&r2=726495&view=diff
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java (original)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/UsersRepositoryAliasingForwarding.java Sun Dec 14 08:58:08 2008
@@ -57,7 +57,7 @@
* <usersRepository>LocalAdmins</usersRepository>: specific users repository
* name. Default to empty. If empty does lookup the default userRepository.
*/
-public class UsersRepositoryAliasingForwarding extends GenericMailet {
+public class UsersRepositoryAliasingForwarding extends AbstractVirtualUserTable {
/**
* The user repository for this mail server. Contains all the users with
@@ -66,76 +66,6 @@
private UsersRepository usersRepository;
/**
- * Delivers a mail to a local mailbox.
- *
- * @param mail
- * the mail being processed
- *
- * @throws MessagingException
- * if an error occurs while storing the mail
- */
- public void service(Mail mail) throws MessagingException {
- Collection recipients = mail.getRecipients();
- Collection errors = new Vector();
-
- MimeMessage message = mail.getMessage();
-
- // Set Return-Path and remove all other Return-Path headers from the
- // message
- // This only works because there is a placeholder inserted by
- // MimeMessageWrapper
- message
- .setHeader(RFC2822Headers.RETURN_PATH,
- (mail.getSender() == null ? "<>" : "<"
- + mail.getSender() + ">"));
-
- Collection newRecipients = new LinkedList();
- for (Iterator i = recipients.iterator(); i.hasNext();) {
- MailAddress recipient = (MailAddress) i.next();
- try {
- Collection usernames = processMail(mail.getSender(), recipient,
- message);
-
- // if the username is null or changed we remove it from the
- // remaining recipients
- if (usernames == null) {
- i.remove();
- } else {
- i.remove();
- // if the username has been changed we add a new recipient
- // with the new name.
- newRecipients.addAll(usernames);
- }
-
- } catch (Exception ex) {
- getMailetContext().log("Error while storing mail.", ex);
- errors.add(recipient);
- }
- }
-
- if (newRecipients.size() > 0) {
- recipients.addAll(newRecipients);
- }
-
- if (!errors.isEmpty()) {
- // If there were errors, we redirect the email to the ERROR
- // processor.
- // In order for this server to meet the requirements of the SMTP
- // specification, mails on the ERROR processor must be returned to
- // the sender. Note that this email doesn't include any details
- // regarding the details of the failure(s).
- // In the future we may wish to address this.
- getMailetContext().sendMail(mail.getSender(), errors, message,
- Mail.ERROR);
- }
-
- if (recipients.size() == 0) {
- // We always consume this message
- mail.setState(Mail.GHOST);
- }
- }
-
- /**
* Return a string describing this mailet.
*
* @return a string describing this mailet
@@ -178,56 +108,7 @@
}
if (mappings != null) {
- Iterator i = mappings.iterator();
- Collection remoteRecipients = new ArrayList();
- Collection localRecipients = new ArrayList();
- while (i.hasNext()) {
- String rcpt = (String) i.next();
-
- if (rcpt.indexOf("@") < 0) {
- // the mapping contains no domain name, use the default domain
- rcpt = rcpt + "@" + getMailetContext().getAttribute(Constants.DEFAULT_DOMAIN);
- }
-
- MailAddress nextMap = new MailAddress(rcpt);
- if (getMailetContext().isLocalServer(nextMap.getHost())) {
- localRecipients.add(nextMap);
- } else {
- remoteRecipients.add(nextMap);
- }
- }
-
- if (remoteRecipients.size() > 0) {
- try {
- getMailetContext().sendMail(sender, remoteRecipients, message);
- StringBuffer logBuffer = new StringBuffer(128).append(
- "Mail for ").append(recipient).append(
- " forwarded to ");
- for (Iterator j = remoteRecipients.iterator(); j.hasNext(); ) {
- logBuffer.append(j.next());
- if (j.hasNext()) logBuffer.append(", ");
- }
- getMailetContext().log(logBuffer.toString());
- return null;
- } catch (MessagingException me) {
- StringBuffer logBuffer = new StringBuffer(128).append(
- "Error forwarding mail to ");
- for (Iterator j = remoteRecipients.iterator(); j.hasNext(); ) {
- logBuffer.append(j.next());
- if (j.hasNext()) logBuffer.append(", ");
- }
- logBuffer.append("attempting local delivery");
-
- getMailetContext().log(logBuffer.toString());
- throw me;
- }
- }
-
- if (localRecipients.size() > 0) {
- return localRecipients;
- } else {
- return null;
- }
+ return handleMappings(mappings, sender, recipient, message);
}
} else {
StringBuffer errorBuffer = new StringBuffer(128)
Added: james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/VirtualUserTable.java
URL: http://svn.apache.org/viewvc/james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/VirtualUserTable.java?rev=726495&view=auto
==============================================================================
--- james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/VirtualUserTable.java (added)
+++ james/server/trunk/spoolmanager-function/src/main/java/org/apache/james/transport/mailets/VirtualUserTable.java Sun Dec 14 08:58:08 2008
@@ -0,0 +1,94 @@
+/****************************************************************
+ * 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 java.util.ArrayList;
+import java.util.Collection;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.james.Constants;
+import org.apache.james.api.vut.ErrorMappingException;
+import org.apache.james.api.vut.VirtualUserTableStore;
+import org.apache.mailet.MailAddress;
+
+/**
+ * Mailet which should get used when using VirtualUserTable-Store to implementations
+ * for mappings of forwards and aliases.
+ *
+ * If no VirtualUsertable-Store name is given the default of DefaultVirtualUserTable
+ * will get used.
+ *
+ * eg. <virtualusertable>DefaultVirtualUserTable</virtualusertable>
+ *
+ */
+public class VirtualUserTable extends AbstractVirtualUserTable {
+ private org.apache.james.api.vut.VirtualUserTable vut;
+
+ /*
+ * (non-Javadoc)
+ * @see org.apache.mailet.base.GenericMailet#init()
+ */
+ public void init() throws MessagingException {
+ super.init();
+ ServiceManager compMgr = (ServiceManager) getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
+
+ try {
+ String vutName = getInitParameter("virtualusertable");
+ if (vutName == null || vutName.length() == 0) {
+ try {
+ vut = ((VirtualUserTableStore) compMgr.lookup(VirtualUserTableStore.ROLE)).getTable(vutName);
+ } catch (ServiceException e) {
+ log("Failed to retrieve VirtualUserTable component:" + e.getMessage());
+ }
+ } else {
+ vut = ((VirtualUserTableStore) compMgr.lookup(VirtualUserTableStore.ROLE)).getTable("DefaultVirtualUserTable");
+ }
+
+ } catch (ServiceException cnfe) {
+ log("Failed to retrieve UsersStore component:" + cnfe.getMessage());
+ }
+ }
+
+ @Override
+ public Collection processMail(MailAddress sender, MailAddress recipient, MimeMessage message) throws MessagingException {
+ try {
+ Collection mappings = vut.getMappings(recipient.getUser(), recipient.getHost());
+
+ if (mappings != null) {
+ return handleMappings(mappings, sender, recipient, message);
+ }
+ } catch (ErrorMappingException e) {
+ StringBuffer errorBuffer = new StringBuffer(128)
+ .append("A problem as occoured trying to alias and forward user ")
+ .append(recipient)
+ .append(": ")
+ .append(e.getMessage());
+ throw new MessagingException(errorBuffer.toString());
+ }
+
+ Collection rcpts = new ArrayList();
+ rcpts.add(recipient);
+ return rcpts;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org