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