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.listservcommands;
019:
020: import org.apache.james.transport.mailets.ICommandListservManager;
021: import org.apache.james.util.XMLResources;
022: import org.apache.mailet.Mail;
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025:
026: import javax.mail.MessagingException;
027: import java.util.Properties;
028:
029: /**
030: * Error handles the error command.
031: * It is configured by:
032: * <pre><command name="error" class="ErrorCommand"/></pre>
033: *
034: * <br />
035: * <br />
036: *
037: * It uses the formatted text-based resources for its return mail body:
038: * <ul>
039: * <li>header
040: * <li>error
041: * <li>admincommands
042: * </ul>
043: *
044: * <br />
045: * <br />
046: * After formatting the text, the message is delivered with {@link #sendStandardReply}
047: *
048: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
049: * @since 2.2.0
050: */
051: public class ErrorCommand extends BaseCommand {
052:
053: //For resources
054: protected XMLResources[] xmlResources;
055:
056: protected static final int HEADER = 0;
057: protected static final int ERROR = 1;
058: protected static final int ADMIN_COMMANDS = 2;
059:
060: public void init(ICommandListservManager commandListservManager,
061: Configuration configuration) throws ConfigurationException {
062: super .init(commandListservManager, configuration);
063: xmlResources = initXMLResources(new String[] { "header",
064: "error", "admincommands" });
065: }
066:
067: /**
068: * Delegate to {@link #onError}
069: * @param mail
070: * @throws MessagingException
071: */
072: public void onCommand(Mail mail) throws MessagingException {
073: onError(mail, "an unknown error occurred",
074: "an unknown error occurred");
075: }
076:
077: /**
078: * An error occurred, send a message with the following text resources:
079: * <ul>
080: * <li>{@link #HEADER}
081: * <li>{@link #ERROR}
082: * <li>{@link #ADMIN_COMMANDS}
083: * </ul>
084: *
085: * @param subject the subject of the message to send
086: * @param mail
087: * @param errorMessage
088: */
089: public void onError(Mail mail, String subject, String errorMessage)
090: throws MessagingException {
091:
092: Properties props = getStandardProperties();
093: props.put("ERROR_MESSAGE", errorMessage);
094:
095: StringBuffer plainTextMessage = new StringBuffer();
096: String header = xmlResources[HEADER].getString("text", props);
097: plainTextMessage.append(header);
098:
099: String errorText = xmlResources[ERROR].getString("text", props);
100: plainTextMessage.append(errorText);
101:
102: String adminCommands = xmlResources[ADMIN_COMMANDS].getString(
103: "text", props);
104: plainTextMessage.append(adminCommands);
105:
106: sendStandardReply(mail, subject, plainTextMessage.toString(),
107: null);
108: }
109: }
|