Example usage for org.apache.poi.poifs.crypt Decryptor DEFAULT_PASSWORD

List of usage examples for org.apache.poi.poifs.crypt Decryptor DEFAULT_PASSWORD

Introduction

In this page you can find the example usage for org.apache.poi.poifs.crypt Decryptor DEFAULT_PASSWORD.

Prototype

String DEFAULT_PASSWORD

To view the source code for org.apache.poi.poifs.crypt Decryptor DEFAULT_PASSWORD.

Click 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   ww  w  . j a v a  2  s.com*/
 *
 *  @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);
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.OfficeParser.java

License:Apache License

protected void parse(DirectoryNode root, ParseContext context, Metadata metadata, XHTMLContentHandler xhtml)
        throws IOException, SAXException, TikaException {

    // Parse summary entries first, to make metadata available early
    new SummaryExtractor(metadata).parseSummaries(root);

    // Parse remaining document entries
    POIFSDocumentType type = POIFSDocumentType.detectType(root);

    if (type != POIFSDocumentType.UNKNOWN) {
        setType(metadata, type.getType());
    }// w  w w. j  ava2  s .  c o m

    switch (type) {
    case SOLIDWORKS_PART:
        // new SolidworksExtractor(context).parse(root, xhtml);
        break;
    case SOLIDWORKS_ASSEMBLY:
        break;
    case SOLIDWORKS_DRAWING:
        break;
    case PUBLISHER:
        PublisherTextExtractor publisherTextExtractor = new PublisherTextExtractor(root);
        xhtml.element("p", publisherTextExtractor.getText());
        break;
    case WORDDOCUMENT:
        new WordExtractor(context, metadata).parse(root, xhtml);
        break;
    case POWERPOINT:
        new HSLFExtractor(context, metadata).parse(root, xhtml);
        break;
    case WORKBOOK:
    case XLR:
        Locale locale = context.get(Locale.class, Locale.getDefault());
        new ExcelExtractor(context, metadata).parse(root, xhtml, locale);
        break;
    case PROJECT:
        // We currently can't do anything beyond the metadata
        break;
    case VISIO:
        VisioTextExtractor visioTextExtractor = new VisioTextExtractor(root);
        for (String text : visioTextExtractor.getAllText()) {
            xhtml.element("p", text);
        }
        break;
    case OUTLOOK:
        OutlookExtractor extractor = new OutlookExtractor(root, context);
        extractor.parse(xhtml, metadata);
        break;
    case ENCRYPTED:
        EncryptionInfo info = new EncryptionInfo(root);
        Decryptor d = Decryptor.getInstance(info);

        try {
            // By default, use the default Office Password
            String password = Decryptor.DEFAULT_PASSWORD;

            // If they supplied a Password Provider, ask that for the password,
            // and use the provider given one if available (stick with default if
            // not)
            PasswordProvider passwordProvider = context.get(PasswordProvider.class);
            if (passwordProvider != null) {
                String suppliedPassword = passwordProvider.getPassword(metadata);
                if (suppliedPassword != null) {
                    password = suppliedPassword;
                }
            }

            // Check if we've the right password or not
            if (!d.verifyPassword(password)) {
                throw new EncryptedDocumentException();
            }

            // Decrypt the OLE2 stream, and delegate the resulting OOXML
            // file to the regular OOXML parser for normal handling
            OOXMLParser parser = new OOXMLParser();

            parser.parse(d.getDataStream(root), new EmbeddedContentHandler(new BodyContentHandler(xhtml)),
                    metadata, context);
        } catch (GeneralSecurityException ex) {
            throw new EncryptedDocumentException(ex);
        }
    }
}

From source file:org.apache.tika.parser.microsoft.OfficeParser.java

License:Apache License

protected void parse(DirectoryNode root, ParseContext context, Metadata metadata, XHTMLContentHandler xhtml)
        throws IOException, SAXException, TikaException {

    // Parse summary entries first, to make metadata available early
    new SummaryExtractor(metadata).parseSummaries(root);

    // Parse remaining document entries
    POIFSDocumentType type = POIFSDocumentType.detectType(root);

    if (type != POIFSDocumentType.UNKNOWN) {
        setType(metadata, type.getType());
    }/*from   ww  w .  j a va  2  s .com*/

    switch (type) {
    case SOLIDWORKS_PART:
    case SOLIDWORKS_ASSEMBLY:
    case SOLIDWORKS_DRAWING:
        break;
    case PUBLISHER:
        PublisherTextExtractor publisherTextExtractor = new PublisherTextExtractor(root);
        xhtml.element("p", publisherTextExtractor.getText());
        break;
    case WORDDOCUMENT:
        new WordExtractor(context).parse(root, xhtml);
        break;
    case POWERPOINT:
        new HSLFExtractor(context).parse(root, xhtml);
        break;
    case WORKBOOK:
    case XLR:
        Locale locale = context.get(Locale.class, Locale.getDefault());
        new ExcelExtractor(context, metadata).parse(root, xhtml, locale);
        break;
    case PROJECT:
        // We currently can't do anything beyond the metadata
        break;
    case VISIO:
        VisioTextExtractor visioTextExtractor = new VisioTextExtractor(root);
        for (String text : visioTextExtractor.getAllText()) {
            xhtml.element("p", text);
        }
        break;
    case OUTLOOK:
        OutlookExtractor extractor = new OutlookExtractor(root, context);

        extractor.parse(xhtml, metadata);
        break;
    case ENCRYPTED:
        EncryptionInfo info = new EncryptionInfo(root);
        Decryptor d = Decryptor.getInstance(info);

        try {
            // By default, use the default Office Password
            String password = Decryptor.DEFAULT_PASSWORD;

            // If they supplied a Password Provider, ask that for the password,
            //  and use the provider given one if available (stick with default if not)
            PasswordProvider passwordProvider = context.get(PasswordProvider.class);
            if (passwordProvider != null) {
                String suppliedPassword = passwordProvider.getPassword(metadata);
                if (suppliedPassword != null) {
                    password = suppliedPassword;
                }
            }

            // Check if we've the right password or not
            if (!d.verifyPassword(password)) {
                throw new EncryptedDocumentException();
            }

            // Decrypt the OLE2 stream, and delegate the resulting OOXML
            //  file to the regular OOXML parser for normal handling
            OOXMLParser parser = new OOXMLParser();

            parser.parse(d.getDataStream(root), new EmbeddedContentHandler(new BodyContentHandler(xhtml)),
                    metadata, context);
        } catch (GeneralSecurityException ex) {
            throw new EncryptedDocumentException(ex);
        }
    default:
        // For unsupported / unhandled types, just the metadata
        //  is extracted, which happened above
        break;
    }
}