diff options
author | Dominik Stadler <centic@apache.org> | 2016-03-12 11:37:32 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2016-03-12 11:37:32 +0000 |
commit | 71f5735238d9af3088682f3eb8321a35b9df2e32 (patch) | |
tree | 5bcb9c298c9dcb97d67b8b79adff99b076e379ee /src/ooxml/java/org/apache/poi/ss | |
parent | a90762bdc54d66f4a5b4f7100db2886b06d36eff (diff) | |
download | poi-71f5735238d9af3088682f3eb8321a35b9df2e32.tar.gz poi-71f5735238d9af3088682f3eb8321a35b9df2e32.zip |
Refactor some common code from the various Document-Factories into a helper class
Fix a potential file-handle-leak for password protected workbooks or slideshows
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734691 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/ss')
-rw-r--r-- | src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java | 41 |
1 files changed, 3 insertions, 38 deletions
diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 7f4d1f7971..621e71d8de 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -17,18 +17,16 @@ package org.apache.poi.ss.usermodel; import java.io.*; -import java.security.GeneralSecurityException; import org.apache.poi.EmptyFileException; import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.POIXMLDocument; +import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.poifs.crypt.Decryptor; -import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; @@ -82,40 +80,7 @@ public class WorkbookFactory { // Encrypted OOXML files go inside OLE2 containers, is this one? if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { - EncryptionInfo info = new EncryptionInfo(fs); - Decryptor d = Decryptor.getInstance(info); - - boolean passwordCorrect = false; - InputStream stream = null; - try { - if (password != null && d.verifyPassword(password)) { - passwordCorrect = true; - } - if (!passwordCorrect && d.verifyPassword(Decryptor.DEFAULT_PASSWORD)) { - passwordCorrect = true; - } - if (passwordCorrect) { - // wrap the stream in a FilterInputStream to close the NPOIFSFileSystem - // as well when the resulting OPCPackage is closed - stream = new FilterInputStream(d.getDataStream(root)) { - @Override - public void close() throws IOException { - fs.close(); - - super.close(); - } - }; - } - } catch (GeneralSecurityException e) { - throw new IOException(e); - } - - if (! passwordCorrect) { - if (password != null) - throw new EncryptedDocumentException("Password incorrect"); - else - throw new EncryptedDocumentException("The supplied spreadsheet is protected, but no password was supplied"); - } + InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, password); OPCPackage pkg = OPCPackage.open(stream); return create(pkg); @@ -212,7 +177,7 @@ public class WorkbookFactory { NPOIFSFileSystem fs = new NPOIFSFileSystem(inp); return create(fs, password); } - if (POIXMLDocument.hasOOXMLHeader(inp)) { + if (DocumentFactoryHelper.hasOOXMLHeader(inp)) { return new XSSFWorkbook(OPCPackage.open(inp)); } throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); |