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;
}
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();
}
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();
}
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;
}
}
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));
}
Aggregations