View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.transport.mailets;
21  
22  import org.apache.avalon.cornerstone.services.store.Store;
23  import org.apache.avalon.framework.configuration.DefaultConfiguration;
24  import org.apache.avalon.framework.service.ServiceException;
25  import org.apache.avalon.framework.service.ServiceManager;
26  import org.apache.james.Constants;
27  import org.apache.james.services.MailRepository;
28  import org.apache.mailet.GenericMailet;
29  import org.apache.mailet.Mail;
30  
31  import javax.mail.MessagingException;
32  
33  import java.util.Iterator;
34  
35  /***
36   * Re-spools Mail found in the specified Repository.
37   *
38   * <mailet match="RecipientIs=respool@localhost" class="FromRepository">
39   *    &lt;repositoryPath&gt; <i>repository path</i> &lt;/repositoryPath&gt;
40   *    &lt;processor&gt; <i>target processor</i> &lt;/repositoryPath&gt;
41   *    &lt;delete&t; [true|<b>false</b>] &lt;/delete&gt;
42   * &lt;/mailet&gt;
43   *
44   * @version This is $Revision: 494012 $
45   */
46  public class FromRepository extends GenericMailet {
47  
48      /***
49       * The repository from where this mailet spools mail.
50       */
51      private MailRepository repository;
52  
53      /***
54       * Whether this mailet should delete messages after being spooled
55       */
56      private boolean delete = false;
57  
58      /***
59       * The path to the repository
60       */
61      private String repositoryPath;
62  
63      /***
64       * The processor that will handle the re-spooled message(s)
65       */
66      private String processor;
67  
68      /***
69       * Initialize the mailet, loading configuration information.
70       */
71      public void init() {
72          repositoryPath = getInitParameter("repositoryPath");
73          processor = (getInitParameter("processor") == null) ? Mail.DEFAULT : getInitParameter("processor");
74  
75          try {
76              delete = (getInitParameter("delete") == null) ? false : new Boolean(getInitParameter("delete")).booleanValue();
77          } catch (Exception e) {
78              // Ignore exception, default to false
79          }
80  
81          ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
82          try {
83              Store mailstore = (Store) compMgr.lookup(Store.ROLE);
84              DefaultConfiguration mailConf
85                  = new DefaultConfiguration("repository", "generated:ToRepository");
86              mailConf.setAttribute("destinationURL", repositoryPath);
87              mailConf.setAttribute("type", "MAIL");
88              repository = (MailRepository) mailstore.select(mailConf);
89          } catch (ServiceException cnfe) {
90              log("Failed to retrieve Store component:" + cnfe.getMessage());
91          } catch (Exception e) {
92              log("Failed to retrieve Store component:" + e.getMessage());
93          }
94      }
95  
96      /***
97       * Spool mail from a particular repository.
98       *
99       * @param triggering e-mail (eventually parameterize via the
100      * trigger message)
101      */
102     public void service(Mail trigger) throws MessagingException {
103         trigger.setState(Mail.GHOST);
104         java.util.Collection processed = new java.util.ArrayList();
105         Iterator list = repository.list();
106         while (list.hasNext()) {
107             String key = (String) list.next();
108             try {
109                 Mail mail =  repository.retrieve(key);
110                 if (mail != null && mail.getRecipients() != null) {
111                     log((new StringBuffer(160).append("Spooling mail ").append(mail.getName()).append(" from ").append(repositoryPath)).toString());
112 
113                     /*
114                     log("Return-Path: " + mail.getMessage().getHeader(RFC2822Headers.RETURN_PATH, ", "));
115                     log("Sender: " + mail.getSender());
116                     log("To: " + mail.getMessage().getHeader(RFC2822Headers.TO, ", "));
117                     log("Recipients: ");
118                     for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
119                         log("    " + ((MailAddress)i.next()).toString());
120                     };
121                     */
122 
123                     mail.setAttribute("FromRepository", Boolean.TRUE);
124                     mail.setState(processor);
125                     getMailetContext().sendMail(mail);
126                     if (delete) processed.add(key);
127                 }
128             } catch (MessagingException e) {
129                 log((new StringBuffer(160).append("Unable to re-spool mail ").append(key).append(" from ").append(repositoryPath)).toString(), e);
130             }
131         }
132 
133         if (delete) {
134             Iterator delList = processed.iterator();
135             while (delList.hasNext()) {
136                 repository.remove((String)delList.next());
137             }
138         }
139     }
140 
141     /***
142      * Return a string describing this mailet.
143      *
144      * @return a string describing this mailet
145      */
146     public String getMailetInfo() {
147         return "FromRepository Mailet";
148     }
149 }