Search in sources :

Example 11 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class FtpRequestHandlerTest method testGetMimeTypeOther.

@Test
public void testGetMimeTypeOther() throws MimeTypeResolutionException {
    String mimeType;
    when(mimeTypeMapper.getMimeTypeForFileExtension("txt")).thenReturn("text/plain");
    mimeType = ftplet.getMimeType("txt", new TemporaryFileBackedOutputStream());
    assertEquals("text/plain", mimeType);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 12 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class FtpRequestHandlerTest method testMimeTypeResolutionFailure.

@Test
public void testMimeTypeResolutionFailure() throws MimeTypeResolutionException {
    String mimeType;
    when(mimeTypeMapper.guessMimeType(any(InputStream.class), eq("xml"))).thenThrow(new MimeTypeResolutionException());
    mimeType = ftplet.getMimeType("xml", new TemporaryFileBackedOutputStream());
    assertEquals("", mimeType);
}
Also used : MimeTypeResolutionException(ddf.mime.MimeTypeResolutionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 13 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class PptxInputTransformer method transformLogic.

/**
     * This is a three step process. First, create a FileBackedOutputStream because we need to
     * consume the stream twice. Once for the injected inputTransformer and once for Apache POI.
     * Next, extract the metadata with the injected input transformer. And last, use Apache POI
     * to create the thumbnail.
     *
     * @param input
     * @param id
     * @return
     * @throws IOException
     * @throws CatalogTransformerException
     */
private Metacard transformLogic(InputStream input, String id) throws IOException, CatalogTransformerException {
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            int c = IOUtils.copy(input, fileBackedOutputStream);
            LOGGER.debug("copied {} bytes from input stream to file backed output stream", c);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }
        Metacard metacard = extractInitialMetadata(fileBackedOutputStream.asByteSource().openStream());
        extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());
        if (!usePptTitleAsTitle) {
            // Allow the metacard title to be set as the filename
            metacard.setAttribute(new AttributeImpl(Core.TITLE, (Serializable) null));
        }
        return metacard;
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException)

Example 14 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class PdfInputTransformer method transformWithExtractors.

private Metacard transformWithExtractors(InputStream input, String id) throws IOException, CatalogTransformerException {
    try (TemporaryFileBackedOutputStream fbos = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(input, fbos);
        } catch (IOException e) {
            throw new CatalogTransformerException("Could not copy bytes of content message.", e);
        }
        String plainText = null;
        try (InputStream isCopy = fbos.asByteSource().openStream()) {
            Parser parser = new AutoDetectParser();
            ContentHandler contentHandler = new ToTextContentHandler();
            TikaMetadataExtractor tikaMetadataExtractor = new TikaMetadataExtractor(parser, contentHandler);
            tikaMetadataExtractor.parseMetadata(isCopy, new ParseContext());
            plainText = contentHandler.toString();
        } catch (CatalogTransformerException e) {
            LOGGER.warn("Cannot extract metadata from pdf", e);
        }
        try (InputStream isCopy = fbos.asByteSource().openStream();
            PDDocument pdfDocument = pdDocumentGenerator.apply(isCopy)) {
            return transformPdf(id, pdfDocument, plainText);
        } catch (InvalidPasswordException e) {
            LOGGER.debug("Cannot transform encrypted pdf", e);
            return initializeMetacard(id);
        }
    }
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) ContentHandler(org.xml.sax.ContentHandler) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ToTextContentHandler(org.apache.tika.sax.ToTextContentHandler) TikaMetadataExtractor(ddf.catalog.transformer.common.tika.TikaMetadataExtractor) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) ParseContext(org.apache.tika.parser.ParseContext) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) InvalidPasswordException(org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException)

Example 15 with TemporaryFileBackedOutputStream

use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.

the class GeoNamesFileExtractor method unZipInputStream.

/**
     * Unzips a file and returns the output as a new InputStream
     *
     * @param resource    - the name of the resource file to be unzipped
     * @param inputStream - the InputStream for the file to be unzipped
     * @return - the unzipped file as an InputStream
     * @throws GeoEntryExtractionException when the given file fails to be unzipped.
     */
private InputStream unZipInputStream(String resource, InputStream inputStream) throws GeoEntryExtractionException {
    try (TemporaryFileBackedOutputStream bufferedOutputStream = new TemporaryFileBackedOutputStream(BUFFER_SIZE);
        ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
        ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            // GeoNames <filename>.zip files will contain <filename>.txt and readme.txt
            if (!zipEntry.getName().equals("readme.txt")) {
                byte[] data = new byte[BUFFER_SIZE];
                int bytesRead;
                while ((bytesRead = zipInputStream.read(data, 0, BUFFER_SIZE)) != -1) {
                    bufferedOutputStream.write(data, 0, bytesRead);
                }
                ByteSource zipByteSource = bufferedOutputStream.asByteSource();
                bufferedOutputStream.flush();
                fileSize = zipByteSource.size();
                return zipByteSource.openBufferedStream();
            }
        }
    } catch (IOException e) {
        throw new GeoEntryExtractionException("Unable to unzip " + resource, e);
    }
    throw new GeoEntryExtractionException("Unable to unzip " + resource);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException)

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)18 IOException (java.io.IOException)13 InputStream (java.io.InputStream)10 Metacard (ddf.catalog.data.Metacard)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)8 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 ByteSource (com.google.common.io.ByteSource)3 InputTransformer (ddf.catalog.transform.InputTransformer)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 ArrayList (java.util.ArrayList)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 MetacardCreationException (ddf.catalog.data.MetacardCreationException)2 TikaMetadataExtractor (ddf.catalog.transformer.common.tika.TikaMetadataExtractor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Serializable (java.io.Serializable)2 DefaultFtpReply (org.apache.ftpserver.ftplet.DefaultFtpReply)2 FtpFile (org.apache.ftpserver.ftplet.FtpFile)2