Search in sources :

Example 1 with CoerceUtil

use of com.yahoo.elide.core.utils.coerce.CoerceUtil in project elide by yahoo.

the class ElideStandaloneSettings method getEntityDictionary.

/**
 * Gets the EntityDictionary for elide.
 * @param injector Service locator for web service for dependency injection.
 * @param dynamicConfiguration optional dynamic config object.
 * @param entitiesToExclude set of Entities to exclude from binding.
 * @return EntityDictionary object initialized.
 */
default EntityDictionary getEntityDictionary(ServiceLocator injector, ClassScanner scanner, Optional<DynamicConfiguration> dynamicConfiguration, Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (getAnalyticProperties().enableDynamicModelConfigAPI()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanNotCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanNotRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanNotDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    new HashMap<>(), // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            injector.inject(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return injector.create(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    dynamicConfiguration.map(DynamicConfiguration::getRoles).orElseGet(Collections::emptySet).forEach(role -> dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role)));
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) DynamicConfiguration(com.yahoo.elide.modelconfig.DynamicConfiguration) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 2 with CoerceUtil

use of com.yahoo.elide.core.utils.coerce.CoerceUtil in project elide by yahoo.

the class ElideAutoConfiguration method buildDictionary.

/**
 * Creates the entity dictionary for Elide which contains static metadata about Elide models.
 * Override to load check classes or life cycle hooks.
 * @param beanFactory Injector to inject Elide models.
 * @param dynamicConfig An instance of DynamicConfiguration.
 * @param settings Elide configuration settings.
 * @param entitiesToExclude set of Entities to exclude from binding.
 * @return a newly configured EntityDictionary.
 */
@Bean
@ConditionalOnMissingBean
@Scope(SCOPE_PROTOTYPE)
public EntityDictionary buildDictionary(AutowireCapableBeanFactory beanFactory, ClassScanner scanner, @Autowired(required = false) DynamicConfiguration dynamicConfig, ElideConfigProperties settings, @Qualifier("entitiesToExclude") Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (settings.getDynamicConfig().isConfigApiEnabled()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanNotCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanNotRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanNotDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    checks, // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            beanFactory.autowireBean(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return beanFactory.createBean(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    if (isAggregationStoreEnabled(settings) && isDynamicConfigEnabled(settings)) {
        dynamicConfig.getRoles().forEach(role -> {
            dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role));
        });
    }
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) Role(com.yahoo.elide.core.security.checks.prefab.Role) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Scope(org.springframework.context.annotation.Scope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with CoerceUtil

use of com.yahoo.elide.core.utils.coerce.CoerceUtil in project elide by yahoo.

the class ElideStandaloneConfigStoreTest method init.

@BeforeAll
public void init() throws Exception {
    configRoot = Files.createTempDirectory("test");
    settings = new ElideStandaloneTestSettings() {

        @Override
        public EntityDictionary getEntityDictionary(ServiceLocator injector, ClassScanner scanner, Optional<DynamicConfiguration> dynamicConfiguration, Set<Type<?>> entitiesToExclude) {
            Map<String, Class<? extends Check>> checks = new HashMap<>();
            if (getAnalyticProperties().enableDynamicModelConfigAPI()) {
                checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanCreate.class);
                checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanRead.class);
                checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanDelete.class);
                checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
            }
            EntityDictionary dictionary = new EntityDictionary(// Checks
            checks, // Role Checks
            new HashMap<>(), new Injector() {

                @Override
                public void inject(Object entity) {
                    injector.inject(entity);
                }

                @Override
                public <T> T instantiate(Class<T> cls) {
                    return injector.create(cls);
                }
            }, // Serde Lookup
            CoerceUtil::lookup, entitiesToExclude, scanner);
            dynamicConfiguration.map(DynamicConfiguration::getRoles).orElseGet(Collections::emptySet).forEach(role -> dictionary.addRoleCheck(role, new Role.RoleMemberCheck(role)));
            return dictionary;
        }

        @Override
        public ElideStandaloneAnalyticSettings getAnalyticProperties() {
            return new ElideStandaloneAnalyticSettings() {

                @Override
                public boolean enableDynamicModelConfig() {
                    return true;
                }

                @Override
                public boolean enableDynamicModelConfigAPI() {
                    return true;
                }

                @Override
                public String getDynamicConfigPath() {
                    return configRoot.toFile().getAbsolutePath();
                }

                @Override
                public boolean enableAggregationDataStore() {
                    return true;
                }

                @Override
                public boolean enableMetaDataStore() {
                    return true;
                }
            };
        }
    };
    elide = new ElideStandalone(settings);
    elide.start(false);
}
Also used : ElideStandalone(com.yahoo.elide.standalone.ElideStandalone) HttpStatus(com.yahoo.elide.core.exceptions.HttpStatus) DynamicConfiguration(com.yahoo.elide.modelconfig.DynamicConfiguration) JSONAPI_CONTENT_TYPE(com.yahoo.elide.Elide.JSONAPI_CONTENT_TYPE) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ElideStandaloneSettings(com.yahoo.elide.standalone.config.ElideStandaloneSettings) HashMap(java.util.HashMap) Role(com.yahoo.elide.core.security.checks.prefab.Role) JsonApiDSL.attr(com.yahoo.elide.test.jsonapi.JsonApiDSL.attr) AfterAll(org.junit.jupiter.api.AfterAll) MediaType(javax.ws.rs.core.MediaType) TestInstance(org.junit.jupiter.api.TestInstance) BeforeAll(org.junit.jupiter.api.BeforeAll) GraphQLDSL.mutation(com.yahoo.elide.test.graphql.GraphQLDSL.mutation) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) Injector(com.yahoo.elide.core.dictionary.Injector) Map(java.util.Map) RestAssured.when(io.restassured.RestAssured.when) GraphQLDSL.argument(com.yahoo.elide.test.graphql.GraphQLDSL.argument) JsonApiDSL.datum(com.yahoo.elide.test.jsonapi.JsonApiDSL.datum) Path(java.nio.file.Path) JsonApiDSL.type(com.yahoo.elide.test.jsonapi.JsonApiDSL.type) JsonApiDSL.links(com.yahoo.elide.test.jsonapi.JsonApiDSL.links) GraphQLDSL.selection(com.yahoo.elide.test.graphql.GraphQLDSL.selection) JsonApiDSL.attributes(com.yahoo.elide.test.jsonapi.JsonApiDSL.attributes) Check(com.yahoo.elide.core.security.checks.Check) Files(java.nio.file.Files) GraphQLDSL.selections(com.yahoo.elide.test.graphql.GraphQLDSL.selections) GraphQLDSL.field(com.yahoo.elide.test.graphql.GraphQLDSL.field) ElideStandaloneAnalyticSettings(com.yahoo.elide.standalone.config.ElideStandaloneAnalyticSettings) Set(java.util.Set) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) JsonApiDSL.resource(com.yahoo.elide.test.jsonapi.JsonApiDSL.resource) GraphQLDSL(com.yahoo.elide.test.graphql.GraphQLDSL) Test(org.junit.jupiter.api.Test) JsonApiDSL.id(com.yahoo.elide.test.jsonapi.JsonApiDSL.id) GraphQLDSL.arguments(com.yahoo.elide.test.graphql.GraphQLDSL.arguments) JsonApiDSL.data(com.yahoo.elide.test.jsonapi.JsonApiDSL.data) Type(com.yahoo.elide.core.type.Type) Optional(java.util.Optional) RestAssured.given(io.restassured.RestAssured.given) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Collections(java.util.Collections) HashMap(java.util.HashMap) ClassScanner(com.yahoo.elide.core.utils.ClassScanner) DynamicConfiguration(com.yahoo.elide.modelconfig.DynamicConfiguration) Injector(com.yahoo.elide.core.dictionary.Injector) Collections(java.util.Collections) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ElideStandaloneAnalyticSettings(com.yahoo.elide.standalone.config.ElideStandaloneAnalyticSettings) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Role(com.yahoo.elide.core.security.checks.prefab.Role) MediaType(javax.ws.rs.core.MediaType) Type(com.yahoo.elide.core.type.Type) ElideStandalone(com.yahoo.elide.standalone.ElideStandalone) HashMap(java.util.HashMap) Map(java.util.Map) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 4 with CoerceUtil

