Search in sources :

Example 21 with TemporaryFileBackedOutputStream

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

the class InputTransformerProducer method generateMetacard.

private Metacard generateMetacard(MimeType mimeType, MimeTypeToTransformerMapper mapper, InputStream message) throws MetacardCreationException {
    LOGGER.trace("ENTERING: generateMetacard");
    List<InputTransformer> listOfCandidates = mapper.findMatches(InputTransformer.class, mimeType);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("List of matches for mimeType [{}]: {}", mimeType, listOfCandidates);
    }
    Metacard generatedMetacard = null;
    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        try {
            IOUtils.copy(message, fileBackedOutputStream);
        } catch (IOException e) {
            throw new MetacardCreationException("Could not copy bytes of content message.", e);
        }
        // can create the metacard, then do not need to try any remaining InputTransformers.
        for (InputTransformer transformer : listOfCandidates) {
            try (InputStream inputStreamMessageCopy = fileBackedOutputStream.asByteSource().openStream()) {
                generatedMetacard = transformer.transform(inputStreamMessageCopy);
            } catch (IOException | CatalogTransformerException e) {
                LOGGER.debug("Transformer [" + transformer + "] could not create metacard.", e);
            }
            if (generatedMetacard != null) {
                break;
            }
        }
        if (generatedMetacard == null) {
            throw new MetacardCreationException("Could not create metacard with mimeType " + mimeType + ". No valid transformers found.");
        }
        LOGGER.trace("EXITING: generateMetacard");
    } catch (IOException e) {
        throw new MetacardCreationException("Could not create metacard.", e);
    }
    return generatedMetacard;
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) IOException(java.io.IOException) InputTransformer(ddf.catalog.transform.InputTransformer)

Example 22 with TemporaryFileBackedOutputStream

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

the class CatalogOutputAdapterTest method testGetNitfSegmentsFlowTFBOSThrows.

/**
 * Test that if the TFBOS throws an exception, the TFBOS is closed
 */
@Test
public void testGetNitfSegmentsFlowTFBOSThrows() throws IOException, NitfFormatException {
    TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
    doThrow(IOException.class).when(tfbos).write(anyObject(), anyInt(), anyInt());
    catalogOutputAdapter = new CatalogOutputAdapter() {

        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
            return tfbos;
        }
    };
    try {
        catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] { (byte) 0 }));
        fail("expected an exception, shouldn't reach this line");
    } catch (IOException e) {
        assertThat(e, notNullValue());
    }
    verify(tfbos).close();
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 23 with TemporaryFileBackedOutputStream

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

the class CatalogOutputAdapterTest method testGetNitfSegmentsFlowTFBOSThrowsDuringRead.

@SuppressWarnings("unchecked")
@Test
public void testGetNitfSegmentsFlowTFBOSThrowsDuringRead() throws IOException, NitfFormatException {
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read()).thenThrow(IOException.class);
    when(inputStream.read(anyObject())).thenThrow(IOException.class);
    when(inputStream.read(anyObject(), anyInt(), anyInt())).thenThrow(IOException.class);
    ByteSource byteSource = mock(ByteSource.class);
    when(byteSource.openBufferedStream()).thenReturn(inputStream);
    TemporaryFileBackedOutputStream tfbos = mock(TemporaryFileBackedOutputStream.class);
    when(tfbos.asByteSource()).thenReturn(byteSource);
    catalogOutputAdapter = new CatalogOutputAdapter() {

        @Override
        protected TemporaryFileBackedOutputStream createTemporaryFileBackedOutputStream() {
            return tfbos;
        }
    };
    try {
        catalogOutputAdapter.getNitfSegmentsFlow(new ByteArrayInputStream(new byte[] { (byte) 0 }));
        fail("expected an exception, shouldn't reach this line");
    } catch (IOException | NitfFormatException e) {
        assertThat(e, notNullValue());
    }
    verify(tfbos).close();
    verify(inputStream).close();
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NitfFormatException(org.codice.imaging.nitf.core.common.NitfFormatException) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with TemporaryFileBackedOutputStream

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

the class OrderRequestImpl method writeSingleFile.

