use of org.graylog.plugins.sidecar.template.RenderTemplateException in project graylog2-server by Graylog2.
the class ConfigurationService method renderTemplate.
private String renderTemplate(String configurationId, Map<String, Object> sidecarContext) throws RenderTemplateException {
Writer writer = new StringWriter();
String template;
final Map<String, Object> context = new HashMap<>();
context.put("sidecar", sidecarContext);
final Map<String, Object> userContext = configurationVariableService.all().stream().collect(Collectors.toMap(ConfigurationVariable::name, ConfigurationVariable::content));
context.put(ConfigurationVariable.VARIABLE_PREFIX, userContext);
try {
Template compiledTemplate = templateConfiguration.getTemplate(configurationId);
compiledTemplate.process(context, writer);
} catch (TemplateException e) {
LOG.error("Failed to render template: " + e.getMessageWithoutStackTop());
throw new RenderTemplateException(e.getFTLInstructionStack(), e);
} catch (IOException e) {
LOG.error("Failed to render template: ", e);
throw new RenderTemplateException(e.getMessage(), e);
}
template = writer.toString();
return template.endsWith("\n") ? template : template + "\n";
}
use of org.graylog.plugins.sidecar.template.RenderTemplateException in project graylog2-server by Graylog2.
the class ConfigurationResource method validate.
private ValidationResult validate(Configuration toValidate) {
final Optional<Configuration> configurationOptional;
final Configuration configuration;
final ValidationResult validation = new ValidationResult();
configurationOptional = Optional.ofNullable(configurationService.findByName(toValidate.name()));
if (configurationOptional.isPresent()) {
configuration = configurationOptional.get();
if (!configuration.id().equals(toValidate.id())) {
// a configuration exists with a different id, so the name is already in use, fail validation
validation.addError("name", "Configuration \"" + toValidate.name() + "\" already exists");
}
}
if (toValidate.name().isEmpty()) {
validation.addError("name", "Configuration name cannot be empty.");
} else if (!VALID_NAME_PATTERN.matcher(toValidate.name()).matches()) {
validation.addError("name", "Configuration name can not include the following characters: ; * ? \" < > | &");
}
if (toValidate.collectorId().isEmpty()) {
validation.addError("collector_id", "Associated collector ID cannot be empty.");
}
if (toValidate.color().isEmpty()) {
validation.addError("color", "Collector color cannot be empty.");
}
if (toValidate.template().isEmpty()) {
validation.addError("template", "Collector template cannot be empty.");
}
try {
this.configurationService.renderPreview(toValidate.template());
} catch (RenderTemplateException e) {
validation.addError("template", "Template error: " + e.getMessage());
}
return validation;
}
Aggregations