Hey it's (more) code!

This commit is contained in:
Colin McDonald 2016-02-11 20:40:06 -05:00
parent 6adaa63089
commit 5f6113ce77
37 changed files with 776 additions and 635 deletions

View File

@ -1,36 +1,20 @@
package net.frozenorb.apiv3;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoDatabase;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterSettings;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.web.Router;
import lombok.Getter;
import net.frozenorb.apiv3.collections.AuditLog;
import net.frozenorb.apiv3.collections.Grant;
import net.frozenorb.apiv3.collections.Server;
import net.frozenorb.apiv3.mongoCodec.InstantCodec;
import net.frozenorb.apiv3.utils.ErrorUtils;
import org.bson.BsonType;
import net.frozenorb.apiv3.accessor.AuditLog;
import net.frozenorb.apiv3.accessor.Grants;
import net.frozenorb.apiv3.accessor.Servers;
import net.frozenorb.apiv3.util.ErrorUtils;
import net.frozenorb.apiv3.util.JsonUtils;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import java.time.Instant;
import java.util.UUID;
import java.util.stream.Collectors;
public final class APIv3 extends AbstractVerticle {
@ -38,27 +22,7 @@ public final class APIv3 extends AbstractVerticle {
public void start() {
Router coreHttpRouter = Router.router(vertx);
BsonTypeClassMap codecMap = new BsonTypeClassMap(ImmutableMap.of(
BsonType.DATE_TIME, Instant.class
));
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new InstantCodec()),
CodecRegistries.fromProviders(new DocumentCodecProvider(codecMap)),
CodecRegistries.fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider())
);
MongoClientSettings settings =
MongoClientSettings.builder()
.clusterSettings(
ClusterSettings.builder()
.mode(ClusterConnectionMode.SINGLE)
.hosts(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)))
.build()
)
.credentialList(ImmutableList.of(MongoCredential.createCredential("test", "minehqapi", "test".toCharArray())))
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(settings);
mongo = mongoClient.getDatabase("minehqapi");
mongo = MongoUtils.initializeConnection(ImmutableList.of(new ServerAddress("ds055505.mongolab.com", 55505)), "minehqapi", "test", "test".toCharArray());
// We always reply in JSON.
coreHttpRouter.route("/*").handler(ctx -> {
@ -67,25 +31,25 @@ public final class APIv3 extends AbstractVerticle {
});
coreHttpRouter.get("/servers").handler(ctx -> {
Server.findAll(res -> {
if (res.succeeded()) {
JsonArray response = new JsonArray();
res.result().forEach(server -> response.add(server.toLiteJson()));
ctx.response().end(response.encode());
Servers.findAll((servers, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson());
ctx.response().end(JsonUtils.toLiteJsonString(servers));
}
});
});
coreHttpRouter.get("/server/:server").handler(ctx -> {
Server.findById(ctx.request().getParam("server"), res -> {
if (res.succeeded()) {
res.result().update();
ctx.response().end(res.result().toFullJson().toJson());
String serverId = ctx.request().getParam("server");
Servers.findById(serverId, (server, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else if (server != null) {
ctx.response().end(JsonUtils.toLiteJsonString(server));
} else {
ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson());
ctx.response().end(ErrorUtils.createResponse("Server '" + serverId + "' not found.").toJson());
}
});
});
@ -93,39 +57,30 @@ public final class APIv3 extends AbstractVerticle {
coreHttpRouter.get("/user/:user/grants").handler(ctx -> {
UUID target = UUID.fromString(ctx.request().getParam("user"));
Grant.findByTarget(target, res -> {
if (res.succeeded()) {
JsonArray response = new JsonArray();
res.result().forEach(grant -> response.add(grant.toLiteJson()));
ctx.response().end(response.encode());
Grants.findByTarget(target, (grants, error) -> {
if (error != null) {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(ErrorUtils.createResponse(res.cause()).toJson());
ctx.response().end(JsonUtils.toLiteJsonString(grants));
}
});
});
coreHttpRouter.get("/auditLog").handler(ctx -> {
AuditLog.findAll((auditLog, error) -> {
AuditLog.findAll((auditLogEntries, error) -> {
if (error != null) {
auditLog.stream().map(AuditLog::toLiteJson).collect(Collectors.toList());
JsonArray response = new JsonArray();
auditLog.result().forEach(server -> {
response.add(server.toLiteJson());
});
ctx.response().end(response.encode());
} else {
ctx.response().end(ErrorUtils.createResponse(error).toJson());
} else {
ctx.response().end(JsonUtils.toLiteJsonString(auditLogEntries));
}
});
});
coreHttpRouter.get("/doAction/:actionType").handler(ctx -> {
String actionType = ctx.request().getParam("actionType");
AuditLog.log(UUID.randomUUID(), "192.168.1.1", actionType, new Document());
ctx.response().end("its done");
AuditLog.log(UUID.randomUUID(), "192.168.1.103", actionType, new Document());
ctx.response().end("{'logged': true}");
});
vertx.createHttpServer().requestHandler(coreHttpRouter::accept).listen(8080);

View File

@ -2,8 +2,6 @@ package net.frozenorb.apiv3;
import org.bson.Document;
import java.util.Collection;
public interface LiteFullJson {
Document toLiteJson();
@ -11,16 +9,4 @@ public interface LiteFullJson {
return toLiteJson();
}
static String toLiteJson(Object value) {
if (value instanceof Collection) {
} else {
}
}
static String toFullJson(Object value) {
}
}

View File

@ -0,0 +1,51 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.model.AuditLogEntry;
import net.frozenorb.apiv3.util.IPUtils;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;
@UtilityClass
public class AuditLog {
public static final String COLLECTION_NAME = "auditLog";
private static final Document TIME_BASED_SORT = new Document("performedAt", -1);
public static void findAll(SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLogEntry::new, callback);
}
public static void findByPerformer(UUID performer, SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), TIME_BASED_SORT, AuditLogEntry::new, callback);
}
public static void findByPerformerIp(long machineIp, SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", machineIp), TIME_BASED_SORT, AuditLogEntry::new, callback);
}
public static void findByActionType(String actionType, SingleResultCallback<Collection<AuditLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), TIME_BASED_SORT, AuditLogEntry::new, callback);
}
public static void log(UUID user, String userIp, String actionType, Document data) {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("performedBy", user.toString());
insert.put("performedAt", new Date());
insert.put("performedFrom", IPUtils.humanToMachine(userIp));
insert.put("actionType", actionType);
insert.put("actionData", data);
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (result, error) -> {});
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.EmailTemplate;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class EmailTemplates {
public static final String COLLECTION_NAME = "emailTemplate";
public static void findAll(SingleResultCallback<Collection<EmailTemplate>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback);
}
public static void findById(String id, SingleResultCallback<EmailTemplate> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback);
}
}

View File

@ -0,0 +1,34 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.Grant;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
import java.util.UUID;
@UtilityClass
public class Grants {
public static final String COLLECTION_NAME = "grant";
private static final Document TIME_BASED_SORT = new Document("addedAt", -1);
public static void findAll(SingleResultCallback<Collection<Grant>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, Grant::new, callback);
}
public static void findById(String id, SingleResultCallback<Grant> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), TIME_BASED_SORT, Grant::new, callback);
}
public static void findByTarget(UUID target, SingleResultCallback<Collection<Grant>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), TIME_BASED_SORT, Grant::new, callback);
}
public static void findByAddedBy(UUID addedBy, SingleResultCallback<Collection<Grant>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), TIME_BASED_SORT, Grant::new, callback);
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.IPBan;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class IPBans {
public static final String COLLECTION_NAME = "ipBan";
public static void findAll(SingleResultCallback<Collection<IPBan>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback);
}
public static void findById(String id, SingleResultCallback<IPBan> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback);
}
}

