unicorn commit: ~ refactored email actions in Mail class and fixed a charset issue when results are sent by emails

changeset:   1624:e2851c6faf30
parent:      1622:d6c8490cbe6c
user:        Thomas Gambet <tgambet@w3.org>
date:        Thu Feb 24 17:05:55 2011 -0500
files:       src/org/w3c/unicorn/action/LanguageAction.java src/org/w3c/unicorn/output/MailOutputModule.java src/org/w3c/unicorn/util/Mail.java
description:
~ refactored email actions in Mail class and fixed a charset issue when results are sent by emails


diff -r d6c8490cbe6c -r e2851c6faf30 src/org/w3c/unicorn/action/LanguageAction.java
--- a/src/org/w3c/unicorn/action/LanguageAction.java	Thu Feb 24 13:23:58 2011 -0500
+++ b/src/org/w3c/unicorn/action/LanguageAction.java	Thu Feb 24 17:05:55 2011 -0500
@@ -325,7 +325,7 @@
 			
 			Mail mailer = new Mail();
 			try {
-				mailer.sendMail(recipients, subject, outputFormaters, contextObjects);
+				mailer.sendMail(recipients, subject, outputFormaters, contextObjects, false);
 			} catch (UnicornException e) {
 				logger.error(e.getMessage(), e);
 			}
diff -r d6c8490cbe6c -r e2851c6faf30 src/org/w3c/unicorn/output/MailOutputModule.java
--- a/src/org/w3c/unicorn/output/MailOutputModule.java	Thu Feb 24 13:23:58 2011 -0500
+++ b/src/org/w3c/unicorn/output/MailOutputModule.java	Thu Feb 24 17:05:55 2011 -0500
@@ -3,11 +3,6 @@
 // Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.output;
 
-import java.io.ByteArrayOutputStream;
-import java.io.CharArrayWriter;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -15,17 +10,12 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.ArrayList;
-import java.util.Properties;
 import org.w3c.unicorn.util.Message;
 
-import javax.mail.*;
-import javax.mail.internet.*;
 import org.w3c.unicorn.UnicornCall;
 import org.w3c.unicorn.exceptions.UnicornException;
+import org.w3c.unicorn.util.Mail;
 import org.w3c.unicorn.util.Property;
