Cleanup some small bits of code

This commit is contained in:
Colin McDonald 2016-06-16 10:49:57 -04:00
parent 78762262c7
commit 3a1b68b6b6
6 changed files with 49 additions and 39 deletions

View File

@ -12,6 +12,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.ConnectionString;
import com.mongodb.MongoCredential;
import com.mongodb.async.client.MongoClient;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoDatabase;
@ -74,6 +75,7 @@ import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import java.io.FileInputStream;
import java.io.IOException;
@ -131,41 +133,20 @@ public final class APIv3 extends AbstractVerticle {
);
}
ClusterSettings clusterSettings = ClusterSettings
ConnectionString connectionString = new ConnectionString("mongodb://" + config.getProperty("mongo.address") + ":" + config.getProperty("mongo.port"));
MongoClient mongoClient = MongoClients.create(MongoClientSettings
.builder()
.applyConnectionString(new ConnectionString("mongodb://" + config.getProperty("mongo.address") + ":" + config.getProperty("mongo.port")))
.build();
List<CodecProvider> providers = new ArrayList<>();
// Our fixed uuid codec
providers.add(new UUIDCodecProvider());
// Normal providers
providers.add(new ValueCodecProvider());
providers.add(new DocumentCodecProvider());
providers.add(new BsonValueCodecProvider());
ObjectMapper objectMapper = ObjectMapperFactory.createObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(UUID.class, new UUIDJsonSerializer());
simpleModule.addDeserializer(UUID.class, new UUIDJsonDeserializer());
objectMapper.registerModule(simpleModule);
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
providers.add(new JacksonCodecProvider(objectMapper));
MongoClientSettings settings = MongoClientSettings
.builder()
.codecRegistry(CodecRegistries.fromProviders(providers))
.codecRegistry(createCodecRegistry())
.credentialList(credentials)
.clusterSettings(clusterSettings)
.build();
.clusterSettings(ClusterSettings.builder()
.applyConnectionString(connectionString)
.build()
)
.build()
);
database = MongoClients.create(settings).getDatabase(config.getProperty("mongo.database"));
database = mongoClient.getDatabase(config.getProperty("mongo.database"));
database.getCollection("auditLog").createIndexes(ImmutableList.of(
new IndexModel(new Document("user", 1)),
new IndexModel(new Document("performedAt", 1)),
@ -195,6 +176,34 @@ public final class APIv3 extends AbstractVerticle {
), (a, b) -> {});
}
private CodecRegistry createCodecRegistry() {
ObjectMapper objectMapper = ObjectMapperFactory.createObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(UUID.class, new UUIDJsonSerializer());
simpleModule.addDeserializer(UUID.class, new UUIDJsonDeserializer());
objectMapper.registerModule(simpleModule);
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
List<CodecProvider> providers = new ArrayList<>();
// Our fixed uuid codec
providers.add(new UUIDCodecProvider());
// Normal providers
providers.add(new ValueCodecProvider());
providers.add(new DocumentCodecProvider());
providers.add(new BsonValueCodecProvider());
// Jackson parser codec
providers.add(new JacksonCodecProvider(objectMapper));
return CodecRegistries.fromProviders(providers);
}
private void setupRedis() {
redisClient = RedisClient.create(
vertx,

View File

@ -111,6 +111,9 @@ public final class ActorAttributeHandler implements Handler<RoutingContext> {
ErrorUtils.respondGeneric(ctx, 401, "Failed to authorize as bungee.");
}
break;
default:
ErrorUtils.respondGeneric(ctx, 401, "Failed to authorize as " + type + ".");
break;
}
}

View File

@ -25,7 +25,7 @@ public final class POSTServerHeartbeat implements Handler<RoutingContext> {
Actor actor = ctx.get("actor");
if (actor.getType() != ActorType.SERVER) {
ErrorUtils.respondServerOnly(ctx);
ErrorUtils.respondGeneric(ctx, 400, "This action can only be performed when requested by a server.");
return;
}

View File

@ -11,7 +11,7 @@ public final class DateTypeAdapter extends TypeAdapter<Date> {
public void write(JsonWriter writer, Date write) throws IOException {
if (write == null) {
writer.value(-1);
writer.nullValue();
} else {
writer.value(write.getTime());
}

View File

@ -28,7 +28,9 @@ public final class Notification {
messageJson.put("from_email", "no-reply@minehq.com");
messageJson.put("from_name", "MineHQ Network");
messageJson.put("to", ImmutableList.of(
new Document("email", email).append("name", null).append("type", "to")
new Document("email", email)
.append("name", null)
.append("type", "to")
));
Document bodyJson = new Document();

View File

@ -8,10 +8,6 @@ import net.frozenorb.apiv3.APIv3;
@UtilityClass
public class ErrorUtils {
public static void respondServerOnly(RoutingContext ctx) {
respondGeneric(ctx, 400, "This action can only be performed when requested by a server.");
}
public static void respondNotFound(RoutingContext ctx, String itemType, String id) {
respondGeneric(ctx, 404, "Not found: " + itemType + " with id " + id + " cannot be found.");
}