Search in sources :

Example 1 with LookaheadInputStream

use of org.apache.tika.io.LookaheadInputStream in project tika by apache.

the class EncryptedPrescriptionDetector method detect.

public MediaType detect(InputStream stream, Metadata metadata) throws IOException {
    Key key = Pharmacy.getKey();
    MediaType type = MediaType.OCTET_STREAM;
    try (InputStream lookahead = new LookaheadInputStream(stream, 1024)) {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        InputStream decrypted = new CipherInputStream(lookahead, cipher);
        QName name = new XmlRootExtractor().extractRootElement(decrypted);
        if (name != null && "http://example.com/xpd".equals(name.getNamespaceURI()) && "prescription".equals(name.getLocalPart())) {
            type = MediaType.application("x-prescription");
        }
    } catch (GeneralSecurityException e) {
    // unable to decrypt, fall through
    }
    return type;
}
Also used : LookaheadInputStream(org.apache.tika.io.LookaheadInputStream) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) LookaheadInputStream(org.apache.tika.io.LookaheadInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) GeneralSecurityException(java.security.GeneralSecurityException) MediaType(org.apache.tika.mime.MediaType) XmlRootExtractor(org.apache.tika.detect.XmlRootExtractor) Cipher(javax.crypto.Cipher) Key(java.security.Key)

Aggregations

InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Key (java.security.Key)1 Cipher (javax.crypto.Cipher)1 CipherInputStream (javax.crypto.CipherInputStream)1 QName (javax.xml.namespace.QName)1 XmlRootExtractor (org.apache.tika.detect.XmlRootExtractor)1 LookaheadInputStream (org.apache.tika.io.LookaheadInputStream)1 MediaType (org.apache.tika.mime.MediaType)1