use of com.yahoo.elide.core.utils.coerce.CoerceUtil in project elide by yahoo.

the class ConfigStoreIntegrationTestSetup method buildDictionary.

@Bean
public EntityDictionary buildDictionary(AutowireCapableBeanFactory beanFactory, ClassScanner scanner, @Autowired(required = false) DynamicConfiguration dynamicConfig, ElideConfigProperties settings, @Qualifier("entitiesToExclude") Set<Type<?>> entitiesToExclude) {
    Map<String, Class<? extends Check>> checks = new HashMap<>();
    if (settings.getDynamicConfig().isConfigApiEnabled()) {
        checks.put(ConfigChecks.CAN_CREATE_CONFIG, ConfigChecks.CanCreate.class);
        checks.put(ConfigChecks.CAN_READ_CONFIG, ConfigChecks.CanRead.class);
        checks.put(ConfigChecks.CAN_DELETE_CONFIG, ConfigChecks.CanDelete.class);
        checks.put(ConfigChecks.CAN_UPDATE_CONFIG, ConfigChecks.CanNotUpdate.class);
    }
    EntityDictionary dictionary = new EntityDictionary(// Checks
    checks, // Role Checks
    new HashMap<>(), new Injector() {

        @Override
        public void inject(Object entity) {
            beanFactory.autowireBean(entity);
        }

        @Override
        public <T> T instantiate(Class<T> cls) {
            return beanFactory.createBean(cls);
        }
    }, // Serde Lookup
    CoerceUtil::lookup, entitiesToExclude, scanner);
    return dictionary;
}
Also used : HashMap(java.util.HashMap) Check(com.yahoo.elide.core.security.checks.Check) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Injector(com.yahoo.elide.core.dictionary.Injector) ConfigChecks(com.yahoo.elide.modelconfig.store.models.ConfigChecks) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Bean(org.springframework.context.annotation.Bean)

