Search in sources :

Example 1 with ParameterKey

use of io.gravitee.rest.api.model.annotations.ParameterKey in project gravitee-management-rest-api by gravitee-io.

the class ConfigServiceImpl method loadConfigByReference.

private void loadConfigByReference(Object[] objects, AbstractCommonSettingsEntity configEntity, String referenceId, ParameterReferenceType referenceType) {
    // get values from DB
    final List<Key> parameterKeys = new ArrayList<>();
    for (Object o : objects) {
        for (Field f : o.getClass().getDeclaredFields()) {
            ParameterKey parameterKey = f.getAnnotation(ParameterKey.class);
            if (parameterKey != null) {
                parameterKeys.add(parameterKey.value());
            }
        }
    }
    Map<String, List<String>> parameterMap = parameterService.findAll(parameterKeys, value -> value == null ? null : value.trim(), referenceId, referenceType);
    // set values
    for (Object o : objects) {
        for (Field f : o.getClass().getDeclaredFields()) {
            ParameterKey parameterKey = f.getAnnotation(ParameterKey.class);
            if (parameterKey != null) {
                boolean accessible = f.isAccessible();
                f.setAccessible(true);
                try {
                    List<String> values = parameterMap.get(parameterKey.value().key());
                    if (environment.containsProperty(parameterKey.value().key())) {
                        configEntity.getMetadata().add(PortalSettingsEntity.METADATA_READONLY, parameterKey.value().key());
                    }
                    final String defaultValue = parameterKey.value().defaultValue();
                    if (Enabled.class.isAssignableFrom(f.getType())) {
                        f.set(o, Boolean.parseBoolean(getFirstValueOrDefault(values, defaultValue)) ? new Enabled(true) : new Enabled(false));
                    } else if (Boolean.class.isAssignableFrom(f.getType())) {
                        f.set(o, Boolean.valueOf(getFirstValueOrDefault(values, defaultValue)));
                    } else if (Integer.class.isAssignableFrom(f.getType())) {
                        f.set(o, Integer.valueOf(getFirstValueOrDefault(values, defaultValue)));
                    } else if (Long.class.isAssignableFrom(f.getType())) {
                        f.set(o, Long.valueOf(getFirstValueOrDefault(values, defaultValue)));
                    } else if (List.class.isAssignableFrom(f.getType())) {
                        if (values == null || values.isEmpty()) {
                            if (StringUtils.isNotEmpty(defaultValue)) {
                                f.set(o, Arrays.asList(defaultValue.split(SEPARATOR)));
                            } else {
                                f.set(o, emptyList());
                            }
                        } else {
                            f.set(o, values);
                        }
                    } else if (Map.class.isAssignableFrom(f.getType())) {
                        if (values == null || values.isEmpty()) {
                            if (defaultValue == null) {
                                f.set(o, emptyMap());
                            } else {
                                f.set(o, singletonMap(defaultValue.split(KV_SEPARATOR)[0], defaultValue.split(KV_SEPARATOR)[1]));
                            }
                        } else {
                            f.set(o, values.stream().collect(toMap(v -> v.split(KV_SEPARATOR)[0], v -> {
                                final String[] split = v.split(KV_SEPARATOR);
                                if (split.length < 2) {
                                    return "";
                                }
                                return split[1];
                            })));
                        }
                    } else {
                        // If the parameter contains a sensitive info, we return dummy value
                        if (!parameterKey.sensitive()) {
                            f.set(o, getFirstValueOrDefault(values, defaultValue));
                        } else {
                            f.set(o, SENSITIVE_VALUE);
                        }
                    }
                } catch (IllegalAccessException e) {
                    LOGGER.error("Unable to set parameter {}. Use the default value", parameterKey.value().key(), e);
                }
                f.setAccessible(accessible);
            }
        }
    }
}
Also used : ConfigService(io.gravitee.rest.api.service.ConfigService) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) SEPARATOR(io.gravitee.rest.api.service.impl.ParameterServiceImpl.SEPARATOR) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ArrayList(java.util.ArrayList) NewsletterService(io.gravitee.rest.api.service.NewsletterService) Collectors.toMap(java.util.stream.Collectors.toMap) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) ParameterService(io.gravitee.rest.api.service.ParameterService) Logger(org.slf4j.Logger) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) io.gravitee.rest.api.model.settings(io.gravitee.rest.api.model.settings) Field(java.lang.reflect.Field) Key(io.gravitee.rest.api.model.parameters.Key) List(java.util.List) Component(org.springframework.stereotype.Component) ParameterKey(io.gravitee.rest.api.model.annotations.ParameterKey) KV_SEPARATOR(io.gravitee.rest.api.service.impl.ParameterServiceImpl.KV_SEPARATOR) Collections(java.util.Collections) ReCaptchaService(io.gravitee.rest.api.service.ReCaptchaService) ArrayList(java.util.ArrayList) ParameterKey(io.gravitee.rest.api.model.annotations.ParameterKey) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Key(io.gravitee.rest.api.model.parameters.Key) ParameterKey(io.gravitee.rest.api.model.annotations.ParameterKey)