private void writeSingleFile(DestinationSink destinationSink, PackagingSpecFormatType packagingSpecFormatType, List<ResourceContainer> files, String filename, List<String> sentFiles) throws IOException {
    ResourceContainer file = files.iterator().next();
    List<Metacard> metacards = Collections.singletonList(file.getMetacard());
    switch(packagingSpecFormatType) {
        case FILESUNC:
            {
                try (InputStream fileInputStream = file.getInputStream()) {
                    destinationSink.writeFile(fileInputStream, file.getSize(), filename, file.getMimeTypeValue(), metacards);
                    sentFiles.add(filename);
                }
            }
            break;
        case FILESCOMPRESS:
            {
                try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    ZipOutputStream zipOut = new ZipOutputStream(fos);
                    InputStream fileInputStream = file.getInputStream()) {
                    getZip(zipOut, fileInputStream, file.getName());
                    ByteSource contents = fos.asByteSource();
                    writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
                }
            }
            break;
        case TARUNC:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                TarOutputStream tarOut = new TarOutputStream(fos)) {
                getTar(tarOut, file);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARZIP:
            {
                writeTarFile(destinationSink, packagingSpecFormatType, filename, sentFiles, file, metacards);
            }
            break;
        case FILESZIP:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                InputStream fileInputStream = file.getInputStream()) {
                getGzip(zipOut, fileInputStream);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARGZIP:
            {
                try (TemporaryFileBackedOutputStream tarFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                    TarOutputStream tarOut = new TarOutputStream(tarFos)) {
                    getTar(tarOut, file);
                    try (TemporaryFileBackedOutputStream gzipFos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                        GZIPOutputStream zipOut = new GZIPOutputStream(gzipFos)) {
                        getGzip(zipOut, tarFos.asByteSource().openStream());
                        ByteSource contents = gzipFos.asByteSource();
                        writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
                    }
                }
            }
            break;
        case FILESGZIP:
            try (TemporaryFileBackedOutputStream fos = new TemporaryFileBackedOutputStream(MAX_MEMORY_SIZE);
                GZIPOutputStream zipOut = new GZIPOutputStream(fos);
                InputStream fileInputStream = file.getInputStream()) {
                getGzip(zipOut, fileInputStream);
                ByteSource contents = fos.asByteSource();
                writeFile(destinationSink, packagingSpecFormatType, filename, sentFiles, contents, metacards);
            }
            break;
        case TARCOMPRESS:
            {
                writeTarFile(destinationSink, packagingSpecFormatType, filename, sentFiles, file, metacards);
            }
            break;
        default:
            LOGGER.debug("Unknown packaging format type, skipping");
            break;
    }
}
Also used : TarOutputStream(org.kamranzafar.jtar.TarOutputStream) Metacard(ddf.catalog.data.Metacard) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteSource(com.google.common.io.ByteSource)

Example 25 with TemporaryFileBackedOutputStream

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

the class ImagingTest method testImageNitfChipCreationNitf.

@Test
public void testImageNitfChipCreationNitf() throws Exception {
    String id = ingestNitfFile(TEST_IMAGE_NITF);
    String chippingUrl = SECURE_ROOT + HTTPS_PORT.getPort() + "/chipping/chipping.html?id=" + id + "&source=Alliance";
    given().get(chippingUrl).then().assertThat().statusCode(HttpStatus.SC_OK);
    final int width = 350;
    final int height = 240;
    String chippedImageUrl = SERVICE_ROOT + "/catalog/" + id + "?transform=nitf-chip&qualifier=overview&x=" + 300 + "&y=" + 200 + "&w=" + width + "&h=" + height;
    InputStream chippedImageStream = given().get(chippedImageUrl).asInputStream();
    List<ImageSegment> imageSegments = new LinkedList<>();
    try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
        IOUtils.copyLarge(chippedImageStream, tfbos);
        NitfSegmentsFlow nitfSegmentsFlow = new NitfParserInputFlowImpl().inputStream(tfbos.asByteSource().openBufferedStream()).allData();
        nitfSegmentsFlow.forEachImageSegment(imageSegments::add);
    }
    assertThat(imageSegments, hasSize(1));
    assertThat(imageSegments.get(0).getNumberOfColumns(), is((long) width));
    assertThat(imageSegments.get(0).getNumberOfRows(), is((long) height));
}
Also used : ImageSegment(org.codice.imaging.nitf.core.image.ImageSegment) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) NitfSegmentsFlow(org.codice.imaging.nitf.fluent.NitfSegmentsFlow) NitfParserInputFlowImpl(org.codice.imaging.nitf.fluent.impl.NitfParserInputFlowImpl) LinkedList(java.util.LinkedList) AbstractAllianceIntegrationTest(org.codice.alliance.test.itests.common.AbstractAllianceIntegrationTest) Test(org.junit.Test)

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)34 IOException (java.io.IOException)23 InputStream (java.io.InputStream)21 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)14 Metacard (ddf.catalog.data.Metacard)13 ByteSource (com.google.common.io.ByteSource)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Test (org.junit.Test)7 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)5 InputTransformer (ddf.catalog.transform.InputTransformer)5 ArrayList (java.util.ArrayList)5 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MetacardCreationException (ddf.catalog.data.MetacardCreationException)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 Result (ddf.catalog.data.Result)2 ResultImpl (ddf.catalog.data.impl.ResultImpl)2