Example 5 with CoerceUtil

use of com.yahoo.elide.core.utils.coerce.CoerceUtil in project elide by yahoo.

the class PersistentResourceFetcherTest method initializeQueryRunner.

@BeforeAll
public void initializeQueryRunner() {
    RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
    hashMapDataStore = new HashMapDataStore(DefaultClassScanner.getInstance(), Author.class.getPackage());
    settings = new ElideSettingsBuilder(hashMapDataStore).withEntityDictionary(dictionary).withJoinFilterDialect(filterDialect).withSubqueryFilterDialect(filterDialect).withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC")).build();
    settings.getSerdes().forEach(CoerceUtil::register);
    initializeMocks();
    Elide elide = new Elide(settings);
    elide.doScans();
    runner = new QueryRunner(elide, NO_VERSION);
}
Also used : ElideSettingsBuilder(com.yahoo.elide.ElideSettingsBuilder) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) HashMapDataStore(com.yahoo.elide.core.datastore.inmemory.HashMapDataStore) Elide(com.yahoo.elide.Elide) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

CoerceUtil (com.yahoo.elide.core.utils.coerce.CoerceUtil)5 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)4 Injector (com.yahoo.elide.core.dictionary.Injector)4 Check (com.yahoo.elide.core.security.checks.Check)4 ConfigChecks (com.yahoo.elide.modelconfig.store.models.ConfigChecks)4 HashMap (java.util.HashMap)4 Role (com.yahoo.elide.core.security.checks.prefab.Role)2 DynamicConfiguration (com.yahoo.elide.modelconfig.DynamicConfiguration)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Bean (org.springframework.context.annotation.Bean)2 Elide (com.yahoo.elide.Elide)1 JSONAPI_CONTENT_TYPE (com.yahoo.elide.Elide.JSONAPI_CONTENT_TYPE)1 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)1 HashMapDataStore (com.yahoo.elide.core.datastore.inmemory.HashMapDataStore)1 HttpStatus (com.yahoo.elide.core.exceptions.HttpStatus)1 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)1 Type (com.yahoo.elide.core.type.Type)1 ClassScanner (com.yahoo.elide.core.utils.ClassScanner)1 ElideStandalone (com.yahoo.elide.standalone.ElideStandalone)1 ElideStandaloneAnalyticSettings (com.yahoo.elide.standalone.config.ElideStandaloneAnalyticSettings)1