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.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailetException;
23
24 import javax.mail.MessagingException;
25
26 /***
27 * This mailet redirects the mail to the named processor
28 *
29 * Sample configuration:
30 * <mailet match="All" class="ToProcessor">
31 * <processor>spam</processor>
32 * <notice>Notice attached to the message (optional)</notice>
33 * </mailet>
34 *
35 */
36 public class ToProcessor extends GenericMailet {
37
38 /***
39 * Controls certain log messages
40 */
41 private boolean isDebug = false;
42
43 /***
44 * The name of the processor to which this mailet forwards mail
45 */
46 String processor;
47
48 /***
49 * The error message to attach to the forwarded message
50 */
51 String noticeText = null;
52
53 /***
54 * Initialize the mailet
55 *
56 * @throws MailetException if the processor parameter is missing
57 */
58 public void init() throws MailetException {
59 isDebug = (getInitParameter("debug") == null) ? false : new Boolean(getInitParameter("debug")).booleanValue();
60 processor = getInitParameter("processor");
61 if (processor == null) {
62 throw new MailetException("processor parameter is required");
63 }
64 noticeText = getInitParameter("notice");
65 }
66
67 /***
68 * Deliver a mail to the processor.
69 *
70 * @param mail the mail to process
71 *
72 * @throws MessagingException in all cases
73 */
74 public void service(Mail mail) throws MessagingException {
75 if (isDebug) {
76 StringBuffer logBuffer =
77 new StringBuffer(128)
78 .append("Sending mail ")
79 .append(mail)
80 .append(" to ")
81 .append(processor);
82 log(logBuffer.toString());
83 }
84 mail.setState(processor);
85 if (noticeText != null) {
86 if (mail.getErrorMessage() == null) {
87 mail.setErrorMessage(noticeText);
88 } else {
89 StringBuffer errorMessageBuffer =
90 new StringBuffer(256)
91 .append(mail.getErrorMessage())
92 .append("\r\n")
93 .append(noticeText);
94 mail.setErrorMessage(errorMessageBuffer.toString());
95 }
96 }
97 }
98
99 /***
100 * Return a string describing this mailet.
101 *
102 * @return a string describing this mailet
103 */
104 public String getMailetInfo() {
105 return "ToProcessor Mailet";
106 }
107 }