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