Search in sources :

Example 1 with EmptyDefaultValueException

use of org.graylog2.contentpacks.exceptions.EmptyDefaultValueException in project graylog2-server by Graylog2.

the class ContentPackService method validateParameters.

private ImmutableMap<String, ValueReference> validateParameters(Map<String, ValueReference> parameters, Set<Parameter> contentPackParameters) {
    final Set<String> contentPackParameterNames = contentPackParameters.stream().map(Parameter::name).collect(Collectors.toSet());
    checkUnknownParameters(parameters, contentPackParameterNames);
    checkMissingParameters(parameters, contentPackParameterNames);
    final Map<String, ValueType> contentPackParameterValueTypes = contentPackParameters.stream().collect(Collectors.toMap(Parameter::name, Parameter::valueType));
    final Set<String> invalidParameters = parameters.entrySet().stream().filter(entry -> entry.getValue().valueType() != contentPackParameterValueTypes.get(entry.getKey())).map(Map.Entry::getKey).collect(Collectors.toSet());
    if (!invalidParameters.isEmpty()) {
        throw new InvalidParametersException(invalidParameters);
    }
    final ImmutableMap.Builder<String, ValueReference> validatedParameters = ImmutableMap.builder();
    for (Parameter contentPackParameter : contentPackParameters) {
        final String name = contentPackParameter.name();
        final ValueReference providedParameter = parameters.get(name);
        if (providedParameter == null) {
            final Optional<?> defaultValue = contentPackParameter.defaultValue();
            final Object value = defaultValue.orElseThrow(() -> new EmptyDefaultValueException(name));
            final ValueReference valueReference = ValueReference.builder().valueType(contentPackParameter.valueType()).value(value).build();
            validatedParameters.put(name, valueReference);
        } else if (providedParameter.valueType() != contentPackParameter.valueType()) {
            throw new InvalidParameterTypeException(contentPackParameter.valueType(), providedParameter.valueType());
        } else {
            validatedParameters.put(name, providedParameter);
        }
    }
    return validatedParameters.build();
}
Also used : InvalidParameterTypeException(org.graylog2.contentpacks.exceptions.InvalidParameterTypeException) ValueType(org.graylog2.contentpacks.model.entities.references.ValueType) InvalidParametersException(org.graylog2.contentpacks.exceptions.InvalidParametersException) EmptyDefaultValueException(org.graylog2.contentpacks.exceptions.EmptyDefaultValueException) ImmutableMap(com.google.common.collect.ImmutableMap) Parameter(org.graylog2.contentpacks.model.parameters.Parameter) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ValueReference(org.graylog2.contentpacks.model.entities.references.ValueReference)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 EmptyDefaultValueException (org.graylog2.contentpacks.exceptions.EmptyDefaultValueException)1 InvalidParameterTypeException (org.graylog2.contentpacks.exceptions.InvalidParameterTypeException)1 InvalidParametersException (org.graylog2.contentpacks.exceptions.InvalidParametersException)1 ValueReference (org.graylog2.contentpacks.model.entities.references.ValueReference)1 ValueType (org.graylog2.contentpacks.model.entities.references.ValueType)1 Parameter (org.graylog2.contentpacks.model.parameters.Parameter)1