All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.bindings.providers.CommonMongoJackObjectMapperProvider Maven / Gradle / Ivy

There is a newer version: 6.1.4
Show newest version
/*
 * Copyright (C) 2020 Graylog, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Server Side Public License, version 1,
 * as published by MongoDB, Inc.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Server Side Public License for more details.
 *
 * You should have received a copy of the Server Side Public License
 * along with this program. If not, see
 * .
 */
package org.graylog2.bindings.providers;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import org.graylog.plugins.views.search.views.MongoIgnore;
import org.graylog2.jackson.MongoJodaDateTimeDeserializer;
import org.graylog2.jackson.MongoJodaDateTimeSerializer;
import org.graylog2.jackson.MongoZonedDateTimeDeserializer;
import org.graylog2.jackson.MongoZonedDateTimeSerializer;
import org.graylog2.security.encryption.EncryptedValueMapperConfig;
import org.joda.time.DateTime;

import javax.inject.Inject;
import javax.inject.Provider;
import java.time.ZonedDateTime;
import java.util.List;

public class CommonMongoJackObjectMapperProvider implements Provider {
    private final Provider objectMapperProvider;

    private static final BeanSerializerModifier customBeanSerializerModifier = new BeanSerializerModifier() {
        @Override
        public List changeProperties(SerializationConfig config, BeanDescription beanDesc,
                                                         List beanProperties) {
            beanProperties.removeIf(prop -> prop.getAnnotation(MongoIgnore.class) != null);
            return beanProperties;
        }
    };

    private static final com.fasterxml.jackson.databind.Module serializationModule = new SimpleModule("JSR-310-MongoJack")
            .addSerializer(ZonedDateTime.class, new MongoZonedDateTimeSerializer())
            .addDeserializer(ZonedDateTime.class, new MongoZonedDateTimeDeserializer())
            .addSerializer(DateTime.class, new MongoJodaDateTimeSerializer())
            .addDeserializer(DateTime.class, new MongoJodaDateTimeDeserializer())
            .setSerializerModifier(customBeanSerializerModifier);

    @Inject
    public CommonMongoJackObjectMapperProvider(Provider objectMapperProvider) {
        this.objectMapperProvider = objectMapperProvider;
    }

    @Override
    public ObjectMapper get() {
        return configure(this.objectMapperProvider.get());
    }

    public static ObjectMapper configure(ObjectMapper objectMapper) {
        var configuredObjectMapper = objectMapper.copy()
                .registerModule(serializationModule);

        EncryptedValueMapperConfig.enableDatabase(configuredObjectMapper);

        return configuredObjectMapper;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy