From 11b2c7e8985294679eb315da9d78cf17c87bcca3 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Wed, 8 Jan 2020 23:49:31 +0000 Subject: #63955 - HMEFContentsExtractor fails to extract content from winmail.dat fixed integration test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872523 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/stress/HMEFFileHandler.java | 56 ++++++++++++++++++---- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'src/integrationtest/org/apache/poi') diff --git a/src/integrationtest/org/apache/poi/stress/HMEFFileHandler.java b/src/integrationtest/org/apache/poi/stress/HMEFFileHandler.java index c61a001376..908600979f 100644 --- a/src/integrationtest/org/apache/poi/stress/HMEFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/HMEFFileHandler.java @@ -18,35 +18,57 @@ package org.apache.poi.stress; import static org.junit.Assert.assertNotNull; +import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.util.Arrays; import org.apache.poi.hmef.HMEFMessage; import org.apache.poi.hmef.attribute.MAPIAttribute; import org.apache.poi.hmef.attribute.MAPIStringAttribute; +import org.apache.poi.hmef.attribute.TNEFAttribute; +import org.apache.poi.hmef.attribute.TNEFProperty; +import org.apache.poi.hsmf.datatypes.MAPIProperty; +import org.apache.poi.poifs.filesystem.FileMagic; +import org.apache.poi.util.LittleEndian; import org.junit.Test; public class HMEFFileHandler extends AbstractFileHandler { + @Override + public void handleExtracting(File file) throws Exception { + FileMagic fm = FileMagic.valueOf(file); + if (fm == FileMagic.OLE2) { + super.handleExtracting(file); + } + } + @Override public void handleFile(InputStream stream, String path) throws Exception { HMEFMessage msg = new HMEFMessage(stream); - + // list all properties StringBuilder props = new StringBuilder(); for(MAPIAttribute att : msg.getMessageMAPIAttributes()) { props.append(att.getType()).append(": ").append(MAPIStringAttribute.getAsString( att)).append("\n"); } - + // there are two test-files that have no body... - if(!msg.getSubject().equals("Testing TNEF Message") && !msg.getSubject().equals("TNEF test message with attachments")) { - assertNotNull("Had: " + msg.getBody() + ", " + msg.getSubject() + ", " + msg.getAttachments() + ": " + props, - msg.getBody()); + String[] HTML_BODY = { + "Testing TNEF Message", "TNEF test message with attachments", "Test" + }; + String bodyStr; + if(Arrays.asList(HTML_BODY).contains(msg.getSubject())) { + MAPIAttribute bodyHtml = msg.getMessageMAPIAttribute(MAPIProperty.BODY_HTML); + assertNotNull(bodyHtml); + bodyStr = new String(bodyHtml.getData(), getEncoding(msg)); + } else { + bodyStr = msg.getBody(); } - assertNotNull("Had: " + msg.getBody() + ", " + msg.getSubject() + ", " + msg.getAttachments() + ": " + props, - msg.getSubject()); + assertNotNull("Body is not set", bodyStr); + assertNotNull("Subject is not set", msg.getSubject()); } - + // a test-case to test this locally without executing the full TestAllFiles @Test public void test() throws Exception { @@ -55,4 +77,22 @@ public class HMEFFileHandler extends AbstractFileHandler { handleFile(stream, path); } } + + private String getEncoding(HMEFMessage tnefDat) { + TNEFAttribute oemCP = tnefDat.getMessageAttribute(TNEFProperty.ID_OEMCODEPAGE); + MAPIAttribute cpId = tnefDat.getMessageMAPIAttribute(MAPIProperty.INTERNET_CPID); + int codePage = 1252; + if (oemCP != null) { + codePage = LittleEndian.getInt(oemCP.getData()); + } else if (cpId != null) { + codePage = LittleEndian.getInt(cpId.getData()); + } + switch (codePage) { + // see http://en.wikipedia.org/wiki/Code_page for more + case 1252: return "Windows-1252"; + case 20127: return "US-ASCII"; + default: return "cp"+codePage; + } + } + } -- cgit v1.2.3