// MessageComposer.sqlj // part of the webmail ACS module // written by Jin Choi // 2000-03-01 // ported to openacs by Dan Wickstrom // 2000-06-18 // This class implements some static methods for composing MIME messages. import java.sql.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import java.util.*; import javax.activation.*; import acspg.*; import nsjava.*; public class MessageComposer { private static boolean runningOutsideOracleP = false; private Pg_Query st; private static NsLog log; protected static Session s = null; public MessageComposer(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException, Throwable { int id = (new Integer(args[0])).intValue(); String tmpfile = args[1]; try { log.write("Debug","Connected."); st = new Pg_Query("subquery"); composeMimeMessage(id,tmpfile); st.releasehandle(); } catch (Exception e) { log.write("Debug", "Error running the example: " + e.getMessage()); e.printStackTrace(); } } public void composeMimeMessage(int msgId, String tmpfile) throws MessagingException, IOException, SQLException, Throwable { Vector parts = new Vector(); // vector of data handlers CLOB bodyText = null; String str; str = this.st.databaseToJavaString("select body from wm_outgoing_messages where outgoing_msg_id = " + msgId); bodyText = new CLOB(str); if (bodyText != null && bodyText.length() > 0) { ClobDataSource cds = new ClobDataSource(bodyText, "text/plain", null); parts.addElement(new DataHandler(cds)); } log.write("Debug", "adding attachments"); AttachmentsIter attIter = new AttachmentsIter(st,"select lob, content_type, filename from wm_outgoing_message_parts where outgoing_msg_id = " + msgId + " order by sort_order"); log.write("Debug", "adding attachments datasource"); while (attIter.next(st)) { BlobDataSource bds = new BlobDataSource(attIter.data(st), attIter.content_type(), attIter.filename()); parts.addElement(new DataHandler(bds)); } attIter.close(); log.write("Debug", "attachments added"); // Create new MimeMessage. if (s == null) { Properties props = new Properties(); s = Session.getDefaultInstance(props, null); log.write("Debug", "obtained session props"); } MimeMessage msg = new MimeMessage(s); // Add the headers. log.write("Debug", "db handle prior to headersiter: " + st.getPointer()); HeadersIter hIter = new HeadersIter(st,"select name, value from wm_outgoing_headers where outgoing_msg_id = " + msgId + " order by sort_order"); while (hIter.next(st)) { log.write("Debug", "writing header"); msg.addHeader(hIter.name(), hIter.value()); } hIter.close(); // Add the attachments. log.write("Debug", "adding parts"); addParts(msg, parts); // Synchronize the headers to reflect the contents. log.write("Debug", "saving changes"); msg.saveChanges(); OutputStream os = new FileOutputStream(tmpfile); // move the message into the blob log.write("Debug", "writing to tmpfile:" + tmpfile); msg.writeTo(os); } protected void addParts(MimeMessage msg, Vector parts) throws MessagingException, IOException { if (parts.size() == 0) { // This should never happen. return; } if (parts.size() > 1) { MimeMultipart msgMultiPart = new MimeMultipart(); Enumeration e = parts.elements(); while (e.hasMoreElements()) { DataHandler dh = (DataHandler) e.nextElement(); String filename = dh.getName(); MimeBodyPart bp = new MimeBodyPart(); bp.setDataHandler(dh); if (filename != null) { bp.setFileName(dh.getName()); } msgMultiPart.addBodyPart(bp); } msg.setContent(msgMultiPart); } else { // There is only one element. DataHandler dh = (DataHandler) parts.elementAt(0); String filename = dh.getName(); if (filename != null) { msg.setFileName(dh.getName()); } msg.setDataHandler(dh); } } public static void instructions() { log.write("Debug", "\nThis example tests the basic webmail message parsing\n"); log.write("Debug", "Useage:\n java MessageComposer jdbc:postgresql:database user password msg_id tmpfile [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of items, don't put anything in\nhere."); // System.exit(1); } public static void compose_message(String args[]) throws Throwable { log = new NsLog(); log.write("Debug", "openacs test of webmail port\n"); if(args.length<2) { instructions(); return; } // Now run the message composer try { MessageComposer composer = new MessageComposer(args); } catch(Exception ex) { log.write("Debug", "Exception caught.\n"+ex); ex.printStackTrace(); } } }