aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2017-06-06 22:21:46 +0000
committerAndreas Beeker <kiwiwings@apache.org>2017-06-06 22:21:46 +0000
commite9684d38c02c122ecbdb253f2212def60973edd3 (patch)
tree670829746a68970fa54142f19515e10618e92f3d /src/java/org/apache/poi
parent90263ad93f22c8ee1ec9e49356f2c1f7464a6240 (diff)
downloadpoi-e9684d38c02c122ecbdb253f2212def60973edd3.tar.gz
poi-e9684d38c02c122ecbdb253f2212def60973edd3.zip
Handle encrypted files with known password in integration tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1797839 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
-rw-r--r--src/java/org/apache/poi/extractor/OLE2ExtractorFactory.java34
-rw-r--r--src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java6
2 files changed, 36 insertions, 4 deletions
diff --git a/src/java/org/apache/poi/extractor/OLE2ExtractorFactory.java b/src/java/org/apache/poi/extractor/OLE2ExtractorFactory.java
index 737e9e3510..6c96181e5f 100644
--- a/src/java/org/apache/poi/extractor/OLE2ExtractorFactory.java
+++ b/src/java/org/apache/poi/extractor/OLE2ExtractorFactory.java
@@ -26,17 +26,22 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.POITextExtractor;
import org.apache.poi.hssf.OldExcelFormatException;
import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
+import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
+import org.apache.poi.poifs.crypt.Decryptor;
+import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -167,9 +172,7 @@ public class OLE2ExtractorFactory {
* Note that this won't check for embedded OOXML resources either, use
* {@link org.apache.poi.extractor.ExtractorFactory} for that.
*/
- public static POITextExtractor createExtractor(DirectoryNode poifsDir)
- throws IOException
- {
+ public static POITextExtractor createExtractor(DirectoryNode poifsDir) throws IOException {
// Look for certain entries in the stream, to figure it
// out from
for (String workbookName : WORKBOOK_DIR_ENTRY_NAMES) {
@@ -267,4 +270,29 @@ public class OLE2ExtractorFactory {
}
return e.toArray(new POITextExtractor[e.size()]);
}
+
+ private static POITextExtractor createEncyptedOOXMLExtractor(DirectoryNode poifsDir)
+ throws IOException {
+ String pass = Biff8EncryptionKey.getCurrentUserPassword();
+ if (pass == null) {
+ pass = Decryptor.DEFAULT_PASSWORD;
+ }
+
+ EncryptionInfo ei = new EncryptionInfo(poifsDir);
+ Decryptor dec = ei.getDecryptor();
+ InputStream is = null;
+ try {
+ if (!dec.verifyPassword(pass)) {
+ throw new EncryptedDocumentException("Invalid password specified - use Biff8EncryptionKey.setCurrentUserPassword() before calling extractor");
+ }
+ is = dec.getDataStream(poifsDir);
+ return createExtractor(is);
+ } catch (IOException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new IOException(e);
+ } finally {
+ IOUtils.closeQuietly(is);
+ }
+ }
}
diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
index 92575d1d6b..97d9466173 100644
--- a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
+++ b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
@@ -76,13 +76,17 @@ public class SlideShowFactory {
// If we get here, it isn't an encrypted PPTX file
// So, treat it as a regular HSLF PPT one
+ boolean passwordSet = false;
if (password != null) {
Biff8EncryptionKey.setCurrentUserPassword(password);
+ passwordSet = true;
}
try {
return createHSLFSlideShow(fs);
} finally {
- Biff8EncryptionKey.setCurrentUserPassword(null);
+ if (passwordSet) {
+ Biff8EncryptionKey.setCurrentUserPassword(null);
+ }
}
}