Search in sources :

Example 1 with GeoNamesRemoteDownloadException

use of org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException in project ddf by codice.

the class Geocoding method updateIndex.

private boolean updateIndex(String resource, boolean createIndex) {
    final ProgressCallback progressCallback = new ProgressCallback() {

        @Override
        public void updateProgress(final int progress) {
            setProgress(progress);
        }
    };
    LOGGER.trace("Updating GeoNames Index...");
    try {
        geoEntryIndexer.updateIndex(resource, geoEntryExtractor, createIndex, progressCallback);
        LOGGER.trace("\nDone Updating GeoNames Index.");
        LOGGER.debug("Done Updating GeoNames Index with : {}", resource);
        return true;
    } catch (GeoEntryExtractionException e) {
        LOGGER.debug("Error extracting GeoNames data from resource {}", resource, e);
        return false;
    } catch (GeoEntryIndexingException e) {
        LOGGER.debug("Error indexing GeoNames data", e);
        return false;
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.debug("Error downloading resource from remote source {}", resource, e);
        return false;
    }
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Example 2 with GeoNamesRemoteDownloadException

use of org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException in project ddf by codice.

the class GeoNamesUpdateCommand method execute.

@Override
public Object execute() {
    final PrintStream console = System.out;
    final ProgressCallback progressCallback = new ProgressCallback() {

        @Override
        public void updateProgress(final int progress) {
            console.printf("\r%d%%", progress);
            console.flush();
        }
    };
    console.println("Updating...");
    try {
        geoEntryIndexer.updateIndex(resource, geoEntryExtractor, create, progressCallback);
        console.println("\nDone.");
    } catch (GeoEntryExtractionException e) {
        LOGGER.debug("Error extracting GeoNames data from resource {}", resource, e);
        console.printf("Could not extract GeoNames data from resource %s.%n" + "Message: %s%n" + "Check the logs for more details.%n", resource, e.getMessage());
    } catch (GeoEntryIndexingException e) {
        LOGGER.debug("Error indexing GeoNames data", e);
        console.printf("Could not index the GeoNames data.%n" + "Message: %s%n" + "Check the logs for more details.%n", e.getMessage());
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.debug("Error downloading resource from remote source {}", resource, e);
        console.printf("Could not download the GeoNames file %s.%n  Message: %s%n" + "Check the logs for more details.%n", resource, e.getMessage());
    }
    return null;
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) PrintStream(java.io.PrintStream) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Example 3 with GeoNamesRemoteDownloadException

use of org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException 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 4 with GeoNamesRemoteDownloadException

use of org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException in project ddf by codice.

the class TestGeoNamesUpdateCommand method testProgressOutput.

@Test
public void testProgressOutput() throws IOException {
    final GeoEntryExtractor geoEntryExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryExtractor geoEntryUrlExtractor = spy(new GeoEntryExtractor() {

        @Override
        public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
            return null;
        }

        @Override
        public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) {
            extractionCallback.updateProgress(50);
            assertThat(consoleInterceptor.getOutput(), containsString("50%"));
            extractionCallback.updateProgress(100);
            assertThat(consoleInterceptor.getOutput(), containsString("100%"));
        }

        @Override
        public void setUrl(String url) {
            return;
        }
    });
    final GeoEntryIndexer geoEntryIndexer = spy(new GeoEntryIndexer() {

        @Override
        public void updateIndex(final List<GeoEntry> newEntries, final boolean create, final ProgressCallback progressCallback) {
        }

        @Override
        public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException, GeoEntryIndexingException, GeoEntryExtractionException {
            final ExtractionCallback extractionCallback = new ExtractionCallback() {

                @Override
                public void extracted(final GeoEntry newEntry) {
                }

                @Override
                public void updateProgress(final int progress) {
                    progressCallback.updateProgress(progress);
                }
            };
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        }
    });
    List<GeoEntryExtractor> geoEntryExtractors = new ArrayList<GeoEntryExtractor>();
    geoEntryExtractors.add(geoEntryExtractor);
    geoEntryExtractors.add(geoEntryUrlExtractor);
    geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    geoNamesUpdateCommand.setResource("test");
    geoNamesUpdateCommand.execute();
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ArrayList(java.util.ArrayList) List(java.util.List) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Aggregations

GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)4 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)3 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)3 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)3 ByteSource (com.google.common.io.ByteSource)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TemporaryFileBackedOutputStream (org.codice.ddf.platform.util.TemporaryFileBackedOutputStream)1 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)1 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)1 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)1 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1