#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
This commit is contained in:
Andreas Beeker 2020-01-08 23:49:31 +00:00
parent f7fe4b0d59
commit 11b2c7e898
2 changed files with 65 additions and 25 deletions

View File

@ -18,16 +18,31 @@ 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);
@ -39,12 +54,19 @@ public class HMEFFileHandler extends AbstractFileHandler {
}
// 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
@ -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;
}
}
}

View File

@ -146,7 +146,7 @@ public final class ExtractorFactory {
} catch (NotOLE2FileException ne) {
// ensure file-handle release
IOUtils.closeQuietly(fs);
throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file");
throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file", ne);
} catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { // NOSONAR
// ensure file-handle release
IOUtils.closeQuietly(fs);