Example 2 with ParameterKey

use of io.gravitee.rest.api.model.annotations.ParameterKey in project gravitee-management-rest-api by gravitee-io.

the class ConfigServiceImpl method saveConfigByReference.

private void saveConfigByReference(Object[] objects, String referenceId, ParameterReferenceType referenceType) {
    for (Object o : objects) {
        for (Field f : o.getClass().getDeclaredFields()) {
            ParameterKey parameterKey = f.getAnnotation(ParameterKey.class);
            if (parameterKey != null) {
                boolean accessible = f.isAccessible();
                f.setAccessible(true);
                try {
                    if (Enabled.class.isAssignableFrom(f.getType())) {
                        final String value = f.get(o) == null ? null : Boolean.toString(((Enabled) f.get(o)).isEnabled());
                        parameterService.save(parameterKey.value(), value, referenceId, referenceType);
                    } else if (Boolean.class.isAssignableFrom(f.getType())) {
                        final String value = f.get(o) == null ? null : Boolean.toString((Boolean) f.get(o));
                        parameterService.save(parameterKey.value(), value, referenceId, referenceType);
                    } else if (Integer.class.isAssignableFrom(f.getType())) {
                        final String value = f.get(o) == null ? null : Integer.toString((Integer) f.get(o));
                        parameterService.save(parameterKey.value(), value, referenceId, referenceType);
                    } else if (Long.class.isAssignableFrom(f.getType())) {
                        final String value = f.get(o) == null ? null : Long.toString((Long) f.get(o));
                        parameterService.save(parameterKey.value(), value, referenceId, referenceType);
                    } else if (List.class.isAssignableFrom(f.getType())) {
                        parameterService.save(parameterKey.value(), (List) f.get(o), referenceId, referenceType);
                    } else if (Map.class.isAssignableFrom(f.getType())) {
                        parameterService.save(parameterKey.value(), (Map) f.get(o), referenceId, referenceType);
                    } else {
                        final String value = (String) f.get(o);
                        if (!parameterKey.sensitive() || !SENSITIVE_VALUE.equals(value)) {
                            parameterService.save(parameterKey.value(), (String) f.get(o), referenceId, referenceType);
                        }
                    }
                } catch (IllegalAccessException e) {
                    LOGGER.error("Unable to set parameter {}. Use the default value", parameterKey.value().key(), e);
                }
                f.setAccessible(accessible);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ParameterKey(io.gravitee.rest.api.model.annotations.ParameterKey)

Aggregations

ParameterKey (io.gravitee.rest.api.model.annotations.ParameterKey)2 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Key (io.gravitee.rest.api.model.parameters.Key)1 ParameterReferenceType (io.gravitee.rest.api.model.parameters.ParameterReferenceType)1 io.gravitee.rest.api.model.settings (io.gravitee.rest.api.model.settings)1 ConfigService (io.gravitee.rest.api.service.ConfigService)1 NewsletterService (io.gravitee.rest.api.service.NewsletterService)1 ParameterService (io.gravitee.rest.api.service.ParameterService)1 ReCaptchaService (io.gravitee.rest.api.service.ReCaptchaService)1 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)1 KV_SEPARATOR (io.gravitee.rest.api.service.impl.ParameterServiceImpl.KV_SEPARATOR)1 SEPARATOR (io.gravitee.rest.api.service.impl.ParameterServiceImpl.SEPARATOR)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1