MSEmbeddedStreamTranslator.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.extractor.microsoft;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.Ole10Native;
import org.apache.poi.poifs.filesystem.Ole10NativeException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.tika.extractor.EmbeddedStreamTranslator;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;
import org.apache.tika.parser.microsoft.OfficeParser;

public class MSEmbeddedStreamTranslator implements EmbeddedStreamTranslator {

    private static final Logger LOG = LoggerFactory.getLogger(MSEmbeddedStreamTranslator.class);

    @Override
    public boolean shouldTranslate(TikaInputStream tis, Metadata metadata) throws IOException {
        String contentType = metadata.get(org.apache.tika.metadata.HttpHeaders.CONTENT_TYPE);
        if ("application/vnd.openxmlformats-officedocument.oleObject".equals(contentType)) {
            return true;
        } else {
            return tis.getOpenContainer() != null &&
                    tis.getOpenContainer() instanceof DirectoryEntry;
        }
    }

    @Override
    public void translate(TikaInputStream tis, Metadata metadata, OutputStream os) throws IOException {
        String contentType = metadata.get(org.apache.tika.metadata.HttpHeaders.CONTENT_TYPE);
        if ("application/vnd.openxmlformats-officedocument.oleObject".equals(contentType)) {
            UnsynchronizedByteArrayOutputStream bos = UnsynchronizedByteArrayOutputStream.builder().get();
            IOUtils.copy(tis, bos);
            POIFSFileSystem poifs = new POIFSFileSystem(bos.toInputStream());
            OfficeParser.POIFSDocumentType type = OfficeParser.POIFSDocumentType.detectType(poifs);
            String name = metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY);

            byte[] data = bos.toByteArray();
            if (type == OfficeParser.POIFSDocumentType.OLE10_NATIVE) {
                try {
                    Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(poifs);
                    if (ole.getDataSize() > 0) {
                        name = ole.getLabel();
                        data = ole.getDataBuffer();
                    }
                } catch (Ole10NativeException ex) {
                    LOG.warn("Skipping invalid part", ex);
                }
            } else {
                name += '.' + type.getExtension();
                metadata.set(TikaCoreProperties.RESOURCE_NAME_EXTENSION_INFERRED, true);
            }
            os.write(data);
            os.flush();
        } else {
            if (tis.getOpenContainer() != null &&
                    tis.getOpenContainer() instanceof DirectoryEntry) {
                try (POIFSFileSystem fs = new POIFSFileSystem()) {
                    copy((DirectoryEntry) tis.getOpenContainer(), fs.getRoot());
                    fs.writeFilesystem(CloseShieldOutputStream.wrap(os));
                }
            }
        }
    }

    protected void copy(DirectoryEntry sourceDir, DirectoryEntry destDir) throws IOException {
        for (Entry entry : sourceDir) {
            if (entry instanceof DirectoryEntry) {
                // Need to recurse
                DirectoryEntry newDir = destDir.createDirectory(entry.getName());
                copy((DirectoryEntry) entry, newDir);
            } else {
                // Copy entry
                try (InputStream contents = new DocumentInputStream((DocumentEntry) entry)) {
                    destDir.createDocument(entry.getName(), contents);
                }
            }
        }
    }
}