Changeset 566:5f3c70de152c

Show
Ignore:
Timestamp:
09/29/09 16:08:25 (4 years ago)
Author:
tgambet
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@567
Message:

refactored with new OutputModule? class
+ implemented multipart messages

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/org/w3c/unicorn/output/MailOutputModule.java

    r556 r566  
    1 // $Id: MailOutputModule.java,v 1.10 2009-09-28 16:41:18 tgambet Exp $ 
     1// $Id: MailOutputModule.java,v 1.11 2009-09-29 16:08:25 tgambet Exp $ 
    22// Author: Thomas Gambet 
    33// (c) COPYRIGHT MIT, ERCIM and Keio, 2009. 
     
    99import java.io.Writer; 
    1010import java.util.Date; 
     11import java.util.List; 
    1112import java.util.Map; 
    1213import java.util.ArrayList; 
    1314import java.util.Properties; 
    1415import org.w3c.unicorn.util.Message; 
     16 
    1517import javax.mail.*; 
    1618import javax.mail.internet.*; 
     
    1921import org.w3c.unicorn.util.Property; 
    2022import org.w3c.unicorn.util.UnicornAuthenticator; 
     23 
     24import javax.mail.internet.MimeBodyPart; 
    2125 
    2226/** 
     
    2731public class MailOutputModule extends OutputModule { 
    2832         
    29         private OutputFormater firstOutputFormater; 
    30          
    31         private OutputFormater mailOutputFormater; 
    32          
    33         private String mimeType; 
     33private List<OutputFormater> mailOutputFormaters; 
    3434         
    3535        private String recipient; 
     36         
     37        private String mailFormat; 
    3638         
    3739        public MailOutputModule(Map<String, String> mapOfOutputParameters, Map<String, String> mapOfSpecificParameters) { 
    3840                super(mapOfOutputParameters, mapOfSpecificParameters); 
    3941                 
    40                 recipient = mapOfSpecificParameters.get("email"); 
    41                 mimeType = mapOfOutputParameters.get("mimetype"); 
     42                recipient = specificParameters.get("email"); 
    4243                 
    43                 String format = mapOfOutputParameters.get("format"); 
    44                 String lang = mapOfOutputParameters.get("lang");  
     44                mailFormat = specificParameters.get("format"); 
    4545                 
    46                 firstOutputFormater = OutputFactory.createOutputFormater(format, lang, mimeType); 
     46                mailOutputFormaters = new ArrayList<OutputFormater>();  
     47                         
     48                mailOutputFormaters.add(OutputFactory.createOutputFormater("text", outputParameters.get("lang"))); 
    4749                 
    48                 if (mapOfSpecificParameters.get("format") != null) 
    49                         format = mapOfSpecificParameters.get("format"); 
    50                 if (mapOfSpecificParameters.get("mimetype") != null) 
    51                         mimeType = mapOfSpecificParameters.get("mimetype"); 
     50                if (mailFormat == null) { 
     51                        mailFormat = defaultOutputFormater.getFormat(); 
     52                } 
    5253                 
    53                 mailOutputFormater = OutputFactory.createOutputFormater(format, lang, mimeType); 
     54                if (!"text".equals(mailFormat)) { 
     55                        mailOutputFormaters.add(OutputFactory.createOutputFormater(mailFormat, outputParameters.get("lang"))); 
     56                } 
     57                 
    5458        } 
    5559         
     
    6670                        messages.add(pendingMess); 
    6771                } 
     72                if (mailOutputFormaters == null && mailOutputFormaters.get(0) == null) 
     73                        throw new UnicornException(); 
     74                 
    6875                displayOnIndex(mapOfStringObject, aWriter); 
    6976                messages.remove(pendingMess); 
     
    7380        public void produceOutput(Map<String, Object> mapOfStringObject, final Writer aWriter) { 
    7481                 
     82                if (recipient == null) 
     83                        return; 
     84                if (mailOutputFormaters == null && mailOutputFormaters.get(0) == null) 
     85                        return; 
    7586                 
    7687                // TODO http://java.sun.com/developer/EJTechTips/2004/tt0625.html#1 for multipart messages 
    7788                 
    78                  
    79                 if (recipient == null) 
    80                         return; 
    81                  
    82                 ArrayList<Message> messages = ((ArrayList<Message>) mapOfStringObject.get("messages")); 
    83                 messages.add(new Message(Message.Level.INFO, "Observation effectuée le " + new Date())); 
     89 
    8490                 
    8591                try { 
     92                        ArrayList<Message> messages = ((ArrayList<Message>) mapOfStringObject.get("messages")); 
     93                        messages.add(new Message(Message.Level.INFO, "Observation effectuée le " + new Date())); 
     94                         
    8695                        mapOfStringObject.put("baseUri", "http://qa-dev.w3.org:8001/unicorn/"); 
    8796                         
     
    95104                         
    96105                        session.setDebug(debug); 
    97                         javax.mail.Message msg = new MimeMessage(session); 
    98                      
    99                     InternetAddress addressFrom = new InternetAddress(mailProps.getProperty("unicorn.mail.from"), "Unicorn"); 
    100                         msg.setFrom(addressFrom); 
    101                         msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient)); 
    102106                         
    103                         // Setting the Subject and Content Type 
    104107                        UnicornCall uniCall = (UnicornCall) mapOfStringObject.get("unicorncall"); 
    105108                        boolean passed = uniCall.isPassed(); 
     
    112115                        subject += "Task \"" + uniCall.getTask().getLongName(outputParameters.get("lang")) + "\" for \"" + uniCall.getDocumentName() + "\""; 
    113116                         
     117                        javax.mail.Message msg = new MimeMessage(session); 
     118                    InternetAddress addressFrom = new InternetAddress(mailProps.getProperty("unicorn.mail.from"), "Unicorn"); 
     119                        msg.setFrom(addressFrom); 
     120                        msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient)); 
    114121                        msg.setSubject(subject); 
    115122                         
    116                         CharArrayWriter writer = new CharArrayWriter(); 
    117                         mailOutputFormater.produceOutput(mapOfStringObject, writer); 
    118                         writer.close(); 
    119                         msg.setContent(writer.toString(), mimeType); 
     123                        if (mailOutputFormaters.size() > 1) { 
     124                                // New multipart message 
     125                                Multipart mp = new MimeMultipart("alternative"); 
     126                                for (OutputFormater outputFormater : mailOutputFormaters) { 
     127                                        MimeBodyPart bodyPart = new MimeBodyPart(); 
     128                                        bodyPart.addHeader("Content-Type", outputFormater.getMimeType() + ", charset=UTF-8"); 
     129                                        CharArrayWriter writer = new CharArrayWriter(); 
     130                                        outputFormater.produceOutput(mapOfStringObject, writer); 
     131                                        writer.close(); 
     132                                        bodyPart.setContent(writer.toString(), outputFormater.getMimeType()); 
     133                                        mp.addBodyPart(bodyPart); 
     134                                } 
     135                                msg.setContent(mp); 
     136                        } else { 
     137                                CharArrayWriter writer = new CharArrayWriter(); 
     138                                mailOutputFormaters.get(0).produceOutput(mapOfStringObject, writer); 
     139                                writer.close(); 
     140                                msg.setContent(writer.toString(), mailOutputFormaters.get(0).getMimeType()); 
     141                        } 
    120142                         
    121143                        Transport.send(msg); 
     144                         
    122145                } catch (AddressException e) { 
    123146                        // TODO Auto-generated catch block 
     
    133156 
    134157        public void produceError(Map<String, Object> mapOfStringObject, final Writer aWriter) { 
    135                 if (outputParameters.get("mimetype").equals("text/html")) { 
     158                if (getMimeType().equals("text/html")) { 
    136159                        displayOnIndex(mapOfStringObject, aWriter); 
    137160                        return; 
    138161                } 
    139                 firstOutputFormater.produceError(mapOfStringObject, aWriter); 
     162                defaultOutputFormater.produceError(mapOfStringObject, aWriter); 
    140163        } 
    141164