Add defaults to MaxMind result parsing

This commit is contained in:
Colin McDonald 2016-06-23 13:29:27 -04:00
parent 61197b1050
commit 2e439a5b18
10 changed files with 25 additions and 27 deletions

View File

@ -13,8 +13,8 @@ public final class MaxMindCity {
public MaxMindCity() {} // For Jackson public MaxMindCity() {} // For Jackson
public MaxMindCity(JsonObject legacy) { public MaxMindCity(JsonObject legacy) {
this.confidence = legacy.getInteger("confidence"); this.confidence = legacy.getInteger("confidence", -1);
this.geonameId = legacy.getInteger("geoname_id"); this.geonameId = legacy.getInteger("geoname_id", -1);
this.name = MaxMindUtils.getEnglishName(legacy); this.name = MaxMindUtils.getEnglishName(legacy);
} }

View File

@ -13,8 +13,8 @@ public final class MaxMindContinent {
public MaxMindContinent() {} // For Jackson public MaxMindContinent() {} // For Jackson
public MaxMindContinent(JsonObject legacy) { public MaxMindContinent(JsonObject legacy) {
this.code = legacy.getString("code"); this.code = legacy.getString("code", "");
this.geonameId = legacy.getInteger("geoname_id"); this.geonameId = legacy.getInteger("geoname_id", -1);
this.name = MaxMindUtils.getEnglishName(legacy); this.name = MaxMindUtils.getEnglishName(legacy);
} }

View File

@ -14,9 +14,9 @@ public final class MaxMindCountry {
public MaxMindCountry() {} // For Jackson public MaxMindCountry() {} // For Jackson
public MaxMindCountry(JsonObject legacy) { public MaxMindCountry(JsonObject legacy) {
this.isoCode = legacy.getString("iso_code"); this.isoCode = legacy.getString("iso_code", "");
this.confidence = legacy.getInteger("confidence"); this.confidence = legacy.getInteger("confidence", -1);
this.geonameId = legacy.getInteger("geoname_id"); this.geonameId = legacy.getInteger("geoname_id", -1);
this.name = MaxMindUtils.getEnglishName(legacy); this.name = MaxMindUtils.getEnglishName(legacy);
} }

View File

@ -12,7 +12,7 @@ public final class MaxMindPostal {
public MaxMindPostal(JsonObject legacy) { public MaxMindPostal(JsonObject legacy) {
this.code = legacy.getString("code", ""); this.code = legacy.getString("code", "");
this.confidence = legacy.getInteger("confidence"); this.confidence = legacy.getInteger("confidence", -1);
} }
} }

View File

@ -13,8 +13,8 @@ public final class MaxMindRegisteredCountry {
public MaxMindRegisteredCountry() {} // For Jackson public MaxMindRegisteredCountry() {} // For Jackson
public MaxMindRegisteredCountry(JsonObject legacy) { public MaxMindRegisteredCountry(JsonObject legacy) {
this.isoCode = legacy.getString("iso_code"); this.isoCode = legacy.getString("iso_code", "");
this.geonameId = legacy.getInteger("geoname_id"); this.geonameId = legacy.getInteger("geoname_id", -1);
this.name = MaxMindUtils.getEnglishName(legacy); this.name = MaxMindUtils.getEnglishName(legacy);
} }

View File

@ -21,13 +21,13 @@ public final class MaxMindResult {
public MaxMindResult() {} // For Jackson public MaxMindResult() {} // For Jackson
public MaxMindResult(JsonObject legacy) { public MaxMindResult(JsonObject legacy) {
this.continent = new MaxMindContinent(legacy.getJsonObject("continent")); this.continent = new MaxMindContinent(legacy.getJsonObject("continent", new JsonObject()));
this.city = new MaxMindCity(legacy.getJsonObject("city")); this.city = new MaxMindCity(legacy.getJsonObject("city", new JsonObject()));
this.postal = new MaxMindPostal(legacy.getJsonObject("postal")); this.postal = new MaxMindPostal(legacy.getJsonObject("postal", new JsonObject()));
this.traits = new MaxMindTraits(legacy.getJsonObject("traits")); this.traits = new MaxMindTraits(legacy.getJsonObject("traits"));
this.location = new MaxMindLocation(legacy.getJsonObject("location")); this.location = new MaxMindLocation(legacy.getJsonObject("location", new JsonObject()));
this.country = new MaxMindCountry(legacy.getJsonObject("country")); this.country = new MaxMindCountry(legacy.getJsonObject("country", new JsonObject()));
this.registeredCountry = new MaxMindRegisteredCountry(legacy.getJsonObject("registered_country")); this.registeredCountry = new MaxMindRegisteredCountry(legacy.getJsonObject("registered_country", new JsonObject()));
List<MaxMindSubdivision> subdivisions = new LinkedList<>(); List<MaxMindSubdivision> subdivisions = new LinkedList<>();

View File

@ -14,9 +14,9 @@ public final class MaxMindSubdivision {
public MaxMindSubdivision() {} // For Jackson public MaxMindSubdivision() {} // For Jackson
public MaxMindSubdivision(JsonObject legacy) { public MaxMindSubdivision(JsonObject legacy) {
this.isoCode = legacy.getString("iso_code"); this.isoCode = legacy.getString("iso_code", "");
this.confidence = legacy.getInteger("confidence", -1); this.confidence = legacy.getInteger("confidence", -1);
this.geonameId = legacy.getInteger("geoname_id"); this.geonameId = legacy.getInteger("geoname_id", -1);
this.name = MaxMindUtils.getEnglishName(legacy); this.name = MaxMindUtils.getEnglishName(legacy);
} }

View File

@ -15,12 +15,12 @@ public final class MaxMindTraits {
public MaxMindTraits() {} // For Jackson public MaxMindTraits() {} // For Jackson
public MaxMindTraits(JsonObject legacy) { public MaxMindTraits(JsonObject legacy) {
this.isp = legacy.getString("isp"); this.isp = legacy.getString("isp", "");
this.domain = legacy.getString("domain"); this.domain = legacy.getString("domain", "");
this.asn = legacy.getInteger("autonomous_system_number"); this.asn = legacy.getInteger("autonomous_system_number", -1);
this.asnOrganization = legacy.getString("autonomous_system_organization"); this.asnOrganization = legacy.getString("autonomous_system_organization" , "");
this.userType = MaxMindUserType.valueOf(legacy.getString("user_type").toUpperCase()); this.userType = MaxMindUserType.valueOf(legacy.getString("user_type", "").toUpperCase());
this.organization = legacy.getString("organization"); this.organization = legacy.getString("organization", "");
} }
} }

View File

@ -20,7 +20,6 @@ public enum MaxMindUserType {
SEARCH_ENGINE_SPIDER(false), SEARCH_ENGINE_SPIDER(false),
TRAVELER(true); TRAVELER(true);
// TODO: ACTUALLY CARE ABOUT THIS
@Getter private boolean allowed; @Getter private boolean allowed;
MaxMindUserType(boolean allowed) { MaxMindUserType(boolean allowed) {

View File

@ -19,7 +19,6 @@ public class MaxMindUtils {
private static final HttpClient httpsClient = APIv3.getVertxInstance().createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true)); private static final HttpClient httpsClient = APIv3.getVertxInstance().createHttpClient(new HttpClientOptions().setSsl(true).setTrustAll(true));
public static void getInsights(String ip, SingleResultCallback<MaxMindResult> callback) { public static void getInsights(String ip, SingleResultCallback<MaxMindResult> callback) {
// We have to specifically use getHttpSClient(), vertx's http client is dumb.
httpsClient.get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> { httpsClient.get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> {
response.bodyHandler((body) -> { response.bodyHandler((body) -> {
JsonObject bodyJson = new JsonObject(body.toString()); JsonObject bodyJson = new JsonObject(body.toString());
@ -33,7 +32,7 @@ public class MaxMindUtils {
} }
public static String getEnglishName(JsonObject source) { public static String getEnglishName(JsonObject source) {
return source.getJsonObject("names").getString("en", "INVALID"); return source.getJsonObject("names", new JsonObject()).getString("en", "INVALID");
} }
} }