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