-import org.w3c.unicorn.util.UnicornAuthenticator;
-
-import javax.mail.internet.MimeBodyPart;
 
 /**
  * This module allows to send the response by mail.
@@ -87,82 +77,30 @@
 			return;
 		if (mailOutputFormaters == null && mailOutputFormaters.get(0) == null)
 			return;
+
+		ArrayList<Message> messages = ((ArrayList<Message>) mapOfStringObject.get("messages"));
+		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss (Z)", new Locale(outputParameters.get("lang")));
 		
+		messages.add(new Message(Message.INFO, "$message_mail_date", null, dateFormat.format(new Date())));
+		
+		mapOfStringObject.put("baseUri", Property.get("UNICORN_URL"));
+		
+		UnicornCall uniCall = (UnicornCall) mapOfStringObject.get("unicorncall");
+		boolean passed = uniCall.isPassed();
+		
+		String subject = "[Unicorn] ";
+		if (passed)
+			subject += "SUCCEEDED: ";
+		else 
+			subject += "FAILED: ";
+		subject += "Task \"" + uniCall.getTask().getLongName(outputParameters.get("lang")) + "\" for \"" + uniCall.getDocumentName() + "\"";
+		
+		Mail sender = new Mail();
 		try {
-			ArrayList<Message> messages = ((ArrayList<Message>) mapOfStringObject.get("messages"));
-			SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss (Z)", new Locale(outputParameters.get("lang")));
-			
-			messages.add(new Message(Message.INFO, "$message_mail_date", null, dateFormat.format(new Date())));
-			
-			mapOfStringObject.put("baseUri", Property.get("UNICORN_URL"));
-			
-			Properties mailProps = Property.getProps("mail.properties");
-			Authenticator auth = new UnicornAuthenticator(mailProps.getProperty("unicorn.mail.username"), mailProps.getProperty("unicorn.mail.password"));
-			Session session = Session.getDefaultInstance(mailProps, auth);
-		    
-			boolean debug = false;
-			if ("true".equals(mailProps.getProperty("unicorn.mail.debug")))
-				debug = true;
-			
-			session.setDebug(debug);
-			
-			UnicornCall uniCall = (UnicornCall) mapOfStringObject.get("unicorncall");
-			boolean passed = uniCall.isPassed();
-			
-			String subject = "[Unicorn] ";
-			if (passed)
-				subject += "SUCCEEDED: ";
-			else 
-				subject += "FAILED: ";
-			subject += "Task \"" + uniCall.getTask().getLongName(outputParameters.get("lang")) + "\" for \"" + uniCall.getDocumentName() + "\"";
-			
-			javax.mail.Message msg = new MimeMessage(session);
-		    InternetAddress addressFrom = new InternetAddress(mailProps.getProperty("unicorn.mail.from"), "Unicorn");
-			msg.setFrom(addressFrom);
-			msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));
-			msg.setSubject(subject);
-			
-			if (mailOutputFormaters.size() > 1) {
-				// New multipart message
-				Multipart mp = new MimeMultipart("alternative");
-				for (OutputFormater outputFormater : mailOutputFormaters) {
-					MimeBodyPart bodyPart = new MimeBodyPart();
-					
-					//CharArrayWriter writer = new CharArrayWriter();
-					ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-					OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
-					
-					outputFormater.produceOutput(mapOfStringObject, outputStreamWriter);
-					outputStreamWriter.close();
-					byteArrayOutputStream.close();
-					bodyPart.setContent(byteArrayOutputStream.toString("UTF-8"), outputFormater.getMimeType());
-					bodyPart.setHeader("Content-Type", outputFormater.getMimeType() + "; charset=UTF-8");
-					bodyPart.setHeader("Content-Transfer-Encoding", "8bit");
-					mp.addBodyPart(bodyPart);
-				}
-				msg.setContent(mp);
-			} else {
-				CharArrayWriter writer = new CharArrayWriter();
-				mailOutputFormaters.get(0).produceOutput(mapOfStringObject, writer);
-				writer.close();
-				msg.setContent(writer.toString(), mailOutputFormaters.get(0).getMimeType());
-			}
-			
-			Transport.send(msg);
-			
-		} catch (AddressException e) {
-			// TODO Auto-generated catch block
+			sender.sendMail(new String[] {recipient}, subject, mailOutputFormaters, mapOfStringObject, true);
+		} catch (UnicornException e) {
 			e.printStackTrace();
-		} catch (MessagingException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (UnsupportedEncodingException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (IOException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} 
+		}
 	}
 
 	public void produceError(Map<String, Object> mapOfStringObject, final Writer aWriter) {
diff -r d6c8490cbe6c -r e2851c6faf30 src/org/w3c/unicorn/util/Mail.java
--- a/src/org/w3c/unicorn/util/Mail.java	Thu Feb 24 13:23:58 2011 -0500
+++ b/src/org/w3c/unicorn/util/Mail.java	Thu Feb 24 17:05:55 2011 -0500
@@ -32,7 +32,7 @@
 
 	private static Log logger = LogFactory.getLog(Mail.class);
 	
-	public void sendMail(String[] recipients, String subject, List<OutputFormater> outputFormaters, Map<String, Object> contextObjects) throws UnicornException {
+	public void sendMail(String[] recipients, String subject, List<OutputFormater> outputFormaters, Map<String, Object> contextObjects, boolean alternative) throws UnicornException {
 		
 		try {
 			Properties mailProps = Property.getProps("mail.properties");
@@ -72,7 +72,11 @@
 			
 			if (outputFormaters.size() > 1) {
 				// New multipart message
-				MimeMultipart mp = new MimeMultipart();
+				MimeMultipart mp;
+				if (alternative)
+					mp = new MimeMultipart("alternative");
+				else
+					mp = new MimeMultipart();
 				for (OutputFormater outputFormater : outputFormaters) {		
 					MimeBodyPart bodyPart = new MimeBodyPart();
 					if (outputFormater instanceof FileOutputFormater)
@@ -90,6 +94,7 @@
 					}
 					
 					bodyPart.setContent(byteArrayOutputStream.toString("UTF-8"), outputFormater.getMimeType() + "; charset=UTF-8");
+					bodyPart.setHeader("Content-Type", outputFormater.getMimeType() + "; charset=UTF-8");
 					bodyPart.setHeader("Content-Transfer-Encoding", "7bit");
 					mp.addBodyPart(bodyPart);
 				}
@@ -119,10 +124,10 @@
 		}
 	}
 	
-	public void sendMail(String[] recipients, String subject, OutputFormater outputFormater, Map<String, Object> contextObjects) throws UnicornException {
+	public void sendMail(String[] recipients, String subject, OutputFormater outputFormater, Map<String, Object> contextObjects, boolean alternative) throws UnicornException {
 		List<OutputFormater> outputFormaters = new ArrayList<OutputFormater>();
 		outputFormaters.add(outputFormater);
 		
-		sendMail(recipients, subject, outputFormaters, contextObjects);
+		sendMail(recipients, subject, outputFormaters, contextObjects, alternative);
 	}
 }

Received on Friday, 25 February 2011 16:08:15 UTC