Source code: com/dghda/kent/actions/EmailAction.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent.actions;
21
22 import java.io.*;
23 import java.util.*;
24
25 import javax.mail.*;
26 import javax.mail.internet.*;
27
28 import com.dghda.kent.*;
29 import com.dghda.module.*;
30
31 public class EmailAction extends BaseKentModule implements ReportAction {
32 private static class BufferedDataSource implements javax.activation.DataSource {
33 public BufferedDataSource (String name, ReportTransform transform, Properties config) {
34 m_Name = name;
35 m_Transform = transform;
36 m_Config = config;
37 }
38 public String getContentType() {
39 return m_Transform.getContentType (m_Config);
40 }
41 public InputStream getInputStream() {
42 return new ByteArrayInputStream (m_Contents.toByteArray());
43 }
44 public String getName() {
45 return m_Name;
46 }
47 public OutputStream getOutputStream() {
48 return m_Contents;
49 }
50 private String m_Name;
51 private ReportTransform m_Transform;
52 private Properties m_Config;
53 private ByteArrayOutputStream m_Contents = new ByteArrayOutputStream();
54 };
55
56 public static final String REPORT_ID = "com.dghda.kent.actions.EmailAction";
57 public static final String FORMAT_ID = (REPORT_ID + ".format");
58 public static final String RECIPIENTS = (REPORT_ID + ".recipients");
59
60 public EmailAction (ReportEngine engine) {
61 super (REPORT_ID, "Email Report", "Sends a report in an email",
62 new Module.ModuleVersion (0, 1, "Duane Griffin"), engine);
63 }
64
65 /**
66 Return the email delivery parameters.
67 These include the destination(s) to send the email, and the format to send the report in.
68 */
69 public String getConfigurationOptions (Properties config) {
70 StringBuffer buffer = new StringBuffer();
71 buffer.append (ReportEngine.CONFIG_SPEC_START);
72
73 // Add input for recipient(s) address(es)
74 buffer.append (" <INPUT ID=\"");
75 buffer.append (RECIPIENTS);
76 buffer.append ("\" Type=\"text\" Label=\"Email address(es) to send to\"/>\n");
77
78 // Add option to select report format
79 String user = config.getProperty (KentModule.AUTHENTICATED_USER);
80 List transforms = getReportEngine().getAvailableTransforms (user);
81 buffer.append (getReportEngine().createModuleSelect (transforms, FORMAT_ID,
82 ReportEngine.SELECT_STYLE_COMBO,
83 "Select the mailed report's format", 1));
84
85 // TODO: Handle configuration options within the selected format (how?!?)
86 buffer.append (ReportEngine.CONFIG_SPEC_END);
87 return buffer.toString();
88 }
89
90 /** Sends the given report to the specified email address(es). */
91 public ReportAction.ActionResult performAction (String template, String data, Properties config) {
92 String user = config.getProperty (KentModule.AUTHENTICATED_USER);
93 ReportAction.ActionResult result = new ReportAction.ActionResult();
94
95 // Find the format they want the results in, or use plain text if none specified
96 String format = config.getProperty (FORMAT_ID);
97 if (format == null)
98 format = com.dghda.kent.transforms.TextTransform.TRANSFORM_ID;
99
100 // Find the selected format, or use plain text transform if the selected one is invalid
101 ReportTransform transform = getReportEngine().getTransform (format, user);
102 if (transform == null) {
103 getReportEngine().getEnvironment().log ("Email format selected " + format + " was invalid");
104 transform = new com.dghda.kent.transforms.TextTransform (getReportEngine());
105 }
106
107 // Parse the report template
108 String title = ("Report " + config.getProperty (KentModule.REPORT_ID) + " results");
109 try {
110 ReportTemplate parsedTemplate = ReportTemplate.constructReportTemplate (getReportEngine(), template);
111 title = parsedTemplate.getTitle();
112 } catch (Exception exc) {
113 getReportEngine().getEnvironment().log ("An error occurred parsing a report template", exc);
114 }
115
116 String recipients = config.getProperty (RECIPIENTS);
117 try {
118
119 // Create the email message
120 MimeMessage msg = constructMessage (title, recipients);
121
122 // Copy the contents of the report into a custom DataSource
123 BufferedDataSource source = new BufferedDataSource (title, transform, config);
124 transform.transform (source.getOutputStream(), template, data, config);
125
126 // Add the report to the message
127 msg.setDataHandler (new javax.activation.DataHandler (source));
128
129 // Send the message
130 Transport.send (msg);
131 } catch (Exception exc) {
132 getReportEngine().getEnvironment().log ("An error occurred attempting to email a report to the following users: " + recipients);
133 result.failed ("Error constructing and sending email containing report", exc);
134 }
135
136 return result;
137 }
138
139 /**
140 Create an email message to the given address with the given subject.
141 @param title The subject line of the email.
142 @param addresses The recipient(s) of the message.
143 */
144 protected MimeMessage constructMessage (String title, String addresses) throws MessagingException {
145 Session session = Session.getDefaultInstance (getReportEngine().getEnvironment().getProperties());
146 MimeMessage result = new MimeMessage (session);
147 result.addRecipients (Message.RecipientType.TO, addresses);
148 result.setSubject (title);
149 return result;
150 }
151 }