Example usage for org.apache.poi EncryptedDocumentException EncryptedDocumentException

List of usage examples for org.apache.poi EncryptedDocumentException EncryptedDocumentException

Introduction

In this page you can find the example usage for org.apache.poi EncryptedDocumentException EncryptedDocumentException.

Prototype

public EncryptedDocumentException(Throwable cause) 

Source Link

Usage

From source file:com.kplot.web.data.WorkbookFactory.java

License:Apache License

/**
 * Creates a Workbook from the given NPOIFSFileSystem, which may
 *  be password protected//from  w  w  w.j a va  2 s  .  c  o  m
 *
 *  @param fs The {@link NPOIFSFileSystem} to read the document from
 *  @param password The password that should be used or null if no password is necessary.
 *
 *  @return The created Workbook
 *
 *  @throws IOException if an error occurs while reading the data
 *  @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook}
 */
private static Workbook create(NPOIFSFileSystem fs, String password)
        throws IOException, InvalidFormatException {
    DirectoryNode root = fs.getRoot();

    // 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) {
                stream = d.getDataStream(root);
            }
        } 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");
        }

        OPCPackage pkg = OPCPackage.open(stream);
        return create(pkg);
    }

    // If we get here, it isn't an encrypted XLSX file
    // So, treat it as a regular HSSF XLS one
    if (password != null) {
        Biff8EncryptionKey.setCurrentUserPassword(password);
    }
    try {
        return new HSSFWorkbook(root, true);
    } finally {
        Biff8EncryptionKey.setCurrentUserPassword(null);
    }
}