diff --git a/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java b/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java index 25d97e9..368e649 100644 --- a/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java +++ b/src/main/java/fun/wilddev/geo/importer/LocationFileImporter.java @@ -57,32 +57,36 @@ public int startImport() throws FileReaderException { String[] line; List buff = new ArrayList<>(BUFF_SIZE); - for (int i = 0; (line = csv.readNext()) != null; i++) { + while ((line = csv.readNext()) != null) { - if (i % BUFF_SIZE == 0) { + if (line.length < 3) { - locationRepository.saveAll(buff); - buff.clear(); - - log.debug("Flushed"); + log.debug("Bad entry, skipping ..."); + continue; + } - } else { + LocationD location = new LocationD(new Country(line[1], line[0]), line[2]); - if (line.length < 3) { + buff.add(location); + counter++; - log.debug("Bad entry, skipping ..."); - continue; - } + log.info("Location read: {}", location); - LocationD location = new LocationD(new Country(line[1], line[0]), line[2]); + if (buff.size() >= BUFF_SIZE) { - buff.add(location); - counter++; + locationRepository.saveAll(buff); + buff.clear(); - log.info("Location read: {}", location); + log.debug("Flushed"); } } + if (!buff.isEmpty()) { + + locationRepository.saveAll(buff); + log.debug("Flushed remaining"); + } + } catch (Exception ex) { throw new FileReaderException("Failed to read location file", ex); } diff --git a/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java b/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java index 92aea63..9997a26 100644 --- a/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java +++ b/src/main/java/fun/wilddev/geo/integrations/ipstack/IpStackHttp.java @@ -50,6 +50,9 @@ public Location find(@NonNull String ip) throws HttpRequestFailedException { val source = response.bodyTo(IpStackLocationResponse.class); + if (source == null) + throw new HttpRequestFailedException("null response"); + if (source.getSuccess() == Boolean.FALSE) throw new HttpRequestFailedException("Request rejected: " + source);