View Javadoc

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.listservcommands;
19  
20  import org.apache.james.transport.mailets.ICommandListservManager;
21  import org.apache.james.util.XMLResources;
22  import org.apache.mailet.Mail;
23  import org.apache.avalon.framework.configuration.Configuration;
24  import org.apache.avalon.framework.configuration.ConfigurationException;
25  
26  import javax.mail.MessagingException;
27  import java.util.Properties;
28  
29  /***
30   * Error handles the error command.
31   * It is configured by:
32   * <pre>&lt;command name="error" class="ErrorCommand"/&gt;</pre>
33   *
34   * <br />
35   * <br />
36   *
37   * It uses the formatted text-based resources for its return mail body:
38   * <ul>
39   *  <li>header
40   *  <li>error
41   *  <li>admincommands
42   * </ul>
43   *
44   * <br />
45   * <br />
46   * After formatting the text, the message is delivered with {@link #sendStandardReply}
47   *
48   * @version CVS $Revision: 423745 $ $Date: 2006-07-20 03:47:46 +0000 (gio, 20 lug 2006) $
49   * @since 2.2.0
50   */
51  public class ErrorCommand extends BaseCommand {
52  
53      //For resources
54      protected XMLResources[] xmlResources;
55  
56      protected static final int HEADER = 0;
57      protected static final int ERROR = 1;
58      protected static final int ADMIN_COMMANDS = 2;
59  
60      public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
61          super.init(commandListservManager, configuration);
62          xmlResources = initXMLResources(new String[]{"header", "error", "admincommands"});
63      }
64  
65      /***
66       * Delegate to {@link #onError}
67       * @param mail
68       * @throws MessagingException
69       */
70      public void onCommand(Mail mail) throws MessagingException {
71          onError(mail, "an unknown error occurred", "an unknown error occurred");
72      }
73  
74      /***
75       * An error occurred, send a message with the following text resources:
76       * <ul>
77       *  <li>{@link #HEADER}
78       *  <li>{@link #ERROR}
79       *  <li>{@link #ADMIN_COMMANDS}
80       * </ul>
81       *
82       * @param subject the subject of the message to send
83       * @param mail
84       * @param errorMessage
85       */
86      public void onError(Mail mail, String subject, String errorMessage) throws MessagingException {
87  
88          Properties props = getStandardProperties();
89          props.put("ERROR_MESSAGE", errorMessage);
90  
91          StringBuffer plainTextMessage = new StringBuffer();
92          String header = xmlResources[HEADER].getString("text", props);
93          plainTextMessage.append(header);
94  
95          String errorText = xmlResources[ERROR].getString("text", props);
96          plainTextMessage.append(errorText);
97  
98          String adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
99          plainTextMessage.append(adminCommands);
100 
101         sendStandardReply(mail, subject, plainTextMessage.toString(), null);
102     }
103 }