View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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.avalon.cornerstone.services.store.Store;
21  import org.apache.avalon.framework.configuration.DefaultConfiguration;
22  import org.apache.avalon.framework.service.ServiceException;
23  import org.apache.avalon.framework.service.ServiceManager;
24  import org.apache.james.Constants;
25  import org.apache.james.services.MailRepository;
26  import org.apache.mailet.GenericMailet;
27  import org.apache.mailet.Mail;
28  
29  import javax.mail.MessagingException;
30  
31  import java.util.Iterator;
32  
33  /***
34   * Re-spools Mail found in the specified Repository.
35   *
36   * <mailet match="RecipientIs=respool@localhost" class="FromRepository">
37   *    &lt;repositoryPath&gt; <i>repository path</i> &lt;/repositoryPath&gt;
38   *    &lt;processor&gt; <i>target processor</i> &lt;/repositoryPath&gt;
39   *    &lt;delete&t; [true|<b>false</b>] &lt;/delete&gt;
40   * &lt;/mailet&gt;
41   *
42   * @version This is $Revision: 382444 $
43   */
44  public class FromRepository extends GenericMailet {
45  
46      /***
47       * The repository from where this mailet spools mail.
48       */
49      private MailRepository repository;
50  
51      /***
52       * Whether this mailet should delete messages after being spooled
53       */
54      private boolean delete = false;
55  
56      /***
57       * The path to the repository
58       */
59      private String repositoryPath;
60  
61      /***
62       * The processor that will handle the re-spooled message(s)
63       */
64      private String processor;
65  
66      /***
67       * Initialize the mailet, loading configuration information.
68       */
69      public void init() {
70          repositoryPath = getInitParameter("repositoryPath");
71          processor = (getInitParameter("processor") == null) ? Mail.DEFAULT : getInitParameter("processor");
72  
73          try {
74              delete = (getInitParameter("delete") == null) ? false : new Boolean(getInitParameter("delete")).booleanValue();
75          } catch (Exception e) {
76              // Ignore exception, default to false
77          }
78  
79          ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
80          try {
81              Store mailstore = (Store) compMgr.lookup(Store.ROLE);
82              DefaultConfiguration mailConf
83                  = new DefaultConfiguration("repository", "generated:ToRepository");
84              mailConf.setAttribute("destinationURL", repositoryPath);
85              mailConf.setAttribute("type", "MAIL");
86              repository = (MailRepository) mailstore.select(mailConf);
87          } catch (ServiceException cnfe) {
88              log("Failed to retrieve Store component:" + cnfe.getMessage());
89          } catch (Exception e) {
90              log("Failed to retrieve Store component:" + e.getMessage());
91          }
92      }
93  
94      /***
95       * Spool mail from a particular repository.
96       *
97       * @param triggering e-mail (eventually parameterize via the
98       * trigger message)
99       */
100     public void service(Mail trigger) throws MessagingException {
101         trigger.setState(Mail.GHOST);
102         java.util.Collection processed = new java.util.ArrayList();
103         Iterator list = repository.list();
104         while (list.hasNext()) {
105             String key = (String) list.next();
106             try {
107                 Mail mail =  repository.retrieve(key);
108                 if (mail != null && mail.getRecipients() != null) {
109                     log((new StringBuffer(160).append("Spooling mail ").append(mail.getName()).append(" from ").append(repositoryPath)).toString());
110 
111                     /*
112                     log("Return-Path: " + mail.getMessage().getHeader(RFC2822Headers.RETURN_PATH, ", "));
113                     log("Sender: " + mail.getSender());
114                     log("To: " + mail.getMessage().getHeader(RFC2822Headers.TO, ", "));
115                     log("Recipients: ");
116                     for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
117                         log("    " + ((MailAddress)i.next()).toString());
118                     };
119                     */
120 
121                     mail.setAttribute("FromRepository", Boolean.TRUE);
122                     mail.setState(processor);
123                     getMailetContext().sendMail(mail);
124                     if (delete) processed.add(key);
125                 }
126             } catch (MessagingException e) {
127                 log((new StringBuffer(160).append("Unable to re-spool mail ").append(key).append(" from ").append(repositoryPath)).toString(), e);
128             }
129         }
130 
131         if (delete) {
132             Iterator delList = processed.iterator();
133             while (delList.hasNext()) {
134                 repository.remove((String)delList.next());
135             }
136         }
137     }
138 
139     /***
140      * Return a string describing this mailet.
141      *
142      * @return a string describing this mailet
143      */
144     public String getMailetInfo() {
145         return "FromRepository Mailet";
146     }
147 }