View File

@ -0,0 +1,25 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.IPLogEntry;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class IPLog {
public static final String COLLECTION_NAME = "ipLog";
public static void findAll(SingleResultCallback<Collection<IPLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLogEntry::new, callback);
}
public static void findById(String id, SingleResultCallback<IPLogEntry> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPLogEntry::new, callback);
}
}

View File

@ -0,0 +1,25 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.MaxMindCacheEntry;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class MaxMindCache {
public static final String COLLECTION_NAME = "maxMindCache";
public static void findAll(SingleResultCallback<Collection<MaxMindCacheEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), MaxMindCacheEntry::new, callback);
}
public static void findById(String id, SingleResultCallback<MaxMindCacheEntry> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), MaxMindCacheEntry::new, callback);
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.NotificationLogEntry;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class NotificationLog {
public static final String COLLECTION_NAME = "notificationLog";
public static void findAll(SingleResultCallback<Collection<NotificationLogEntry>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLogEntry::new, callback);
}
public static void findById(String id, SingleResultCallback<NotificationLogEntry> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationLogEntry::new, callback);
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.Punishment;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class Punishments {
public static final String COLLECTION_NAME = "punishment";
public static void findAll(SingleResultCallback<Collection<Punishment>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback);
}
public static void findById(String id, SingleResultCallback<Punishment> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback);
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.ServerGroup;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class ServerGroups {
public static final String COLLECTION_NAME = "serverGroup";
public static void findAll(SingleResultCallback<Collection<ServerGroup>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback);
}
public static void findById(String id, SingleResultCallback<ServerGroup> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback);
}
}

