Search in sources :

Example 1 with SecureContentHandler

use of org.apache.tika.sax.SecureContentHandler in project tika by apache.

the class AutoDetectParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    TemporaryResources tmp = new TemporaryResources();
    try {
        TikaInputStream tis = TikaInputStream.get(stream, tmp);
        // Automatically detect the MIME type of the document
        MediaType type = detector.detect(tis, metadata);
        metadata.set(Metadata.CONTENT_TYPE, type.toString());
        // TIKA-216: Zip bomb prevention
        SecureContentHandler sch = handler != null ? new SecureContentHandler(handler, tis) : null;
        //the caller hasn't specified one.
        if (context.get(EmbeddedDocumentExtractor.class) == null) {
            Parser p = context.get(Parser.class);
            if (p == null) {
                context.set(Parser.class, this);
            }
            context.set(EmbeddedDocumentExtractor.class, new ParsingEmbeddedDocumentExtractor(context));
        }
        try {
            // Parse the document
            super.parse(tis, sch, metadata, context);
        } catch (SAXException e) {
            // Convert zip bomb exceptions to TikaExceptions
            sch.throwIfCauseOf(e);
            throw e;
        }
    } finally {
        tmp.dispose();
    }
}
Also used : ParsingEmbeddedDocumentExtractor(org.apache.tika.extractor.ParsingEmbeddedDocumentExtractor) ParsingEmbeddedDocumentExtractor(org.apache.tika.extractor.ParsingEmbeddedDocumentExtractor) EmbeddedDocumentExtractor(org.apache.tika.extractor.EmbeddedDocumentExtractor) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) MediaType(org.apache.tika.mime.MediaType) SecureContentHandler(org.apache.tika.sax.SecureContentHandler) SAXException(org.xml.sax.SAXException)

Aggregations

EmbeddedDocumentExtractor (org.apache.tika.extractor.EmbeddedDocumentExtractor)1 ParsingEmbeddedDocumentExtractor (org.apache.tika.extractor.ParsingEmbeddedDocumentExtractor)1 TemporaryResources (org.apache.tika.io.TemporaryResources)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 MediaType (org.apache.tika.mime.MediaType)1 SecureContentHandler (org.apache.tika.sax.SecureContentHandler)1 SAXException (org.xml.sax.SAXException)1