Search in sources :

Example 1 with COSNull

use of com.tom_roush.pdfbox.cos.COSNull in project PdfBox-Android by TomRoush.

the class PDStream method getMetadata.

/**
 * Get the metadata that is part of the document catalog. This will return
 * null if there is no meta data for this object.
 *
 * @return The metadata for this object.
 * @throws IllegalStateException if the value of the metadata entry is different from a stream
 *                               or null
 */
public PDMetadata getMetadata() {
    PDMetadata retval = null;
    COSBase mdStream = stream.getDictionaryObject(COSName.METADATA);
    if (mdStream != null) {
        if (mdStream instanceof COSStream) {
            retval = new PDMetadata((COSStream) mdStream);
        } else if (mdStream instanceof COSNull) {
        // null is authorized
        } else {
            throw new IllegalStateException("Expected a COSStream but was a " + mdStream.getClass().getSimpleName());
        }
    }
    return retval;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSNull(com.tom_roush.pdfbox.cos.COSNull)

Example 2 with COSNull

use of com.tom_roush.pdfbox.cos.COSNull in project PdfBox-Android by TomRoush.

the class COSParser method prepareDecryption.

/**
 * Prepare for decryption.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException if something went wrong
 */
private void prepareDecryption() throws IOException {
    if (encryption != null) {
        return;
    }
    COSBase trailerEncryptItem = document.getTrailer().getItem(COSName.ENCRYPT);
    if (trailerEncryptItem == null || trailerEncryptItem instanceof COSNull) {
        return;
    }
    if (trailerEncryptItem instanceof COSObject) {
        COSObject trailerEncryptObj = (COSObject) trailerEncryptItem;
        parseDictionaryRecursive(trailerEncryptObj);
    }
    try {
        encryption = new PDEncryption(document.getEncryptionDictionary());
        DecryptionMaterial decryptionMaterial;
        if (keyStoreInputStream != null) {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            ks.load(keyStoreInputStream, password.toCharArray());
            decryptionMaterial = new PublicKeyDecryptionMaterial(ks, keyAlias, password);
        } else {
            decryptionMaterial = new StandardDecryptionMaterial(password);
        }
        securityHandler = encryption.getSecurityHandler();
        securityHandler.prepareForDecryption(encryption, document.getDocumentID(), decryptionMaterial);
        accessPermission = securityHandler.getCurrentAccessPermission();
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Error (" + e.getClass().getSimpleName() + ") while creating security handler for decryption", e);
    } finally {
        if (keyStoreInputStream != null) {
            IOUtils.closeQuietly(keyStoreInputStream);
        }
    }
}
Also used : DecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSNull(com.tom_roush.pdfbox.cos.COSNull) PublicKeyDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial) StandardDecryptionMaterial(com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial) PDEncryption(com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption) IOException(java.io.IOException) KeyStore(java.security.KeyStore) InvalidPasswordException(com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException) IOException(java.io.IOException)

Aggregations

COSBase (com.tom_roush.pdfbox.cos.COSBase)2 COSNull (com.tom_roush.pdfbox.cos.COSNull)2 COSObject (com.tom_roush.pdfbox.cos.COSObject)1 COSStream (com.tom_roush.pdfbox.cos.COSStream)1 DecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.DecryptionMaterial)1 InvalidPasswordException (com.tom_roush.pdfbox.pdmodel.encryption.InvalidPasswordException)1 PDEncryption (com.tom_roush.pdfbox.pdmodel.encryption.PDEncryption)1 PublicKeyDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyDecryptionMaterial)1 StandardDecryptionMaterial (com.tom_roush.pdfbox.pdmodel.encryption.StandardDecryptionMaterial)1 IOException (java.io.IOException)1 KeyStore (java.security.KeyStore)1