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