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);
}
use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.
the class GeoNamesFileExtractor method getInputStreamFromUrl.
/**
* Download a GeoNames .zip file from a remote location
*
* @param resource - the name of the zip file to download ( ex. AD )
* @param response - the response from the get request
* @param inputStream - the InputStream from the web connection
* @param progressCallback - the callback to receive updates about the progress, may be null if
* you don't want any updates
* @throws GeoNamesRemoteDownloadException when the connection could not be established or the
* file could not be downloaded.
*/
private InputStream getInputStreamFromUrl(String resource, Response response, InputStream inputStream, ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException {
int responseCode = 0;
try (TemporaryFileBackedOutputStream fileOutputStream = new TemporaryFileBackedOutputStream(BUFFER_SIZE)) {
responseCode = response.getStatus();
int totalFileSize = response.getLength();
if (inputStream == null) {
throw new GeoNamesRemoteDownloadException("Unable to get input stream from " + url + ". Server responded with : " + responseCode);
}
double totalBytesRead = 0.0;
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
if (progressCallback != null) {
progressCallback.updateProgress((int) ((totalBytesRead / totalFileSize) * 50));
}
}
if (progressCallback != null) {
progressCallback.updateProgress(50);
}
ByteSource byteSource = fileOutputStream.asByteSource();
fileOutputStream.flush();
inputStream.close();
closeConnection();
return byteSource.openBufferedStream();
} catch (IOException e) {
throw new GeoNamesRemoteDownloadException("Unable to download " + resource + " from " + url + ". Server responded with : " + responseCode, e);
}
}
use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.
the class MimeTypeMapperImpl method guessMimeType.
@Override
public String guessMimeType(InputStream is, String fileExtension) throws MimeTypeResolutionException {
LOGGER.trace(ENTERING_STR, "guessMimeType()");
String mimeType = null;
LOGGER.debug("Looping through{} MimeTypeResolvers", mimeTypeResolvers.size());
// This is to force the TikaMimeTypeResolver to be called
// after the CustomMimeTypeResolvers to prevent Tika default mapping
// from being used when a CustomMimeTypeResolver may be more appropriate.
List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
if (StringUtils.isEmpty(fileExtension)) {
try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
IOUtils.copy(is, tfbos);
try (InputStream inputStream = tfbos.asByteSource().openStream()) {
Detector detector = new DefaultDetector();
MediaType mediaType = detector.detect(inputStream, new Metadata());
fileExtension = getFileExtensionForMimeType(mediaType.toString()).replace(".", "");
} finally {
is = tfbos.asByteSource().openStream();
}
} catch (Exception e) {
LOGGER.debug("Failed to guess mimeType for file without extension.");
}
}
// If file has XML extension, then read root element namespace once so
// each MimeTypeResolver does not have to open the stream and read the namespace
String namespace = null;
if (fileExtension.equals(XML_FILE_EXTENSION)) {
try {
namespace = XML_UTILS.getRootNamespace(IOUtils.toString(is));
} catch (IOException ioe) {
LOGGER.debug("Could not read namespace from input stream.", ioe);
}
LOGGER.debug("namespace = {}", namespace);
}
// Once a file extension is find for the given mime type, exit the loop.
for (MimeTypeResolver resolver : sortedResolvers) {
LOGGER.debug(CALLING_RESOLVER_MSG, resolver.getName());
try {
// an InputTransformer to create a metacard for that "generic" XML file.
if (fileExtension.equals(XML_FILE_EXTENSION)) {
if (namespace != null && resolver.hasSchema()) {
if (namespace.equals(resolver.getSchema())) {
mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
}
}
} else {
mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
}
} catch (Exception e) {
LOGGER.debug("Error resolving mime type for file extension: {}", fileExtension);
throw new MimeTypeResolutionException(e);
}
if (StringUtils.isNotEmpty(mimeType)) {
LOGGER.debug("mimeType [{}] retrieved from MimeTypeResolver: ", mimeType, resolver.getName());
break;
}
}
LOGGER.debug(MIME_TYPE_FILE_EXT_MSG, mimeType, fileExtension);
LOGGER.trace(EXITING_STR, "guessMimeType()");
return mimeType;
}
use of org.codice.ddf.platform.util.TemporaryFileBackedOutputStream in project ddf by codice.
the class KmlInputTransformer method transform.
@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
MetacardImpl metacard;
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
IOUtils.copy(inputStream, fileBackedOutputStream);
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform KML to Metacard. Error reading input stream.", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
metacard = (MetacardImpl) unmarshal(inputStreamCopy);
}
if (metacard == null) {
throw new CatalogTransformerException("Unable to transform Kml to Metacard.");
} else if (StringUtils.isNotEmpty(id)) {
metacard.setAttribute(Metacard.ID, id);
}
try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
String kmlString = CharStreams.toString(reader);
metacard.setAttribute(Metacard.METADATA, kmlString);
}
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform KML to Metacard. Error using file-backed stream.", e);
}
return metacard;
}
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
* @return
* @throws IOException
* @throws CatalogTransformerException
*/
private Metacard transformLogic(InputStream input) 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());
try {
extractThumbnail(metacard, fileBackedOutputStream.asByteSource().openStream());
} catch (EncryptedDocumentException e) {
LOGGER.debug("Unable to generate thumbnail", e);
}
return metacard;
}
}
Aggregations