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