View File

@ -0,0 +1,28 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.Server;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class Servers {
public static final String COLLECTION_NAME = "server";
public static void findAll(SingleResultCallback<Collection<Server>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback);
}
public static void findById(String id, SingleResultCallback<Server> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback);
}
public static void findByGroup(String groupId, SingleResultCallback<Collection<Server>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback);
}
}

View File

@ -0,0 +1,24 @@
package net.frozenorb.apiv3.accessor;
import com.mongodb.async.SingleResultCallback;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.model.User;
import net.frozenorb.apiv3.util.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@UtilityClass
public class Users {
public static final String COLLECTION_NAME = "user";
public static void findAll(SingleResultCallback<Collection<User>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback);
}
public static void findById(String id, SingleResultCallback<User> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), User::new, callback);
}
}

View File

@ -1,91 +0,0 @@
package net.frozenorb.apiv3.collections;
import com.mongodb.async.SingleResultCallback;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.IPUtils;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;
@ToString
public final class AuditLog implements LiteFullJson {
private static final String COLLECTION_NAME = "auditLog";
private static final Document TIME_BASED_SORT = new Document("performedAt", -1);
@Getter private String id;
@Getter private UUID performedBy;
@Getter private Instant performedAt;
@Getter private long performedFrom;
@Getter private String actionType;
@Getter private Document actionData;
public static void findAll(SingleResultCallback<Collection<AuditLog>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), TIME_BASED_SORT, AuditLog::new, callback);
}
public static void findByPerformer(UUID performer, Handler<AsyncResult<Collection<AuditLog>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedBy", performer.toString()), AuditLog::new, callback);
}
public static void findByPerformerIp(long machineIp, Handler<AsyncResult<Collection<AuditLog>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("performedFrom", machineIp), AuditLog::new, callback);
}
public static void findByActionType(String actionType, Handler<AsyncResult<Collection<AuditLog>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("actionType", actionType), AuditLog::new, callback);
}
public static void log(UUID user, String userIp, String actionType, Document data) {
Document insert = new Document();
insert.put("_id", new ObjectId().toString());
insert.put("performedBy", user.toString());
insert.put("performedAt", new Date());
insert.put("performedFrom", IPUtils.humanToMachine(userIp));
insert.put("actionType", actionType);
insert.put("actionData", data);
APIv3.getMongo().getCollection(COLLECTION_NAME).insertOne(insert, (result, error) -> {});
}
private AuditLog(Document json) {
this.id = json.getString("_id");
this.performedBy = UUID.fromString(json.getString("performedBy"));
this.performedAt = json.get("performedAt", Instant.class);
this.performedFrom = json.getLong("performedFrom");
this.actionType = json.getString("actionType");
this.actionData = json.get("actionData", Document.class);
}
public Document toLiteJson() {
Document json = new Document();
json.put("id", id);
json.put("performedBy", performedBy.toString());
json.put("performedAt", performedAt.toString());
json.put("performedFrom", IPUtils.machineToHuman(performedFrom));
json.put("actionType", actionType);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
json.put("actionData", actionData);
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class IPBan implements LiteFullJson {
private static final String COLLECTION_NAME = "ipBan";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<IPBan>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPBan::new, callback);
}
public static void findById(String id, Handler<AsyncResult<IPBan>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPBan::new, callback);
}
private IPBan(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class IPLog implements LiteFullJson {
private static final String COLLECTION_NAME = "ipLog";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<IPLog>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), IPLog::new, callback);
}
public static void findById(String id, Handler<AsyncResult<IPLog>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), IPLog::new, callback);
}
private IPLog(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class MaxMindCache implements LiteFullJson {
private static final String COLLECTION_NAME = "maxMindCache";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<MaxMindCache>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), MaxMindCache::new, callback);
}
public static void findById(String id, Handler<AsyncResult<MaxMindCache>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), MaxMindCache::new, callback);
}
private MaxMindCache(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class NotificationLog implements LiteFullJson {
private static final String COLLECTION_NAME = "notificationLog";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<NotificationLog>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), NotificationLog::new, callback);
}
public static void findById(String id, Handler<AsyncResult<NotificationLog>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), NotificationLog::new, callback);
}
private NotificationLog(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class Punishment implements LiteFullJson {
private static final String COLLECTION_NAME = "punishment";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<Punishment>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Punishment::new, callback);
}
public static void findById(String id, Handler<AsyncResult<Punishment>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Punishment::new, callback);
}
private Punishment(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class ServerGroup implements LiteFullJson {
private static final String COLLECTION_NAME = "serverGroup";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<ServerGroup>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), ServerGroup::new, callback);
}
public static void findById(String id, Handler<AsyncResult<ServerGroup>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), ServerGroup::new, callback);
}
private ServerGroup(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,48 +0,0 @@
package net.frozenorb.apiv3.collections;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
@ToString
public final class User implements LiteFullJson {
private static final String COLLECTION_NAME = "user";
@Getter private String id;
public static void findAll(Handler<AsyncResult<Collection<User>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), User::new, callback);
}
public static void findById(String id, Handler<AsyncResult<User>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), User::new, callback);
}
private User(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,51 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.util.IPUtils;
import org.bson.Document;
import java.time.Instant;
import java.util.UUID;
@ToString
public final class AuditLogEntry implements LiteFullJson {
@Getter private String id;
@Getter private UUID performedBy;
@Getter private Instant performedAt;
@Getter private long performedFrom;
@Getter private String actionType;
@Getter private Document actionData;
public AuditLogEntry(Document json) {
this.id = json.getString("_id");
this.performedBy = UUID.fromString(json.getString("performedBy"));
this.performedAt = (Instant) json.get("performedAt");
this.performedFrom = json.getLong("performedFrom");
this.actionType = json.getString("actionType");
this.actionData = (Document) json.get("actionData");
}
public Document toLiteJson() {
Document json = new Document();
json.put("id", id);
json.put("performedBy", performedBy.toString());
json.put("performedAt", performedAt.toString());
json.put("performedFrom", IPUtils.machineToHuman(performedFrom));
json.put("actionType", actionType);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
json.put("actionData", actionData);
return json;
}
}

View File

@ -1,34 +1,20 @@
package net.frozenorb.apiv3.collections;
package net.frozenorb.apiv3.model;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.util.Collection;
import java.util.Map;
@ToString
public final class EmailTemplate implements LiteFullJson {
private static final String COLLECTION_NAME = "emailTemplate";
@Getter private String id;
@Getter private String title;
@Getter private String body;
public static void findAll(Handler<AsyncResult<Collection<EmailTemplate>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), EmailTemplate::new, callback);
}
public static void findById(String id, Handler<AsyncResult<EmailTemplate>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), EmailTemplate::new, callback);
}
private EmailTemplate(Document json) {
public EmailTemplate(Document json) {
this.id = json.getString("_id");
this.title = json.getString("title");
this.body = json.getString("body");

View File

@ -1,12 +1,9 @@
package net.frozenorb.apiv3.collections;
package net.frozenorb.apiv3.model;
import com.google.common.collect.ImmutableSet;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import org.bson.Document;
import java.time.Instant;
@ -17,8 +14,6 @@ import java.util.UUID;
@ToString
public final class Grant implements LiteFullJson {
private static final String COLLECTION_NAME = "grant";
@Getter private String id;
@Getter private UUID target;
@Getter private String reason;
@ -31,23 +26,7 @@ public final class Grant implements LiteFullJson {
@Getter private Instant removedAt;
@Getter private String removalReason;
public static void findAll(Handler<AsyncResult<Collection<Grant>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Grant::new, callback);
}
public static void findById(String id, Handler<AsyncResult<Grant>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback);
}
public static void findByTarget(UUID target, Handler<AsyncResult<Collection<Grant>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), Grant::new, callback);
}
public static void findByAddedBy(UUID addedBy, Handler<AsyncResult<Collection<Grant>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), Grant::new, callback);
}
private Grant(Document json) {
public Grant(Document json) {
this.id = json.getString("_id");
this.target = UUID.fromString(json.getString("target"));
this.reason = json.getString("reason");

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class IPBan implements LiteFullJson {
@Getter private String id;
public IPBan(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class IPLogEntry implements LiteFullJson {
@Getter private String id;
public IPLogEntry(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class MaxMindCacheEntry implements LiteFullJson {
@Getter private String id;
public MaxMindCacheEntry(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class NotificationLogEntry implements LiteFullJson {
@Getter private String id;
public NotificationLogEntry(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class Punishment implements LiteFullJson {
@Getter private String id;
public Punishment(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,23 +1,19 @@
package net.frozenorb.apiv3.collections;
package net.frozenorb.apiv3.model;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.LiteFullJson;
import net.frozenorb.apiv3.utils.MongoUtils;
import net.frozenorb.apiv3.accessor.Servers;
import org.bson.Document;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
@ToString(exclude={ "secret" })
@ToString
public final class Server implements LiteFullJson {
private static final String COLLECTION_NAME = "server";
@Getter private String id;
@Getter private String bungeeId;
@Getter private String displayName;
@ -28,42 +24,30 @@ public final class Server implements LiteFullJson {
@Getter private double lastTps;
@Getter private List<UUID> players;
public static void findAll(Handler<AsyncResult<Collection<Server>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Server::new, callback);
}
public static void findById(String id, Handler<AsyncResult<Server>> callback) {
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Server::new, callback);
}
public static void findByGroup(String groupId, Handler<AsyncResult<Collection<Server>>> callback) {
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("group", groupId), Server::new, callback);
}
private Server(Document json) {
public Server(Document json) {
this.id = json.getString("_id");
this.bungeeId = json.getString("bungeeId");
this.displayName = json.getString("displayName");
this.secret = json.getString("secret");
this.group = json.getString("group");
this.internalIp = json.getString("internalIp");
this.lastUpdate = json.get("lastUpdate", Instant.class);
this.lastTps = json.get("lastTps", Number.class).doubleValue();
this.lastUpdate = (Instant) json.get("lastUpdate");
this.lastTps = ((Number) json.get("lastTps")).doubleValue();
this.players = new ArrayList<>();
for (Object uuidString : json.get("players", Collection.class)) {
for (Object uuidString : (Collection) json.get("players")) {
players.add(UUID.fromString((String) uuidString));
}
}
public void update() {
APIv3.getMongo().getCollection(COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {});
APIv3.getMongo().getCollection(Servers.COLLECTION_NAME).updateOne(new Document("_id", id), new Document("$set", new Document("lastUpdate", new Date())), (result, error) -> {});
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
json.put("id", id);
json.put("bungeeId", bungeeId);
json.put("displayName", displayName);
json.put("group", group);

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class ServerGroup implements LiteFullJson {
@Getter private String id;
public ServerGroup(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -0,0 +1,33 @@
package net.frozenorb.apiv3.model;
import lombok.Getter;
import lombok.ToString;
import net.frozenorb.apiv3.LiteFullJson;
import org.bson.Document;
@ToString
public final class User implements LiteFullJson {
@Getter private String id;
public User(Document json) {
this.id = json.getString("_id");
}
public Document toLiteJson() {
Document json = new Document();
json.put("_id", id);
return json;
}
public Document toFullJson() {
Document json = toLiteJson();
return json;
}
}

View File

@ -1,4 +1,4 @@
package net.frozenorb.apiv3.utils;
package net.frozenorb.apiv3.util;
import lombok.experimental.UtilityClass;
import org.bson.Document;
@ -18,7 +18,11 @@ public class ErrorUtils {
return createResponse(throwable.getClass().getSimpleName(), identifier);
}
public static Document createResponse(String reason, String identifier) {
public static Document createResponse(String reason) {
return createResponse(reason, null);
}
private static Document createResponse(String reason, String identifier) {
Document json = new Document();
json.put("failed", true);

View File

@ -1,4 +1,4 @@
package net.frozenorb.apiv3.utils;
package net.frozenorb.apiv3.util;
import lombok.experimental.UtilityClass;

View File

@ -0,0 +1,40 @@
package net.frozenorb.apiv3.util;
import io.vertx.core.json.JsonArray;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.LiteFullJson;
import java.util.Collection;
@UtilityClass
public class JsonUtils {
public static String toLiteJsonString(Collection<? extends LiteFullJson> encode) {
JsonArray result = new JsonArray();
for (LiteFullJson entry : encode) {
result.add(entry.toLiteJson());
}
return result.encode();
}
public static String toLiteJsonString(LiteFullJson encode) {
return encode.toLiteJson().toJson();
}
public static String toFullJsonString(Collection<? extends LiteFullJson> encode) {
JsonArray result = new JsonArray();
for (LiteFullJson entry : encode) {
result.add(entry.toFullJson());
}
return result.encode();
}
public static String toFullJsonString(LiteFullJson encode) {
return encode.toFullJson().toJson();
}
}

View File

@ -0,0 +1,98 @@
package net.frozenorb.apiv3.util;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.async.SingleResultCallback;
import com.mongodb.async.client.FindIterable;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoDatabase;
import com.mongodb.connection.ClusterConnectionMode;
import com.mongodb.connection.ClusterSettings;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.mongoCodec.InstantCodec;
import org.bson.BsonType;
import org.bson.Document;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@UtilityClass
public class MongoUtils {
public static MongoDatabase initializeConnection(List<ServerAddress> hosts, String databaseName, String databaseUsername, char[] databasePassword) {
BsonTypeClassMap codecMap = new BsonTypeClassMap(ImmutableMap.of(
BsonType.DATE_TIME, Instant.class
));
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new InstantCodec()),
CodecRegistries.fromProviders(new DocumentCodecProvider(codecMap)),
CodecRegistries.fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider())
);
MongoClientSettings settings =
MongoClientSettings.builder()
.clusterSettings(
ClusterSettings.builder()
.mode(hosts.size() == 1 ? ClusterConnectionMode.SINGLE : ClusterConnectionMode.MULTIPLE)
.hosts(hosts)
.build()
)
.credentialList(ImmutableList.of(MongoCredential.createCredential(databaseUsername, databaseName, databasePassword)))
.codecRegistry(codecRegistry)
.build();
return MongoClients.create(settings).getDatabase(databaseName);
}
private static FindIterable<Document> createFindIterable(String collection, Document query, Document sort) {
return APIv3.getMongo().getCollection(collection).find(query).sort(sort);
}
public static <T> void findOneAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<T> callback) {
findOneAndTransform(collection, query, new Document(), transformation, callback);
}
public static <T> void findOneAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, SingleResultCallback<T> callback) {
createFindIterable(collection, query, sort).first((result, error) -> {
if (error != null) {
callback.onResult(null, error);
} else if (result != null) {
T transformed = transformation.apply(result);
callback.onResult(transformed, null);
} else {
callback.onResult(null, null);
}
});
}
public static <T> void findAndTransform(String collection, Document query, Function<Document, T> transformation, SingleResultCallback<Collection<T>> callback) {
findAndTransform(collection, query, new Document(), transformation, callback);
}
public static <T> void findAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, SingleResultCallback<Collection<T>> callback) {
createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> {
if (error != null) {
callback.onResult(null, error);
} else if (!result.isEmpty()) {
Collection<T> transformed = Collections2.transform(result, transformation);
callback.onResult(transformed, null);
} else {
callback.onResult(ImmutableList.of(), null);
}
});
}
}

View File

@ -1,53 +0,0 @@
package net.frozenorb.apiv3.utils;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.mongodb.async.client.FindIterable;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import lombok.experimental.UtilityClass;
import net.frozenorb.apiv3.APIv3;
import org.bson.Document;
import java.util.ArrayList;
import java.util.Collection;
@UtilityClass
public class MongoUtils {
private static FindIterable<Document> createFindIterable(String collection, Document query, Document sort) {
return APIv3.getMongo().getCollection(collection).find(query).sort(sort);
}
public static <T> void findOneAndTransform(String collection, Document query, Function<Document, T> transformation, Handler<AsyncResult<T>> callback) {
findOneAndTransform(collection, query, new Document(), transformation, callback);
}
public static <T> void findOneAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, Handler<AsyncResult<T>> callback) {
createFindIterable(collection, query, sort).first((result, error) -> {
if (error != null) {
callback.handle(Future.failedFuture(error));
} else {
T transformed = transformation.apply(result);
callback.handle(Future.succeededFuture(transformed));
}
});
}
public static <T> void findAndTransform(String collection, Document query, Function<Document, T> transformation, Handler<AsyncResult<Collection<T>>> callback) {
findAndTransform(collection, query, new Document(), transformation, callback);
}
public static <T> void findAndTransform(String collection, Document query, Document sort, Function<Document, T> transformation, Handler<AsyncResult<Collection<T>>> callback) {
createFindIterable(collection, query, sort).into(new ArrayList<>(), (result, error) -> {
if (error != null) {
callback.handle(Future.failedFuture(error));
} else {
Collection<T> transformed = Collections2.transform(result, transformation);
callback.handle(Future.succeededFuture(transformed));
}
});
}
}