mirror of https://github.com/apache/poi.git
#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:
parent
f7fe4b0d59
commit
11b2c7e898
|
@ -18,16 +18,31 @@ package org.apache.poi.stress;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.apache.poi.hmef.HMEFMessage;
|
import org.apache.poi.hmef.HMEFMessage;
|
||||||
import org.apache.poi.hmef.attribute.MAPIAttribute;
|
import org.apache.poi.hmef.attribute.MAPIAttribute;
|
||||||
import org.apache.poi.hmef.attribute.MAPIStringAttribute;
|
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;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class HMEFFileHandler extends AbstractFileHandler {
|
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
|
@Override
|
||||||
public void handleFile(InputStream stream, String path) throws Exception {
|
public void handleFile(InputStream stream, String path) throws Exception {
|
||||||
HMEFMessage msg = new HMEFMessage(stream);
|
HMEFMessage msg = new HMEFMessage(stream);
|
||||||
|
@ -39,12 +54,19 @@ public class HMEFFileHandler extends AbstractFileHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
// there are two test-files that have no body...
|
// there are two test-files that have no body...
|
||||||
if(!msg.getSubject().equals("Testing TNEF Message") && !msg.getSubject().equals("TNEF test message with attachments")) {
|
String[] HTML_BODY = {
|
||||||
assertNotNull("Had: " + msg.getBody() + ", " + msg.getSubject() + ", " + msg.getAttachments() + ": " + props,
|
"Testing TNEF Message", "TNEF test message with attachments", "Test"
|
||||||
msg.getBody());
|
};
|
||||||
|
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,
|
assertNotNull("Body is not set", bodyStr);
|
||||||
msg.getSubject());
|
assertNotNull("Subject is not set", msg.getSubject());
|
||||||
}
|
}
|
||||||
|
|
||||||
// a test-case to test this locally without executing the full TestAllFiles
|
// a test-case to test this locally without executing the full TestAllFiles
|
||||||
|
@ -55,4 +77,22 @@ public class HMEFFileHandler extends AbstractFileHandler {
|
||||||
handleFile(stream, path);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ public final class ExtractorFactory {
|
||||||
} catch (NotOLE2FileException ne) {
|
} catch (NotOLE2FileException ne) {
|
||||||
// ensure file-handle release
|
// ensure file-handle release
|
||||||
IOUtils.closeQuietly(fs);
|
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
|
} catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { // NOSONAR
|
||||||
// ensure file-handle release
|
// ensure file-handle release
|
||||||
IOUtils.closeQuietly(fs);
|
IOUtils.closeQuietly(fs);
|
||||||
|
|
Loading…
Reference in New Issue