Search in sources :

Example 1 with GeoEntryExtractionException

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

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

the class TestGeoNamesLuceneIndexerExceptions method testExceptionWhenAddingDocumentByExtractor.

@Test
public void testExceptionWhenAddingDocumentByExtractor() throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException, IOException {
    configureMocks();
    geoNamesLuceneIndexer.setIndexLocation(ABSOLUTE_PATH + TEST_PATH + "index");
    try {
        final GeoEntryExtractor geoEntryExtractor = 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) throws GeoEntryExtractionException {
                extractionCallback.updateProgress(0);
                try {
                    extractionCallback.extracted(GEO_ENTRY);
                } catch (GeoEntryIndexingException e) {
                    throw new GeoEntryExtractionException("Unable to add entry.", e);
                }
            }

            @Override
            public void setUrl(String url) {
                return;
            }
        };
        geoNamesLuceneIndexer.updateIndex("", geoEntryExtractor, true, null);
        fail("Should have thrown a GeoEntryExtractionException; extractionCallback.extract() threw " + "a GeoEntryIndexingException because addDocument() threw an " + "IOException.");
    } catch (GeoEntryExtractionException e) {
        assertThat(e.getCause(), instanceOf(GeoEntryIndexingException.class));
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 3 with GeoEntryExtractionException

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

the class GeoNamesFileExtractor method getInputStreamFromResource.

/**
     * Determines the appropriate InputStream to get from the given file resource.  If there is no
     * file extension, the resource is downloaded from a url ( default : http://download.geonames.org/export/dump/ )
     * otherwise it is treated as an absolute path for a file.
     *
     * @param resource           - a String representing the resource.  Could be a data set from GeoNames
     *                           (allCities, cities15000, AU, US etc) or an absolute path.
     * @param extractionCallback - the callback to receive updates about the progress, may be
     *                           null if you don't want any updates
     * @return the InputStream for the given resource
     * @throws GeoEntryExtractionException     if an error occurs during extraction, obtaining the InputStream
     *                                         from a local resource, or if the resource is not a .zip or .txt file
     * @throws GeoNamesRemoteDownloadException if an error occurs when attempting to get an InputStream
     *                                         from a URL
     */
private InputStream getInputStreamFromResource(String resource, ProgressCallback extractionCallback) throws GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    if (FilenameUtils.isExtension(resource, "zip")) {
        InputStream inputStream = getFileInputStream(resource);
        return unZipInputStream(resource, inputStream);
    } else if (FilenameUtils.isExtension(resource, "txt")) {
        return getFileInputStream(resource);
    } else if (StringUtils.isAlphanumeric(resource)) {
        // Support the "all" keyword
        if (resource.equalsIgnoreCase("all")) {
            resource = "allCountries";
        // Support case insensitive "cities" keyword
        } else if (resource.matches("((?i)cities[0-9]+)")) {
            resource = resource.toLowerCase();
        // Support case insensitive country codes
        } else if (!resource.equalsIgnoreCase("allCountries")) {
            resource = resource.toUpperCase();
        }
        Response response = createConnection(resource);
        InputStream urlInputStream = getUrlInputStreamFromWebClient();
        InputStream inputStream = getInputStreamFromUrl(resource, response, urlInputStream, extractionCallback);
        return unZipInputStream(resource, inputStream);
    }
    throw new GeoEntryExtractionException("Unable to update the index.  " + resource + " is not a .zip or .txt, or an invalid country code was entered.");
}
Also used : Response(javax.ws.rs.core.Response) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream)

Example 4 with GeoEntryExtractionException

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

the class GeoNamesFileExtractor method getFileInputStream.

/**
     * Get the InputStream for the given file resource.
     *
     * @param resource - the absolute path of the file to open the InputStream for.
     * @return the InputStream for the file resource
     * @throws GeoEntryExtractionException when the file cannot be found.
     */
private InputStream getFileInputStream(String resource) throws GeoEntryExtractionException {
    FileInputStream fileInputStream;
    try {
        File file = new File(resource);
        fileSize = file.getTotalSpace();
        fileInputStream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new GeoEntryExtractionException(resource + " cannot be found", e);
    }
    return fileInputStream;
}
Also used : GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with GeoEntryExtractionException

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

the class GeoNamesFileExtractor method pushGeoEntriesToExtractionCallback.

@Override
public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) throws GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    if (extractionCallback == null) {
        throw new IllegalArgumentException("You must pass a non-null callback.");
    }
    InputStream fileInputStream = getInputStreamFromResource(resource, extractionCallback);
    try (InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
        BufferedReader reader = new BufferedReader(inputStreamReader)) {
        double bytesRead = 0.0;
        for (String line; (line = reader.readLine()) != null; ) {
            extractionCallback.extracted(extractGeoEntry(line));
            bytesRead += line.getBytes(StandardCharsets.UTF_8).length;
            extractionCallback.updateProgress((int) (50 + (bytesRead / fileSize) * 50));
        }
        extractionCallback.updateProgress(100);
    } catch (IOException e) {
        throw new GeoEntryExtractionException("Unable to open stream for " + resource, e);
    } catch (IndexOutOfBoundsException | NumberFormatException e) {
        throw new GeoEntryExtractionException(resource + " does not follow the " + "expected GeoNames file format.", e);
    } catch (GeoEntryIndexingException e) {
        throw new GeoEntryExtractionException("Unable to extract GeoEntry from " + resource + ".", e);
    }
}
Also used : GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) InputStreamReader(java.io.InputStreamReader) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Aggregations

GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)11 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)7 ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)6 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)4 Test (org.junit.Test)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 List (java.util.List)3 ZipInputStream (java.util.zip.ZipInputStream)3 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)3 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)3 GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ByteSource (com.google.common.io.ByteSource)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1