use of com.sputnik.ouidb.exception.NoRecordsFoundException in project ouidb-to-json-publisher by jfisbein.
the class OUIDBDownloader method download.
public Reader download() {
Cache cache = new Cache(new File(System.getProperty("java.io.tmpdir")), TEN_MB);
OkHttpClient client = new OkHttpClient.Builder().cache(cache).build();
Reader result = null;
Iterator<String> urlIterator = Arrays.asList(ouiDbUrls).iterator();
while (result == null && urlIterator.hasNext()) {
String ouiDbUrl = urlIterator.next();
Request request = new Request.Builder().url(ouiDbUrl).build();
log.info("Trying to download info from {}", ouiDbUrl);
try {
Response response = client.newCall(request).execute();
if (response.body() != null) {
String contentTypeHeader = getContentTypeHeader(response).orElse("text/plain; charset=utf-8");
if (contentTypeHeader.equalsIgnoreCase("application/x-gzip")) {
result = new InputStreamReader(new GZIPInputStream(response.body().byteStream()));
} else if (contentTypeHeader.equalsIgnoreCase("application/x-bzip2")) {
result = new InputStreamReader(new BZip2CompressorInputStream(response.body().byteStream()));
} else {
result = response.body().charStream();
}
}
} catch (IOException e) {
log.warn("Error downloading OUIs from {} - {}: {}", ouiDbUrl, e.getClass().getSimpleName(), e.getMessage());
}
}
if (result == null) {
throw new NoRecordsFoundException("No records found");
}
return result;
}
use of com.sputnik.ouidb.exception.NoRecordsFoundException in project ouidb-to-json-publisher by jfisbein.
the class OUIDBDownloader method parseDb.
protected Map<String, Organization> parseDb(Reader db) throws IOException {
Map<String, Organization> response = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
try (BufferedReader reader = new BufferedReader(db)) {
String line;
int counter = 0;
Organization organization = null;
while ((line = reader.readLine()) != null) {
line = normalize(line);
if (line.contains("(hex)")) {
String[] split = StringUtils.splitByWholeSeparator(line, "(hex)");
String prefix = normalizePrefix(split[0]);
String organizationName = normalizeOrganizationName(split[1]);
counter = 0;
organization = new Organization(organizationName);
organization.setAddress(new Address());
response.put(prefix, organization);
} else if (counter < 3 && organization != null && !line.contains("(base 16)")) {
counter = fillAddress(line, counter, organization.getAddress());
}
}
}
if (response.isEmpty()) {
throw new NoRecordsFoundException("No records found");
}
return response;
}
Aggregations