Add MaxMind result models, IpIntel model

This commit is contained in:
Colin McDonald 2016-06-17 14:58:06 -04:00
parent 3a836e8db6
commit 63e8683b67
11 changed files with 267 additions and 29 deletions

View File

@ -1,4 +1,23 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindCity {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
import java.util.Map;
public final class MaxMindCity {
@Getter private int confidence;
@Getter private int geonameId;
@Getter private String name;
public MaxMindCity() {} // For Jackson
public MaxMindCity(Document legacy) {
this.confidence = legacy.getInteger("confidence");
this.geonameId = legacy.getInteger("geoname_id");
this.name = MaxMindUtils.getEnglishName(legacy);
}
}

View File

@ -1,4 +1,21 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindContinent {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
public final class MaxMindContinent {
@Getter private String code;
@Getter private int geonameId;
@Getter private String name;
public MaxMindContinent() {} // For Jackson
public MaxMindContinent(Document legacy) {
this.code = legacy.getString("code");
this.geonameId = legacy.getInteger("geoname_id");
this.name = MaxMindUtils.getEnglishName(legacy);
}
}

View File

@ -1,4 +1,23 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindCountry {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
public final class MaxMindCountry {
@Getter private String isoCode;
@Getter private int confidence;
@Getter private int geonameId;
@Getter private String name;
public MaxMindCountry() {} // For Jackson
public MaxMindCountry(Document legacy) {
this.isoCode = legacy.getString("iso_code");
this.confidence = legacy.getInteger("confidence");
this.geonameId = legacy.getInteger("geoname_id");
this.name = MaxMindUtils.getEnglishName(legacy);
}
}

View File

@ -1,4 +1,28 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindLocation {
}
import lombok.Getter;
import org.bson.Document;
public final class MaxMindLocation {
@Getter private double latitude;
@Getter private double longitude;
@Getter private int accuracyRadius;
@Getter private String timeZone;
@Getter private int populationDensity;
@Getter private int metroCode;
@Getter private int averageIncome;
public MaxMindLocation() {} // For Jackson
public MaxMindLocation(Document legacy) {
this.latitude = legacy.getDouble("latitude");
this.longitude = legacy.getDouble("longitude");
this.accuracyRadius = legacy.getInteger("accuracy_radius");
this.timeZone = legacy.getString("time_zone");
this.populationDensity = legacy.getInteger("population_density");
this.metroCode = legacy.getInteger("metro_code");
this.averageIncome = legacy.getInteger("average_income");
}
}

View File

@ -1,4 +1,19 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindPostal {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
public final class MaxMindPostal {
@Getter private String code;
@Getter private int confidence;
public MaxMindPostal() {} // For Jackson
public MaxMindPostal(Document legacy) {
this.code = legacy.getString("code");
this.confidence = legacy.getInteger("confidence");
}
}

View File

@ -1,4 +1,21 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindRegisteredCountry {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
public final class MaxMindRegisteredCountry {
@Getter private String isoCode;
@Getter private int geonameId;
@Getter private String name;
public MaxMindRegisteredCountry() {} // For Jackson
public MaxMindRegisteredCountry(Document legacy) {
this.isoCode = legacy.getString("iso_code");
this.geonameId = legacy.getInteger("geoname_id");
this.name = MaxMindUtils.getEnglishName(legacy);
}
}

View File

@ -1,20 +1,41 @@
package net.frozenorb.apiv3.maxmind;
import lombok.Getter;
import org.bson.Document;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
public final class MaxMindResult {
private String ip;
private Instant lastUpdatedAt;
private MaxMindContinent continent;
private MaxMindCity city;
private MaxMindPostal postal;
private MaxMindTraits traits;
private MaxMindLocation location;
private MaxMindSubdivision[] subdivisions;
private MaxMindCountry country;
private MaxMindRegisteredCountry registeredCountry;
@Getter private MaxMindContinent continent;
@Getter private MaxMindCity city;
@Getter private MaxMindPostal postal;
@Getter private MaxMindTraits traits;
@Getter private MaxMindLocation location;
@Getter private MaxMindSubdivision[] subdivisions;
@Getter private MaxMindCountry country;
@Getter private MaxMindRegisteredCountry registeredCountry;
public MaxMindResult() {}
public MaxMindResult() {} // For Jackson
public MaxMindResult(Document legacy) {
this.continent = new MaxMindContinent((Document) legacy.get("continent"));
this.city = new MaxMindCity((Document) legacy.get("city"));
this.postal = new MaxMindPostal((Document) legacy.get("postal"));
this.traits = new MaxMindTraits((Document) legacy.get("traits"));
this.location = new MaxMindLocation((Document) legacy.get("location"));
this.country = new MaxMindCountry((Document) legacy.get("country"));
this.registeredCountry = new MaxMindRegisteredCountry((Document) legacy.get("registered_country"));
List<MaxMindSubdivision> subdivisions = new ArrayList<>();
for (Object subdivision : (List<Object>) legacy.get("subdivisions")) {
subdivisions.add(new MaxMindSubdivision((Document) subdivision));
}
this.subdivisions = subdivisions.toArray(new MaxMindSubdivision[subdivisions.size()]);
}
}

View File

@ -1,4 +1,23 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindSubdivision {
}
import lombok.Getter;
import net.frozenorb.apiv3.util.MaxMindUtils;
import org.bson.Document;
public final class MaxMindSubdivision {
@Getter private String isoCode;
@Getter private int confidence;
@Getter private int geonameId;
@Getter private String name;
public MaxMindSubdivision() {} // For Jackson
public MaxMindSubdivision(Document legacy) {
this.isoCode = legacy.getString("iso_code");
this.confidence = legacy.getInteger("confidence");
this.geonameId = legacy.getInteger("geoname_id");
this.name = MaxMindUtils.getEnglishName(legacy);
}
}

View File

@ -1,4 +1,26 @@
package net.frozenorb.apiv3.maxmind;
public class MaxMindTraits {
}
import lombok.Getter;
import org.bson.Document;
public final class MaxMindTraits {
@Getter private String isp;
@Getter private String domain;
@Getter private int asn;
@Getter private String asnOrganization;
@Getter private String type; // TODO: MAKE ENUM
@Getter private String organization;
public MaxMindTraits() {} // For Jackson
public MaxMindTraits(Document legacy) {
this.isp = legacy.getString("isp");
this.domain = legacy.getString("domain");
this.asn = legacy.getInteger("autonomous_system_number");
this.asnOrganization = legacy.getString("autonomous_system_organization");
this.type = legacy.getString("user_type");
this.organization = legacy.getString("organization");
}
}

View File

@ -0,0 +1,59 @@
package net.frozenorb.apiv3.model;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.MongoCollection;
import fr.javatic.mongo.jacksonCodec.Entity;
import fr.javatic.mongo.jacksonCodec.objectId.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.maxmind.MaxMindResult;
import net.frozenorb.apiv3.unsorted.BlockingCallback;
import net.frozenorb.apiv3.util.SyncUtils;
import org.bson.Document;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@Entity
@AllArgsConstructor
public final class IpIntel {
private static final MongoCollection<IpIntel> ipIntelCollection = APIv3.getDatabase().getCollection("ipIntel", IpIntel.class);
@Getter @Id private String id;
@Getter private Instant lastUpdatedAt;
@Getter private MaxMindResult result;
public static List<IpIntel> findAllSync() {
return SyncUtils.blockMulti(ipIntelCollection.find().sort(new Document("lastSeenAt", -1)));
}
public static IpIntel findByIdSync(String id) {
return SyncUtils.blockOne(ipIntelCollection.find(new Document("_id", id)));
}
public static void findAll(SingleResultCallback<List<IpIntel>> callback) {
ipIntelCollection.find().sort(new Document("lastSeenAt", -1)).into(new ArrayList<>(), callback);
}
public static void findById(String id, SingleResultCallback<IpIntel> callback) {
ipIntelCollection.find(new Document("_id", id)).first(callback);
}
public IpIntel() {} // For Jackson
public IpIntel(String ip, MaxMindResult result) {
this.id = ip;
this.lastUpdatedAt = Instant.now();
this.result = result;
}
public void insert() {
BlockingCallback<Void> callback = new BlockingCallback<>();
ipIntelCollection.insertOne(this, callback);
callback.get();
}
}

View File

@ -4,9 +4,11 @@ import com.google.common.base.Charsets;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.maxmind.MaxMindResult;
import org.bson.Document;
import java.util.Base64;
import java.util.Map;
@UtilityClass
public class MaxMindUtils {
@ -14,12 +16,12 @@ public class MaxMindUtils {
private static final String maxMindUserId = APIv3.getConfig().getProperty("maxMind.userId");
private static final String maxMindLicenseKey = APIv3.getConfig().getProperty("maxMind.maxMindLicenseKey");
public static void getInsights(String ip, SingleResultCallback<Document> callback) {
public static void getInsights(String ip, SingleResultCallback<MaxMindResult> callback) {
// We have to specifically use getHttpSClient(), vertx's http client is dumb.
APIv3.getHttpsClient().get(443, "geoip.maxmind.com", "/geoip/v2.1/insights/" + ip, (response) -> {
response.bodyHandler((body) -> {
Document resJson = Document.parse(body.toString());
callback.onResult(resJson, null);
callback.onResult(new MaxMindResult(ip, resJson), null);
});
response.exceptionHandler((error) -> {
@ -28,4 +30,8 @@ public class MaxMindUtils {
}).putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString((maxMindUserId + ":" + maxMindLicenseKey).getBytes(Charsets.UTF_8))).end();
}
public static String getEnglishName(Document source) {
return ((Map<String, String>) source.get("names")).get("en");
}
}