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();
}
Aggregations