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 FtpRequestHandlerTest method testGetMimeTypeXml.
@Test
public void testGetMimeTypeXml() throws MimeTypeResolutionException {
String mimeType;
when(mimeTypeMapper.guessMimeType(any(InputStream.class), eq("xml"))).thenReturn("text/xml");
mimeType = ftplet.getMimeType("xml", new TemporaryFileBackedOutputStream());
assertEquals("text/xml", mimeType);
}
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;
}
Aggregations