Search in sources :

Example 1 with NoRecordsFoundException

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) Request(okhttp3.Request) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) NoRecordsFoundException(com.sputnik.ouidb.exception.NoRecordsFoundException) Response(okhttp3.Response) GZIPInputStream(java.util.zip.GZIPInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) File(java.io.File) Cache(okhttp3.Cache)

Example 2 with NoRecordsFoundException

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;
}
Also used : Organization(com.sputnik.ouidb.model.Organization) Address(com.sputnik.ouidb.model.Address) BufferedReader(java.io.BufferedReader) TreeMap(java.util.TreeMap) NoRecordsFoundException(com.sputnik.ouidb.exception.NoRecordsFoundException)

Aggregations

NoRecordsFoundException (com.sputnik.ouidb.exception.NoRecordsFoundException)2 BufferedReader (java.io.BufferedReader)2 Address (com.sputnik.ouidb.model.Address)1 Organization (com.sputnik.ouidb.model.Organization)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 TreeMap (java.util.TreeMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Cache (okhttp3.Cache)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)1