Changeset 566:5f3c70de152c
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
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 $ |
| 2 | 2 | // Author: Thomas Gambet |
| 3 | 3 | // (c) COPYRIGHT MIT, ERCIM and Keio, 2009. |
| … |
… |
|
| 9 | 9 | import java.io.Writer; |
| 10 | 10 | import java.util.Date; |
| | 11 | import java.util.List; |
| 11 | 12 | import java.util.Map; |
| 12 | 13 | import java.util.ArrayList; |
| 13 | 14 | import java.util.Properties; |
| 14 | 15 | import org.w3c.unicorn.util.Message; |
| | 16 | |
| 15 | 17 | import javax.mail.*; |
| 16 | 18 | import javax.mail.internet.*; |
| … |
… |
|
| 19 | 21 | import org.w3c.unicorn.util.Property; |
| 20 | 22 | import org.w3c.unicorn.util.UnicornAuthenticator; |
| | 23 | |
| | 24 | import javax.mail.internet.MimeBodyPart; |
| 21 | 25 | |
| 22 | 26 | /** |
| … |
… |
|
| 27 | 31 | public class MailOutputModule extends OutputModule { |
| 28 | 32 | |
| 29 | | private OutputFormater firstOutputFormater; |
| 30 | | |
| 31 | | private OutputFormater mailOutputFormater; |
| 32 | | |
| 33 | | private String mimeType; |
| | 33 | private List<OutputFormater> mailOutputFormaters; |
| 34 | 34 | |
| 35 | 35 | private String recipient; |
| | 36 | |
| | 37 | private String mailFormat; |
| 36 | 38 | |
| 37 | 39 | public MailOutputModule(Map<String, String> mapOfOutputParameters, Map<String, String> mapOfSpecificParameters) { |
| 38 | 40 | super(mapOfOutputParameters, mapOfSpecificParameters); |
| 39 | 41 | |
| 40 | | recipient = mapOfSpecificParameters.get("email"); |
| 41 | | mimeType = mapOfOutputParameters.get("mimetype"); |
| | 42 | recipient = specificParameters.get("email"); |
| 42 | 43 | |
| 43 | | String format = mapOfOutputParameters.get("format"); |
| 44 | | String lang = mapOfOutputParameters.get("lang"); |
| | 44 | mailFormat = specificParameters.get("format"); |
| 45 | 45 | |
| 46 | | firstOutputFormater = OutputFactory.createOutputFormater(format, lang, mimeType); |
| | 46 | mailOutputFormaters = new ArrayList<OutputFormater>(); |
| | 47 | |
| | 48 | mailOutputFormaters.add(OutputFactory.createOutputFormater("text", outputParameters.get("lang"))); |
| 47 | 49 | |
| 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 | } |
| 52 | 53 | |
| 53 | | mailOutputFormater = OutputFactory.createOutputFormater(format, lang, mimeType); |
| | 54 | if (!"text".equals(mailFormat)) { |
| | 55 | mailOutputFormaters.add(OutputFactory.createOutputFormater(mailFormat, outputParameters.get("lang"))); |
| | 56 | } |
| | 57 | |
| 54 | 58 | } |
| 55 | 59 | |
| … |
… |
|
| 66 | 70 | messages.add(pendingMess); |
| 67 | 71 | } |
| | 72 | if (mailOutputFormaters == null && mailOutputFormaters.get(0) == null) |
| | 73 | throw new UnicornException(); |
| | 74 | |
| 68 | 75 | displayOnIndex(mapOfStringObject, aWriter); |
| 69 | 76 | messages.remove(pendingMess); |
| … |
… |
|
| 73 | 80 | public void produceOutput(Map<String, Object> mapOfStringObject, final Writer aWriter) { |
| 74 | 81 | |
| | 82 | if (recipient == null) |
| | 83 | return; |
| | 84 | if (mailOutputFormaters == null && mailOutputFormaters.get(0) == null) |
| | 85 | return; |
| 75 | 86 | |
| 76 | 87 | // TODO http://java.sun.com/developer/EJTechTips/2004/tt0625.html#1 for multipart messages |
| 77 | 88 | |
| 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 | |
| 84 | 90 | |
| 85 | 91 | 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 | |
| 86 | 95 | mapOfStringObject.put("baseUri", "http://qa-dev.w3.org:8001/unicorn/"); |
| 87 | 96 | |
| … |
… |
|
| 95 | 104 | |
| 96 | 105 | 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)); |
| 102 | 106 | |
| 103 | | // Setting the Subject and Content Type |
| 104 | 107 | UnicornCall uniCall = (UnicornCall) mapOfStringObject.get("unicorncall"); |
| 105 | 108 | boolean passed = uniCall.isPassed(); |
| … |
… |
|
| 112 | 115 | subject += "Task \"" + uniCall.getTask().getLongName(outputParameters.get("lang")) + "\" for \"" + uniCall.getDocumentName() + "\""; |
| 113 | 116 | |
| | 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)); |
| 114 | 121 | msg.setSubject(subject); |
| 115 | 122 | |
| 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 | } |
| 120 | 142 | |
| 121 | 143 | Transport.send(msg); |
| | 144 | |
| 122 | 145 | } catch (AddressException e) { |
| 123 | 146 | // TODO Auto-generated catch block |
| … |
… |
|
| 133 | 156 | |
| 134 | 157 | public void produceError(Map<String, Object> mapOfStringObject, final Writer aWriter) { |
| 135 | | if (outputParameters.get("mimetype").equals("text/html")) { |
| | 158 | if (getMimeType().equals("text/html")) { |
| 136 | 159 | displayOnIndex(mapOfStringObject, aWriter); |
| 137 | 160 | return; |
| 138 | 161 | } |
| 139 | | firstOutputFormater.produceError(mapOfStringObject, aWriter); |
| | 162 | defaultOutputFormater.produceError(mapOfStringObject, aWriter); |
| 140 | 163 | } |
| 141 | 164 | |