Author: echatellier Date: 2014-07-01 16:08:48 +0200 (Tue, 01 Jul 2014) New Revision: 305 Url: http://forge.codelutin.com/projects/faxtomail/repository/revisions/305 Log: Add try/catch (error) if html content can't converted to image Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java Modified: trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java =================================================================== --- trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java 2014-07-01 13:42:59 UTC (rev 304) +++ trunk/faxtomail-ui-web/src/main/java/com/franciaflex/faxtomail/web/job/MailFilterJob.java 2014-07-01 14:08:48 UTC (rev 305) @@ -25,6 +25,7 @@ */ import static org.nuiton.i18n.I18n.t; +import gui.ava.html.image.generator.HtmlImageGenerator; import java.io.File; import java.io.FileInputStream; @@ -58,16 +59,6 @@ import javax.mail.internet.MimeUtility; import javax.xml.parsers.ParserConfigurationException; -import com.franciaflex.faxtomail.persistence.entities.FaxToMailUser; -import com.franciaflex.faxtomail.persistence.entities.MailFilter; - -import com.franciaflex.faxtomail.services.FaxToMailServiceUtils; -import com.itextpdf.text.Document; -import com.itextpdf.text.DocumentException; -import com.itextpdf.text.PageSize; -import com.itextpdf.text.Paragraph; -import com.itextpdf.text.pdf.PdfWriter; -import gui.ava.html.image.generator.HtmlImageGenerator; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; @@ -79,6 +70,7 @@ import org.nuiton.util.StringUtil; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; +import org.xml.sax.SAXException; import com.franciaflex.faxtomail.persistence.entities.Attachment; import com.franciaflex.faxtomail.persistence.entities.AttachmentFile; @@ -90,14 +82,21 @@ import com.franciaflex.faxtomail.persistence.entities.EmailAccount; import com.franciaflex.faxtomail.persistence.entities.EmailImpl; import com.franciaflex.faxtomail.persistence.entities.ExtensionCommand; +import com.franciaflex.faxtomail.persistence.entities.FaxToMailUser; +import com.franciaflex.faxtomail.persistence.entities.MailFilter; import com.franciaflex.faxtomail.persistence.entities.MailFolder; import com.franciaflex.faxtomail.services.DecoratorService; import com.franciaflex.faxtomail.services.FaxToMailServiceContext; +import com.franciaflex.faxtomail.services.FaxToMailServiceUtils; import com.franciaflex.faxtomail.services.service.ClientService; import com.franciaflex.faxtomail.services.service.ConfigurationService; import com.franciaflex.faxtomail.services.service.EmailService; import com.franciaflex.faxtomail.services.service.MailFolderService; -import org.xml.sax.SAXException; +import com.itextpdf.text.Document; +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.PageSize; +import com.itextpdf.text.Paragraph; +import com.itextpdf.text.pdf.PdfWriter; /** * @author Kevin Morin (Code Lutin) @@ -221,7 +220,7 @@ List<String> modifiedProperties = new ArrayList<>(); - if (log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Object: " + message.getSubject()); } @@ -243,7 +242,7 @@ String recipient = address.toString(); - // some reciepient are like "toto tutu<toto.tutu73@gmail.com>" + // some recipient are like "toto tutu<toto.tutu73@gmail.com>" // the regex is to extract email address from it recipient = recipient.replaceFirst("^.*<(.*)>$", "$1"); recipient = recipient.toLowerCase(); @@ -377,8 +376,13 @@ if (message.isMimeType("multipart/*")) { String htmlContent = decomposeMultipartEmail(attachments, message, email, emailService); if (htmlContent != null) { + if (log.isDebugEnabled()) { + log.debug("Converting html content to pdf : " + message.getSubject()); + } Attachment attachment = convertHTMLToPdf(attachments, htmlContent, t("faxtomail.email.content.attachment.fileName")); - attachments.add(attachment); + if (attachment != null) { + attachments.add(attachment); + } } } else { @@ -537,7 +541,7 @@ /** * Convert attachment to pdf. * - * @param attachment attchment to convert + * @param attachment attachment to convert * @param extensionCommand command * @return * @throws IOException @@ -594,13 +598,13 @@ } // cleanup - //file.delete(); - //outfile.delete(); + file.delete(); + outfile.delete(); return attachment; } - public Attachment convertTextToPdf(String content, String name) throws IOException, DocumentException { + protected Attachment convertTextToPdf(String content, String name) throws IOException, DocumentException { File target = File.createTempFile("faxtomail-", ".tmp"); target.deleteOnExit(); @@ -626,52 +630,73 @@ attachment.setOriginalFile(attachmentFileNew); attachment.setOriginalFileName(name); attachment.setAddedByUser(false); + + target.delete(); return attachment; } - public Attachment convertHTMLToPdf(List<Attachment> attachments, String content, String name) throws IOException, ParserConfigurationException, SAXException, DocumentException { + /** + * Convert html content to image. + * + * @param attachments attachment content to link content to + * @param content html content + * @param name result attachment name + * @return image attachment or {@code null} if content can't be converted + * @throws IOException + */ + protected Attachment convertHTMLToPdf(List<Attachment> attachments, String content, String name) throws IOException { File target = File.createTempFile("faxtomail-", ".png"); target.deleteOnExit(); - content = content.replaceAll("<meta (.*?)>(</meta>)?", ""); - for (Attachment attachment : attachments) { - String key = attachment.getContentId(); - if (key == null) { - key = attachment.getOriginalFileName(); + Attachment result = null; + try { + content = content.replaceAll("<meta (.*?)>(</meta>)?", ""); + for (Attachment attachment : attachments) { + String key = attachment.getContentId(); + if (key == null) { + key = attachment.getOriginalFileName(); + } + + // get file content + AttachmentFile attachmentFile = attachment.getOriginalFile(); + File file = attachmentFile.getFile(); + + // replace the inline attachments with the extracted attachment file url + // match les patterns: + // <td background="cid:bg.gif" height="52"> + // <img border=0 src="cid:bg.gif" /> + // <img src='cid:5e9ef859-ea65-4f9b-a9fa-30d4a2c5837c' + content = content.replaceAll("(\\w+)=([\"'])cid:" + Pattern.quote(key) + "([\"'])", "$1=$2file://" + file.getAbsolutePath() + "$3"); + + if (log.isDebugEnabled()) { + log.debug("Mapping attachment id " + key + " to file " + file.getAbsolutePath()); + } } - - // get file content - AttachmentFile attachmentFile = attachment.getOriginalFile(); - File file = attachmentFile.getFile(); - - // replace the inline attachments with the extracted attachment file url - // match les patterns: - // <td background="cid:bg.gif" height="52"> - // <img border=0 src="cid:bg.gif" /> - // <img src='cid:5e9ef859-ea65-4f9b-a9fa-30d4a2c5837c' - content = content.replaceAll("(\\w+)=([\"'])cid:" + Pattern.quote(key) + "([\"'])", "$1=$2file://" + file.getAbsolutePath() + "$3"); - - if (log.isDebugEnabled()) { - log.debug("Mapping attachment id " + key + " to file " + file.getAbsolutePath()); + + HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); + imageGenerator.loadHtml(content); + imageGenerator.saveAsImage(target); + + // convert content to blob + EmailService emailService = serviceContext.newService(EmailService.class); + AttachmentFile attachmentFileNew = emailService.getAttachmentFileFromStream(new FileInputStream(target)); + attachmentFileNew.setFilename(name + ".png"); + + result = new AttachmentImpl(); + result.setOriginalFile(attachmentFileNew); + result.setOriginalFileName(name); + result.setAddedByUser(false); + } catch (OutOfMemoryError er) { + // certains mails (spam) très compliqué ne peuvent pas être convertit en images + if (log.isWarnEnabled()) { + log.warn("Can't convert html content to image", er); } } - HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); - imageGenerator.loadHtml(content); - imageGenerator.saveAsImage(target); + target.delete(); - // convert content to blob - EmailService emailService = serviceContext.newService(EmailService.class); - AttachmentFile attachmentFileNew = emailService.getAttachmentFileFromStream(new FileInputStream(target)); - attachmentFileNew.setFilename(name + ".png"); - - Attachment attachment = new AttachmentImpl(); - attachment.setOriginalFile(attachmentFileNew); - attachment.setOriginalFileName(name); - attachment.setAddedByUser(false); - - return attachment; + return result; } }