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