Search in sources :

Example 1 with RenderTemplateException

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";
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) RenderTemplateException(org.graylog.plugins.sidecar.template.RenderTemplateException) RenderTemplateException(org.graylog.plugins.sidecar.template.RenderTemplateException) BasicDBObject(com.mongodb.BasicDBObject) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 2 with RenderTemplateException

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;
}
Also used : Configuration(org.graylog.plugins.sidecar.rest.models.Configuration) RenderTemplateException(org.graylog.plugins.sidecar.template.RenderTemplateException) ValidationResult(org.graylog2.plugin.rest.ValidationResult)

Aggregations

RenderTemplateException (org.graylog.plugins.sidecar.template.RenderTemplateException)2 BasicDBObject (com.mongodb.BasicDBObject)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 Configuration (org.graylog.plugins.sidecar.rest.models.Configuration)1 ValidationResult (org.graylog2.plugin.rest.ValidationResult)1