1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package gnu.hylafax.util;
30
31 import gnu.hylafax.job.ReceiveEvent;
32 import gnu.hylafax.job.ReceiveListener;
33 import gnu.hylafax.job.SendEvent;
34 import gnu.hylafax.job.SendListener;
35
36 import java.io.File;
37 import java.text.SimpleDateFormat;
38 import java.util.Date;
39 import java.util.Properties;
40
41 import javax.activation.DataHandler;
42 import javax.activation.FileDataSource;
43 import javax.activation.MimetypesFileTypeMap;
44 import javax.mail.Message;
45 import javax.mail.Part;
46 import javax.mail.Session;
47 import javax.mail.Transport;
48 import javax.mail.internet.MimeBodyPart;
49 import javax.mail.internet.MimeMessage;
50 import javax.mail.internet.MimeMultipart;
51
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54
55 /***
56 * This class implements an example fax job listener. It emails the status
57 * updates to a given mailbox.
58 *
59 * @author $Author: sjardine $
60 * @version $Id: MailListener.java,v 1.5 2007/05/07 18:26:53 sjardine Exp $
61 * @see gnu.hylafax.job.SendNotifier
62 * @see gnu.hylafax.job.SendEvent
63 * @see gnu.hylafax.job.ReceiveNotifier
64 * @see gnu.hylafax.job.ReceiveEvent
65 * @see gnu.hylafax.util.Notifier
66 */
67 public class MailListener implements SendListener, ReceiveListener {
68 static final SimpleDateFormat rfc822df = new SimpleDateFormat(
69 "dd MMM yyyy HH:mm:ss z");
70 static final String KEY_TO = "notifier.to";
71 static final String KEY_FROM = "notifier.from";
72
73 private Properties properties;
74
75 private final static Log log = LogFactory.getLog(MailListener.class);
76
77
78 public MailListener() {
79 properties = new Properties(System.getProperties());
80 }
81
82 /***
83 *
84 *
85 */
86 public void onSendEvent(SendEvent event) {
87 try {
88
89 } catch (Exception e) {
90 log.error(e.getMessage(), e);
91 }
92 }
93
94 /***
95 * This method is called when a fax-received event occurs. It composes an
96 * email, attaching relevant files and mails it to a target mailbox.
97 */
98 public void onReceiveEvent(ReceiveEvent event) {
99 String subject;
100 String body;
101 String cid = null;
102 Date now = new Date();
103
104 subject = "Facsimile received";
105 if ((event.getCidName() != null) && (!"".equals(event.getCidName()))) {
106 cid = event.getCidName();
107 } else if ((event.getCidNumber() != null)
108 && (!"".equals(event.getCidNumber()))) {
109 cid = event.getCidNumber();
110 }
111
112 try {
113 File f = new File(event.getFilename());
114 if (!f.exists()) {
115
116 f = new File("log" + File.separator + "c"
117 + event.getCommunicationIdentifier());
118 subject = "Facsimile failed";
119 body = "A facsimile failed to be received at " + now + "\n\n"
120 + "See the attached log file for session details.\n";
121 } else {
122 body = "The attached facsimile was received " + now + "\n";
123 }
124 if ((event.getMessage() != null)
125 && (!"".equals(event.getMessage()))) {
126 body += "The server's message is:\n\n\t" + event.getMessage();
127 }
128 if (cid != null)
129 subject += " from " + cid;
130
131 Session s = Session.getDefaultInstance(properties);
132 MimeMessage msg = new MimeMessage(s);
133 msg.addRecipients(Message.RecipientType.TO, properties
134 .getProperty(KEY_TO));
135 msg.setSubject(subject);
136 msg.addHeader("From", properties.getProperty(KEY_FROM));
137 msg.addHeader("Date", rfc822df.format(now));
138 msg
139 .addHeader("X-MailListener",
140 "$Id: MailListener.java,v 1.5 2007/05/07 18:26:53 sjardine Exp $");
141
142
143 MimeBodyPart part0 = new MimeBodyPart();
144 part0.setText(body);
145
146
147 FileDataSource fds = new FileDataSource(f);
148 fds.setFileTypeMap(new MimetypesFileTypeMap());
149 DataHandler fdh = new DataHandler(fds);
150 MimeBodyPart part1 = new MimeBodyPart();
151 part1.setDataHandler(fdh);
152 part1.setDisposition(Part.INLINE);
153 part1.setFileName(f.getName());
154
155 MimeMultipart mp = new MimeMultipart();
156 mp.addBodyPart(part0);
157 mp.addBodyPart(part1);
158 msg.setContent(mp);
159
160
161 Transport.send(msg);
162
163 } catch (Exception e) {
164 log.error(e.getMessage(), e);
165 }
166 }
167
168 }
169