ToMultiRepositorypublic class ToMultiRepository extends org.apache.mailet.GenericMailet Receives a Mail from JamesSpoolManager and takes care of delivery of the
message to local inboxes or a specific repository.
Differently from LocalDelivery this does not lookup the UserRepository This
simply store the message in a repository named like the local part of the
recipient address.
If no repository is specified then this fallback to MailServer.getUserInbox.
Otherwise you can add your own configuration for the repository
e.g: file://var/spool/userspools/
SPOOL
file://var/mail/inboxes/ MAIL
Header "Delivered-To" can be added to every message adding the
Delivered-To |
Fields Summary |
---|
private static long | countThe number of mails generated. Access needs to be synchronized for thread
safety and to ensure that all threads see the latest value. | private org.apache.james.services.MailServer | mailServerThe mailserver reference | private org.apache.avalon.cornerstone.services.store.Store | storeThe mailstore | private String | repositoryUrlThe optional repositoryUrl | private String | repositoryTypeThe optional repositoryType | private String | deliveryHeaderThe delivery header | private boolean | resetReturnPathresetReturnPath |
Methods Summary |
---|
public java.lang.String | getId()Return a new mail id.
long localCount = -1;
synchronized (James.class) {
localCount = count++;
}
StringBuffer idBuffer = new StringBuffer(64).append("Mail").append(
System.currentTimeMillis()).append("-").append(localCount);
return idBuffer.toString();
| public java.lang.String | getMailetInfo()Return a string describing this mailet.
return "ToMultiRepository Mailet";
| private org.apache.james.services.MailRepository | getRepository(java.lang.String userName)Get the user inbox: if the repositoryUrl is null then get the userinbox
from the mailserver, otherwise lookup the store with the given
repositoryurl/type
MailRepository userInbox;
if (repositoryUrl == null) {
userInbox = mailServer.getUserInbox(userName);
} else {
StringBuffer destinationBuffer = new StringBuffer(192).append(
repositoryUrl).append(userName).append("/");
String destination = destinationBuffer.toString();
DefaultConfiguration mboxConf = new DefaultConfiguration(
"repository", "generated:ToMultiRepository.getUserInbox()");
mboxConf.setAttribute("destinationURL", destination);
mboxConf.setAttribute("type", repositoryType);
try {
userInbox = (MailRepository) store.select(mboxConf);
} catch (Exception e) {
log("Cannot open repository " + e);
userInbox = null;
}
}
return userInbox;
| public void | init()
super.init();
ServiceManager compMgr = (ServiceManager) getMailetContext()
.getAttribute(Constants.AVALON_COMPONENT_MANAGER);
try {
// Instantiate the a MailRepository for outgoing mails
mailServer = (MailServer) compMgr.lookup(MailServer.ROLE);
} catch (ServiceException cnfe) {
log("Failed to retrieve MailServer component:" + cnfe.getMessage());
} catch (Exception e) {
log("Failed to retrieve MailServer component:" + e.getMessage());
}
try {
// Instantiate the a MailRepository for outgoing mails
store = (Store) compMgr.lookup(Store.ROLE);
} catch (ServiceException cnfe) {
log("Failed to retrieve Store component:" + cnfe.getMessage());
} catch (Exception e) {
log("Failed to retrieve Store component:" + e.getMessage());
}
repositoryUrl = getInitParameter("repositoryUrl");
if (repositoryUrl != null) {
repositoryType = getInitParameter("repositoryType");
if (repositoryType == null)
repositoryType = "MAIL";
}
deliveryHeader = getInitParameter("addDeliveryHeader");
String resetReturnPathString = getInitParameter("resetReturnPath");
resetReturnPath = "true".equalsIgnoreCase(resetReturnPathString);
| public void | service(org.apache.mailet.Mail mail)Delivers a mail to a local mailbox.
Collection recipients = mail.getRecipients();
Collection errors = new Vector();
MimeMessage message = null;
if (deliveryHeader != null || resetReturnPath) {
message = mail.getMessage();
}
if (resetReturnPath) {
// 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()
+ ">"));
}
Enumeration headers;
InternetHeaders deliveredTo = new InternetHeaders();
if (deliveryHeader != null) {
// Copy any Delivered-To headers from the message
headers = message
.getMatchingHeaders(new String[] { deliveryHeader });
while (headers.hasMoreElements()) {
Header header = (Header) headers.nextElement();
deliveredTo.addHeader(header.getName(), header.getValue());
}
}
for (Iterator i = recipients.iterator(); i.hasNext();) {
MailAddress recipient = (MailAddress) i.next();
try {
if (deliveryHeader != null) {
// Add qmail's de facto standard Delivered-To header
message.addHeader(deliveryHeader, recipient.toString());
}
storeMail(mail.getSender(), recipient, message);
if (deliveryHeader != null) {
if (i.hasNext()) {
// Remove headers but leave all placeholders
message.removeHeader(deliveryHeader);
headers = deliveredTo.getAllHeaders();
// And restore any original Delivered-To headers
while (headers.hasMoreElements()) {
Header header = (Header) headers.nextElement();
message.addHeader(header.getName(), header
.getValue());
}
}
}
} catch (Exception ex) {
getMailetContext().log("Error while storing mail.", ex);
errors.add(recipient);
}
}
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, mail.getMessage(),
Mail.ERROR);
}
// We always consume this message
mail.setState(Mail.GHOST);
| public void | storeMail(org.apache.mailet.MailAddress sender, org.apache.mailet.MailAddress recipient, javax.mail.internet.MimeMessage message)
String username;
if (recipient == null) {
throw new IllegalArgumentException(
"Recipient for mail to be spooled cannot be null.");
}
if (message == null) {
throw new IllegalArgumentException(
"Mail message to be spooled cannot be null.");
}
username = recipient.getUser();
Collection recipients = new HashSet();
recipients.add(recipient);
MailImpl mail = new MailImpl(getId(), sender, recipients, message);
try {
MailRepository userInbox = getRepository(username);
if (userInbox == null) {
StringBuffer errorBuffer = new StringBuffer(128).append(
"The repository for user ").append(username).append(
" was not found on this server.");
throw new MessagingException(errorBuffer.toString());
}
userInbox.store(mail);
} finally {
mail.dispose();
}
|
|