001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.avalon.cornerstone.services.store.Store;
021: import org.apache.avalon.framework.configuration.DefaultConfiguration;
022: import org.apache.avalon.framework.service.ServiceException;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.james.Constants;
025: import org.apache.james.services.MailRepository;
026: import org.apache.mailet.GenericMailet;
027: import org.apache.mailet.Mail;
028:
029: import javax.mail.MessagingException;
030:
031: import java.util.Iterator;
032:
033: /**
034: * Re-spools Mail found in the specified Repository.
035: *
036: * <mailet match="RecipientIs=respool@localhost" class="FromRepository">
037: * <repositoryPath> <i>repository path</i> </repositoryPath>
038: * <processor> <i>target processor</i> </repositoryPath>
039: * <delete&t; [true|<b>false</b>] </delete>
040: * </mailet>
041: *
042: * @version This is $Revision: 494012 $
043: */
044: public class FromRepository extends GenericMailet {
045:
046: /**
047: * The repository from where this mailet spools mail.
048: */
049: private MailRepository repository;
050:
051: /**
052: * Whether this mailet should delete messages after being spooled
053: */
054: private boolean delete = false;
055:
056: /**
057: * The path to the repository
058: */
059: private String repositoryPath;
060:
061: /**
062: * The processor that will handle the re-spooled message(s)
063: */
064: private String processor;
065:
066: /**
067: * Initialize the mailet, loading configuration information.
068: */
069: public void init() {
070: repositoryPath = getInitParameter("repositoryPath");
071: processor = (getInitParameter("processor") == null) ? Mail.DEFAULT
072: : getInitParameter("processor");
073:
074: try {
075: delete = (getInitParameter("delete") == null) ? false
076: : new Boolean(getInitParameter("delete"))
077: .booleanValue();
078: } catch (Exception e) {
079: // Ignore exception, default to false
080: }
081:
082: ServiceManager compMgr = (ServiceManager) getMailetContext()
083: .getAttribute(Constants.AVALON_COMPONENT_MANAGER);
084: try {
085: Store mailstore = (Store) compMgr.lookup(Store.ROLE);
086: DefaultConfiguration mailConf = new DefaultConfiguration(
087: "repository", "generated:ToRepository");
088: mailConf.setAttribute("destinationURL", repositoryPath);
089: mailConf.setAttribute("type", "MAIL");
090: repository = (MailRepository) mailstore.select(mailConf);
091: } catch (ServiceException cnfe) {
092: log("Failed to retrieve Store component:"
093: + cnfe.getMessage());
094: } catch (Exception e) {
095: log("Failed to retrieve Store component:" + e.getMessage());
096: }
097: }
098:
099: /**
100: * Spool mail from a particular repository.
101: *
102: * @param 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 ")
115: .append(mail.getName()).append(" from ")
116: .append(repositoryPath)).toString());
117:
118: /*
119: log("Return-Path: " + mail.getMessage().getHeader(RFC2822Headers.RETURN_PATH, ", "));
120: log("Sender: " + mail.getSender());
121: log("To: " + mail.getMessage().getHeader(RFC2822Headers.TO, ", "));
122: log("Recipients: ");
123: for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
124: log(" " + ((MailAddress)i.next()).toString());
125: };
126: */
127:
128: mail.setAttribute("FromRepository", Boolean.TRUE);
129: mail.setState(processor);
130: getMailetContext().sendMail(mail);
131: if (delete)
132: processed.add(key);
133: }
134: } catch (MessagingException e) {
135: log((new StringBuffer(160).append(
136: "Unable to re-spool mail ").append(key).append(
137: " from ").append(repositoryPath)).toString(), e);
138: }
139: }
140:
141: if (delete) {
142: Iterator delList = processed.iterator();
143: while (delList.hasNext()) {
144: repository.remove((String) delList.next());
145: }
146: }
147: }
148:
149: /**
150: * Return a string describing this mailet.
151: *
152: * @return a string describing this mailet
153: */
154: public String getMailetInfo() {
155: return "FromRepository Mailet";
156: }
157: }
|