use of org.graylog2.contentpacks.model.entities.references.ValueType in project graylog2-server by Graylog2.
the class ReferenceConverter method convert.
@Override
public Reference convert(JsonNode jsonNode) {
if (jsonNode.isObject()) {
final ImmutableSet<String> fieldNames = ImmutableSet.copyOf(jsonNode.fieldNames());
if (fieldNames.equals(EXPECTED_FIELD_NAMES)) {
// TODO: Possible to use ValueTypeDeserializer to avoid duplication?
final String valueTypeText = jsonNode.path(ValueReference.FIELD_TYPE).asText();
final ValueType valueType = ValueType.valueOf(valueTypeText.toUpperCase(Locale.ROOT));
final JsonNode value = jsonNode.path(ValueReference.FIELD_VALUE);
if (valueType == ValueType.BOOLEAN && value.isBoolean()) {
return ValueReference.of(value.booleanValue());
} else if (valueType == ValueType.DOUBLE && value.isDouble()) {
return ValueReference.of(value.doubleValue());
} else if (valueType == ValueType.FLOAT && value.isFloat()) {
return ValueReference.of(value.floatValue());
} else if (valueType == ValueType.INTEGER && value.isInt()) {
return ValueReference.of(value.intValue());
} else if (valueType == ValueType.LONG && (value.isLong() || value.isInt())) {
// Jackson actually creates an int value for a small number so we check for both (long and int value) here
return ValueReference.of(value.longValue());
} else if (valueType == ValueType.STRING && value.isTextual()) {
return ValueReference.of(value.textValue());
} else if (valueType == ValueType.PARAMETER && value.isTextual()) {
return ValueReference.createParameter(value.textValue());
} else {
return null;
}
} else {
final ImmutableMap.Builder<String, Reference> map = ImmutableMap.builder();
final Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
final Map.Entry<String, JsonNode> entry = fields.next();
map.put(entry.getKey(), convert(entry.getValue()));
}
return new ReferenceMap(map.build());
}
} else if (jsonNode.isArray()) {
final ImmutableList.Builder<Reference> list = ImmutableList.builder();
for (JsonNode value : jsonNode) {
list.add(convert(value));
}
return new ReferenceList(list.build());
}
return null;
}
use of org.graylog2.contentpacks.model.entities.references.ValueType 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