KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > LocalDelivery


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.transport.mailets;
19
20 import org.apache.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailAddress;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 /**
31  * Receives a Mail from JamesSpoolManager and takes care of delivery
32  * of the message to local inboxes.
33  *
34  */

35 public class LocalDelivery extends GenericMailet {
36     /**
37      * Delivers a mail to a local mailbox.
38      *
39      * @param mail the mail being processed
40      *
41      * @throws MessagingException if an error occurs while storing the mail
42      */

43     public void service(Mail mail) throws MessagingException JavaDoc {
44         Collection JavaDoc recipients = mail.getRecipients();
45         Collection JavaDoc errors = new Vector JavaDoc();
46         MimeMessage JavaDoc message = mail.getMessage();
47         for (Iterator JavaDoc i = recipients.iterator(); i.hasNext(); ) {
48             MailAddress recipient = (MailAddress) i.next();
49             try {
50                 // Add qmail's de facto standard Delivered-To header
51
MimeMessage JavaDoc localMessage = new MimeMessage JavaDoc(message) {
52                     protected void updateHeaders() throws MessagingException JavaDoc {
53                         if (getMessageID() == null) super.updateHeaders();
54                         else {
55                             modified = false;
56                         }
57                     }
58                 };
59                 localMessage.addHeader("Delivered-To", recipient.toString());
60                 localMessage.saveChanges();
61
62                 getMailetContext().storeMail(mail.getSender(), recipient, localMessage);
63             } catch (Exception JavaDoc ex) {
64                 getMailetContext().log("Error while storing mail.", ex);
65                 errors.add(recipient);
66             }
67         }
68
69         if (!errors.isEmpty()) {
70             // If there were errors, we redirect the email to the ERROR processor.
71
// In order for this server to meet the requirements of the SMTP specification,
72
// mails on the ERROR processor must be returned to the sender. Note that this
73
// email doesn't include any details regarding the details of the failure(s).
74
// In the future we may wish to address this.
75
getMailetContext().sendMail(mail.getSender(),
76                                         errors, message, Mail.ERROR);
77         }
78         //We always consume this message
79
mail.setState(Mail.GHOST);
80     }
81
82     /**
83      * Return a string describing this mailet.
84      *
85      * @return a string describing this mailet
86      */

87     public String JavaDoc getMailetInfo() {
88         return "Local Delivery Mailet";
89     }
90 }
91
Popular Tags