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