use of org.codice.ddf.spatial.geocoding.GeoEntryExtractionException 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;
}
use of org.codice.ddf.spatial.geocoding.GeoEntryExtractionException 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.spatial.geocoding.GeoEntryExtractionException in project ddf by codice.
the class TestGeoNamesUpdateCommand method testExceptionDuringExtraction.
@Test
public void testExceptionDuringExtraction() throws IOException, GeoNamesRemoteDownloadException, GeoEntryExtractionException, GeoEntryIndexingException {
final String errorText = "Extraction error text";
final GeoEntryExtractor geoEntryExtractor = mock(GeoEntryExtractor.class);
final GeoEntryExtractionException geoEntryExtractionException = new GeoEntryExtractionException(errorText);
doThrow(geoEntryExtractionException).when(geoEntryExtractor).pushGeoEntriesToExtractionCallback(anyString(), any(ExtractionCallback.class));
final GeoEntryIndexer geoEntryIndexer = 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, GeoEntryExtractionException, GeoEntryIndexingException {
geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, mock(ExtractionCallback.class));
}
};
geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
geoNamesUpdateCommand.setResource("temp.txt");
geoNamesUpdateCommand.execute();
assertThat(consoleInterceptor.getOutput(), containsString(errorText));
consoleInterceptor.resetSystemOut();
consoleInterceptor.closeBuffer();
}
use of org.codice.ddf.spatial.geocoding.GeoEntryExtractionException 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();
}
use of org.codice.ddf.spatial.geocoding.GeoEntryExtractionException in project ddf by codice.
the class TestGeoNamesLuceneIndexer method testCreateIndexFromExtractor.
@Test
public void testCreateIndexFromExtractor() throws IOException, GeoEntryIndexingException, GeoNamesRemoteDownloadException, GeoEntryExtractionException {
configureMocks();
final ProgressCallback progressCallback = mock(ProgressCallback.class);
geoNamesLuceneIndexer.updateIndex(null, 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 {
try {
extractionCallback.extracted(GEO_ENTRY_1);
extractionCallback.extracted(GEO_ENTRY_2);
extractionCallback.extracted(GEO_ENTRY_3);
extractionCallback.extracted(GEO_ENTRY_4);
extractionCallback.extracted(GEO_ENTRY_5);
extractionCallback.extracted(GEO_ENTRY_6);
extractionCallback.extracted(GEO_ENTRY_7);
extractionCallback.extracted(GEO_ENTRY_8);
extractionCallback.extracted(GEO_ENTRY_9);
extractionCallback.updateProgress(100);
} catch (GeoEntryIndexingException e) {
throw new GeoEntryExtractionException("Unable to add entry.", e);
}
}
@Override
public void setUrl(String url) {
return;
}
}, true, progressCallback);
verify(indexWriter, times(9)).addDocument(documentArgumentCaptor.capture());
final List<Document> documentList = documentArgumentCaptor.getAllValues();
verifyDocumentList(documentList);
verify(progressCallback, times(1)).updateProgress(100);
}
Aggregations