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;
23
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.james.util.XMLResources;
26 import org.apache.oro.text.regex.MalformedPatternException;
27 import org.apache.oro.text.regex.Pattern;
28 import org.apache.oro.text.regex.Perl5Compiler;
29 import org.apache.oro.text.regex.Perl5Matcher;
30 import org.apache.oro.text.regex.StringSubstitution;
31 import org.apache.oro.text.regex.Util;
32
33 import javax.mail.MessagingException;
34
35
36 /**
37 * CommandListservFooter is based on the AddFooter mailet.
38 *
39 * It is used by the {@link CommandListservProcessor} to inject a footer into mailing list.
40 * <br />
41 * <br />
42 *
43 * @version CVS $Revision: 486089 $ $Date: 2006-12-12 10:42:07 +0000 (Tue, 12 Dec 2006) $
44 * @since 2.2.0
45 * @see XMLResources
46 */
47 public class CommandListservFooter extends AbstractAddFooter {
48
49 protected String footerText;
50 protected String footerHtml;
51
52 /**
53 * The list serv manager
54 */
55 protected ICommandListservManager commandListservManager;
56
57 /**
58 * For matching
59 */
60 protected Perl5Compiler perl5Compiler = new Perl5Compiler();
61 protected Pattern insertPattern;
62 protected Pattern newlinePattern;
63
64 //For resources
65 protected XMLResources[] xmlResources = new XMLResources[2];
66
67 protected static final int TEXT_PLAIN = 0;
68 protected static final int TEXT_HTML = 1;
69
70 public CommandListservFooter(ICommandListservManager commandListservManager) {
71 this.commandListservManager = commandListservManager;
72 try {
73 insertPattern = perl5Compiler.compile("</body>\\s*</html>", Perl5Compiler.CASE_INSENSITIVE_MASK);
74 newlinePattern = perl5Compiler.compile("\r\n|\n", Perl5Compiler.CASE_INSENSITIVE_MASK);
75 } catch (MalformedPatternException e) {
76 throw new IllegalStateException("Unable to parse regexps: " + e.getMessage());
77 }
78 }
79
80 /**
81 * Initialize the mailet
82 */
83 public void init() throws MessagingException {
84 try {
85 xmlResources = commandListservManager.initXMLResources(new String[]{"footer", "footer_html"});
86 } catch (ConfigurationException e) {
87 throw new MessagingException(e.getMessage(), e);
88 }
89 }
90
91 /**
92 * Return a string describing this mailet.
93 *
94 * @return a string describing this mailet
95 */
96 public String getMailetInfo() {
97 return "CommandListservFooter Mailet";
98 }
99
100
101 /**
102 * Get and cache the footer text
103 *
104 * @return the footer text
105 * @see XMLResources
106 */
107 protected String getFooterText() {
108 if (footerText == null) {
109 footerText = getFormattedText(TEXT_PLAIN);
110 }
111 return footerText;
112 }
113
114 /**
115 * Get and cache the footer html text
116 *
117 * @return the footer text
118 * @see XMLResources
119 */
120 protected String getFooterHTML() {
121 if (footerHtml == null) {
122 String footerText = getFormattedText(TEXT_HTML);
123 footerHtml = Util.substitute(new Perl5Matcher(),
124 newlinePattern,
125 new StringSubstitution(" <br />"),
126 footerText,
127 Util.SUBSTITUTE_ALL);
128 }
129 return footerHtml;
130 }
131
132 /**
133 * @see XMLResources#getString(String)
134 * @param index either {@link #TEXT_PLAIN} or {@link #TEXT_HTML}
135 * @return a formatted text with the proper list and domain
136 */
137 protected String getFormattedText(int index) {
138 return xmlResources[index].getString("text");
139 }
140 }