Search in sources :

Example 16 with TemporaryFileBackedOutputStream

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);
    }
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException)

Example 17 with TemporaryFileBackedOutputStream

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);
}
Also used : TemporaryFileBackedOutputStream(org.codice.ddf.platform.util.TemporaryFileBackedOutputStream) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 18 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)

Aggregations

TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)18 IOException (java.io.IOException)13 InputStream (java.io.InputStream)10 Metacard (ddf.catalog.data.Metacard)8 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)8 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.junit.Test)4 ByteSource (com.google.common.io.ByteSource)3 InputTransformer (ddf.catalog.transform.InputTransformer)3 MimeTypeResolutionException (ddf.mime.MimeTypeResolutionException)3 Subject (ddf.security.Subject)3 ArrayList (java.util.ArrayList)3 CreateStorageRequest (ddf.catalog.content.operation.CreateStorageRequest)2 MetacardCreationException (ddf.catalog.data.MetacardCreationException)2 TikaMetadataExtractor (ddf.catalog.transformer.common.tika.TikaMetadataExtractor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Serializable (java.io.Serializable)2 DefaultFtpReply (org.apache.ftpserver.ftplet.DefaultFtpReply)2 FtpFile (org.apache.ftpserver.ftplet.FtpFile)2