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.mailet.GenericMailet;
021: import org.apache.mailet.Mail;
022: import org.apache.mailet.MailetException;
023:
024: import javax.mail.MessagingException;
025:
026: /**
027: * This mailet redirects the mail to the named processor
028: *
029: * Sample configuration:
030: * <mailet match="All" class="ToProcessor">
031: * <processor>spam</processor>
032: * <notice>Notice attached to the message (optional)</notice>
033: * </mailet>
034: *
035: */
036: public class ToProcessor extends GenericMailet {
037:
038: /**
039: * Controls certain log messages
040: */
041: private boolean isDebug = false;
042:
043: /**
044: * The name of the processor to which this mailet forwards mail
045: */
046: String processor;
047:
048: /**
049: * The error message to attach to the forwarded message
050: */
051: String noticeText = null;
052:
053: /**
054: * Initialize the mailet
055: *
056: * @throws MailetException if the processor parameter is missing
057: */
058: public void init() throws MailetException {
059: isDebug = (getInitParameter("debug") == null) ? false
060: : new Boolean(getInitParameter("debug")).booleanValue();
061: processor = getInitParameter("processor");
062: if (processor == null) {
063: throw new MailetException("processor parameter is required");
064: }
065: noticeText = getInitParameter("notice");
066: }
067:
068: /**
069: * Deliver a mail to the processor.
070: *
071: * @param mail the mail to process
072: *
073: * @throws MessagingException in all cases
074: */
075: public void service(Mail mail) throws MessagingException {
076: if (isDebug) {
077: StringBuffer logBuffer = new StringBuffer(128).append(
078: "Sending mail ").append(mail).append(" to ")
079: .append(processor);
080: log(logBuffer.toString());
081: }
082: mail.setState(processor);
083: if (noticeText != null) {
084: if (mail.getErrorMessage() == null) {
085: mail.setErrorMessage(noticeText);
086: } else {
087: StringBuffer errorMessageBuffer = new StringBuffer(256)
088: .append(mail.getErrorMessage()).append("\r\n")
089: .append(noticeText);
090: mail.setErrorMessage(errorMessageBuffer.toString());
091: }
092: }
093: }
094:
095: /**
096: * Return a string describing this mailet.
097: *
098: * @return a string describing this mailet
099: */
100: public String getMailetInfo() {
101: return "ToProcessor Mailet";
102: }
103: }
|