You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractFileHandler.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.stress;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assume.assumeFalse;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.HashSet;
  25. import java.util.Set;
  26. import org.apache.poi.EncryptedDocumentException;
  27. import org.apache.poi.extractor.POIOLE2TextExtractor;
  28. import org.apache.poi.extractor.POITextExtractor;
  29. import org.apache.poi.ooxml.extractor.ExtractorFactory;
  30. import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
  31. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  32. import org.apache.poi.util.IOUtils;
  33. import org.apache.xmlbeans.XmlException;
  34. public abstract class AbstractFileHandler implements FileHandler {
  35. public static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<>();
  36. static {
  37. // password protected files without password
  38. // ... currently none ...
  39. // unsupported file-types, no supported OLE2 parts
  40. EXPECTED_EXTRACTOR_FAILURES.add("hmef/quick-winmail.dat");
  41. EXPECTED_EXTRACTOR_FAILURES.add("hmef/winmail-sample1.dat");
  42. EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-simple.dat");
  43. EXPECTED_EXTRACTOR_FAILURES.add("hmef/bug52400-winmail-with-attachments.dat");
  44. EXPECTED_EXTRACTOR_FAILURES.add("hpsf/Test0313rur.adm");
  45. EXPECTED_EXTRACTOR_FAILURES.add("poifs/Notes.ole2");
  46. }
  47. @Override
  48. public void handleExtracting(File file) throws Exception {
  49. boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
  50. try {
  51. ExtractorFactory.setThreadPrefersEventExtractors(true);
  52. handleExtractingInternal(file);
  53. ExtractorFactory.setThreadPrefersEventExtractors(false);
  54. handleExtractingInternal(file);
  55. } finally {
  56. ExtractorFactory.setThreadPrefersEventExtractors(before);
  57. }
  58. /* Did fail for some documents with special XML contents...
  59. try {
  60. OOXMLPrettyPrint.main(new String[] { file.getAbsolutePath(),
  61. "/tmp/pretty-" + file.getName() });
  62. } catch (ZipException e) {
  63. // ignore, not a Zip/OOXML file
  64. }*/
  65. }
  66. private void handleExtractingInternal(File file) throws Exception {
  67. long length = file.length();
  68. long modified = file.lastModified();
  69. POITextExtractor extractor = null;
  70. try {
  71. extractor = ExtractorFactory.createExtractor(file);
  72. assertNotNull("Should get a POITextExtractor but had none for file " + file, extractor);
  73. assertNotNull("Should get some text but had none for file " + file, extractor.getText());
  74. // also try metadata
  75. @SuppressWarnings("resource")
  76. POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor();
  77. assertNotNull(metadataExtractor.getText());
  78. assertFalse("Expected Extraction to fail for file " + file + " and handler " + this + ", but did not fail!",
  79. EXPECTED_EXTRACTOR_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName()));
  80. assertEquals("File should not be modified by extractor", length, file.length());
  81. assertEquals("File should not be modified by extractor", modified, file.lastModified());
  82. handleExtractingAsStream(file);
  83. if (extractor instanceof POIOLE2TextExtractor) {
  84. try (HPSFPropertiesExtractor hpsfExtractor = new HPSFPropertiesExtractor((POIOLE2TextExtractor) extractor)) {
  85. assertNotNull(hpsfExtractor.getDocumentSummaryInformationText());
  86. assertNotNull(hpsfExtractor.getSummaryInformationText());
  87. String text = hpsfExtractor.getText();
  88. //System.out.println(text);
  89. assertNotNull(text);
  90. }
  91. }
  92. } catch (IllegalArgumentException e) {
  93. if(!EXPECTED_EXTRACTOR_FAILURES.contains(file.getParentFile().getName() + "/" + file.getName())) {
  94. throw e;
  95. }
  96. } catch (EncryptedDocumentException e) {
  97. String msg = "org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files";
  98. assumeFalse(msg.equals(e.getMessage()));
  99. throw e;
  100. } catch (IllegalStateException e) {
  101. if (!e.getMessage().contains("POI Scratchpad jar missing") || !Boolean.getBoolean("scratchpad.ignore")) {
  102. throw e;
  103. }
  104. } finally {
  105. IOUtils.closeQuietly(extractor);
  106. }
  107. }
  108. private void handleExtractingAsStream(File file) throws IOException, OpenXML4JException, XmlException {
  109. try (InputStream stream = new FileInputStream(file)) {
  110. try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) {
  111. assertNotNull(streamExtractor);
  112. assertNotNull(streamExtractor.getText());
  113. }
  114. }
  115. }
  116. @Override
  117. public void handleAdditional(File file) throws Exception {
  118. // by default we do nothing here
  119. }
  120. }