View Javadoc

1   // MailListener.java - emails job status updates to a user
2   // $Id: MailListener.java,v 1.5 2007/05/07 18:26:53 sjardine Exp $
3   //
4   // - basically gives an example for getting the status callbacks on a FAX job
5   //
6   // Copyright 2003, Joe Phillips <joe.phillips@innovationsw.com>
7   // Copyright 2003, Innovation Software Group LLC - http://www.innovationsw.com/
8   //
9   // This library is free software; you can redistribute it and/or
10  // modify it under the terms of the GNU Library General Public
11  // License as published by the Free Software Foundation; either
12  // version 2 of the License, or (at your option) any later version.
13  //
14  // This library is distributed in the hope that it will be useful,
15  // but WITHOUT ANY WARRANTY; without even the implied warranty of
16  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  // Library General Public License for more details.
18  //
19  // You should have received a copy of the GNU Library General Public
20  // License along with this library; if not, write to the Free
21  // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  //
23  //
24  // for information on the HylaFAX FAX server see
25  //  http://www.hylafax.org/
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      // default constructor
78      public MailListener() {
79  	properties = new Properties(System.getProperties());
80      }// MailListener
81  
82      /***
83       * 
84       * 
85       */
86      public void onSendEvent(SendEvent event) {
87  	try {
88  	    // nothing for now.
89  	} catch (Exception e) {
90  	    log.error(e.getMessage(), e);
91  	}
92      }// onSendEvent
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 		// failure? attach log file instead
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 	    // first body part
143 	    MimeBodyPart part0 = new MimeBodyPart();
144 	    part0.setText(body);
145 
146 	    // second body part
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 	    // build the message
155 	    MimeMultipart mp = new MimeMultipart();
156 	    mp.addBodyPart(part0);
157 	    mp.addBodyPart(part1);
158 	    msg.setContent(mp);
159 
160 	    // send the message
161 	    Transport.send(msg);
162 
163 	} catch (Exception e) {
164 	    log.error(e.getMessage(), e);
165 	}
166     }// onReceiveEvent
167 
168 }// MailListener
169 // MailListener.java