use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.
the class HTTPAlarmCallback method getRequestedConfiguration.
@Override
public ConfigurationRequest getRequestedConfiguration() {
final ConfigurationRequest configurationRequest = new ConfigurationRequest();
configurationRequest.addField(new TextField(CK_URL, "URL", "https://example.org/alerts", "The URL to POST to when an alert is triggered", ConfigurationField.Optional.NOT_OPTIONAL));
return configurationRequest;
}
use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.
the class ConfigurationMapConverter method convertValues.
/**
* Converts the values in the map to the requested types. This has been copied from the Graylog web interface
* and should be removed once we have better configuration objects.
*/
public static Map<String, Object> convertValues(final Map<String, Object> data, final ConfigurationRequest configurationRequest) throws ValidationException {
final Map<String, Object> configuration = Maps.newHashMapWithExpectedSize(data.size());
final Map<String, Map<String, Object>> configurationFields = configurationRequest.asList();
for (final Map.Entry<String, Object> entry : data.entrySet()) {
final String field = entry.getKey();
final Map<String, Object> fieldDescription = configurationFields.get(field);
if (fieldDescription == null || fieldDescription.isEmpty()) {
throw new ValidationException(field, "Unknown configuration field description for field \"" + field + "\"");
}
final String type = (String) fieldDescription.get("type");
// Decide what to cast to. (string, bool, number)
Object value;
switch(type) {
case "text":
case "dropdown":
value = entry.getValue() == null ? "" : String.valueOf(entry.getValue());
break;
case "number":
try {
value = Integer.parseInt(String.valueOf(entry.getValue()));
} catch (NumberFormatException e) {
// If a numeric field is optional and not provided, use null as value
if ("true".equals(String.valueOf(fieldDescription.get("is_optional")))) {
value = null;
} else {
throw new ValidationException(field, e.getMessage());
}
}
break;
case "boolean":
value = "true".equalsIgnoreCase(String.valueOf(entry.getValue()));
break;
case "list":
final List<?> valueList = entry.getValue() == null ? Collections.emptyList() : (List<?>) entry.getValue();
value = valueList.stream().filter(o -> o != null && o instanceof String).map(String::valueOf).collect(Collectors.toList());
break;
default:
throw new ValidationException(field, "Unknown configuration field type \"" + type + "\"");
}
configuration.put(field, value);
}
return configuration;
}
use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.
the class ConfigurationMapConverterTest method testConvertValues.
@Test
public void testConvertValues() throws Exception {
final ImmutableMap<String, String> dropdownChoices = ImmutableMap.of("a", "1", "b", "2");
final ConfigurationRequest cr = new ConfigurationRequest();
cr.addField(new TextField("string", "string", "default", ""));
cr.addField(new TextField("empty-string", "empty", "", ""));
cr.addField(new TextField("null-string", "null", null, ""));
cr.addField(new TextField("non-string", "non-string", null, ""));
cr.addField(new NumberField("number", "number", 42, ""));
cr.addField(new BooleanField("boolean-true", "true", false, ""));
cr.addField(new BooleanField("boolean-false", "false", false, ""));
cr.addField(new DropdownField("dropdown", "dropdown", "a", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
cr.addField(new DropdownField("dropdown-empty", "dropdown-empty", "", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
cr.addField(new DropdownField("dropdown-null", "dropdown-null", "", dropdownChoices, "", ConfigurationField.Optional.NOT_OPTIONAL));
final UUID uuid = UUID.randomUUID();
final Map<String, Object> data = new HashMap<>();
data.put("string", "foo");
data.put("empty-string", "");
data.put("null-string", null);
data.put("non-string", uuid);
data.put("number", "5");
data.put("boolean-true", "true");
data.put("boolean-false", "false");
data.put("dropdown", "a");
data.put("dropdown-empty", "");
data.put("dropdown-null", null);
final Map<String, Object> config = ConfigurationMapConverter.convertValues(data, cr);
assertThat(config).contains(entry("string", "foo"), entry("empty-string", ""), entry("null-string", ""), entry("non-string", uuid.toString()), entry("number", 5), entry("boolean-true", true), entry("boolean-false", false), entry("dropdown", "a"), entry("dropdown-empty", ""), entry("dropdown-null", ""));
}
use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.
the class ConfigurationMapConverterTest method convertValuesThrowsIllegalArgumentExceptionOnUnknwonType.
@Test
public void convertValuesThrowsIllegalArgumentExceptionOnUnknwonType() throws Exception {
thrown.expect(ValidationException.class);
thrown.expectMessage("Unknown configuration field type \"dummy\"");
final ConfigurationRequest cr = new ConfigurationRequest();
cr.addField(new DummyField());
final Map<String, Object> data = new HashMap<>();
data.put("dummy", "foo");
ConfigurationMapConverter.convertValues(data, cr);
}
use of org.graylog2.plugin.configuration.ConfigurationRequest in project graylog2-server by Graylog2.
the class MessageInput method checkConfiguration.
public void checkConfiguration() throws ConfigurationException {
final ConfigurationRequest cr = getRequestedConfiguration();
cr.check(getConfiguration());
}
Aggregations