public String get(MultivaluedMap<String, String> headers) { final Map<String, Object> model = ImmutableMap.<String, Object>builder() .put("title", "Graylog Web Interface") .put("cssFiles", cssFiles) .put("jsFiles", sortedJsFiles) .put("appPrefix", RestTools.buildExternalUri(headers, httpConfiguration.getHttpExternalUri())) .build(); return templateEngine.transform(template, model); } }
@Inject public FormatStringDecorator(@Assisted Decorator decorator, Engine templateEngine) { final String formatString = (String) requireNonNull(decorator.config().get(CK_FORMAT_STRING), CK_FORMAT_STRING + " cannot be null"); this.targetField = (String) requireNonNull(decorator.config().get(CK_TARGET_FIELD), CK_TARGET_FIELD + " cannot be null"); requireAllFields = (boolean) requireNonNull(decorator.config().get(CK_REQUIRE_ALL_FIELDS), CK_REQUIRE_ALL_FIELDS + " cannot be null"); template = requireNonNull(templateEngine, "templateEngine").getTemplate(formatString); usedVariables = template.getUsedVariableDescriptions(); }
private void bindSingletons() { bind(MongoConnection.class).toProvider(MongoConnectionProvider.class); if (configuration.isMessageJournalEnabled()) { install(new KafkaJournalModule()); install(new JournalReaderModule()); } else { install(new NoopJournalModule()); } bind(SystemJobManager.class).toProvider(SystemJobManagerProvider.class); bind(LdapConnector.class).in(Scopes.SINGLETON); bind(LdapUserAuthenticator.class).in(Scopes.SINGLETON); bind(DefaultSecurityManager.class).toProvider(DefaultSecurityManagerProvider.class).asEagerSingleton(); bind(SystemJobFactory.class).toProvider(SystemJobFactoryProvider.class); bind(GracefulShutdown.class).in(Scopes.SINGLETON); bind(ClusterStatsModule.class).asEagerSingleton(); bind(ClusterConfigService.class).to(ClusterConfigServiceImpl.class).asEagerSingleton(); bind(GrokPatternRegistry.class).in(Scopes.SINGLETON); bind(Engine.class).toInstance(Engine.createEngine()); bind(ErrorPageGenerator.class).to(GraylogErrorPageGenerator.class).asEagerSingleton(); registerRestControllerPackage("org.graylog2.rest.resources"); registerRestControllerPackage("org.graylog2.shared.rest.resources"); }
@Override public synchronized String transform(Map<String, Object> model, Locale locale, ModelAdaptor modelAdaptor, ProcessListener processListener) { try { context = new TemplateContext(template, locale, sourceName, new ScopedMap( model), modelAdaptor, engine, engine.getErrorHandler(), processListener); String transformed = transformPure(context); return transformed; } finally { context = null; } }
@Override public synchronized String transform(Map<String, Object> model, Locale locale, ModelAdaptor modelAdaptor, ProcessListener processListener) { TemplateContext context = new TemplateContext(template, locale, sourceName, new ScopedMap(model), modelAdaptor, engine, engine.getErrorHandler(), processListener); String transformed = transformCompiled(context); return transformed; }
@Override public Object getValue(TemplateContext context, Token arg1, List<String> arg2, String expression) { // First look in model map. Needed for dot-separated properties Object value = context.model.get(expression); if (value != null) { return value; } else { return super.getValue(context, arg1, arg2, expression); } }
public Object evaluate(TemplateContext context) { context.engine.getErrorHandler().error("invalid-expression", this); return ""; } }
public Object evaluate(TemplateContext context) { AnnotationProcessor<?> annotationProcessor = context .resolveAnnotationProcessor(receiver); if (annotationProcessor != null) { Object value = annotationProcessor.eval(this, context); if (value != null) { return value; } } return ""; } }
public static Engine createCompilingEngine() { Engine engine = new Engine(); engine.setUseCompilation(true); return engine; }
public static Engine createNonCachingEngine() { Engine engine = new Engine(); engine.setEnabledInterpretedTemplateCache(false); return engine; }
/** * Gets a template for a certain source. * * @param template * the template source * @return the prepared template */ public Template getTemplate(String template) { return getTemplate(template, null); }
@VisibleForTesting String buildBody(Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) { final String template; if (pluginConfig == null || pluginConfig.getString("body") == null) { template = bodyTemplate; } else { template = pluginConfig.getString("body"); } Map<String, Object> model = getModel(stream, checkResult, backlog); return this.templateEngine.transform(template, model); }
@Override public synchronized String transform(Map<String, Object> model, Locale locale, ModelAdaptor modelAdaptor, ProcessListener processListener) { try { context = new TemplateContext(template, locale, sourceName, new ScopedMap( model), modelAdaptor, engine, engine.getErrorHandler(), processListener); String transformed = transformPure(context); return transformed; } finally { context = null; output = null; } }
public Object evaluate(TemplateContext context) { context.engine.getErrorHandler().error(ErrorMessage.INVALID_EXPRESSION, this); return ""; } }
public Object evaluate(TemplateContext context) { AnnotationProcessor<?> annotationProcessor = context .resolveAnnotationProcessor(receiver); if (annotationProcessor != null) { Object value = annotationProcessor.eval(this, context); if (value != null) { return value; } } return ""; } }
@VisibleForTesting String buildSubject(Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) { final String template; if (pluginConfig == null || pluginConfig.getString("subject") == null) { template = "Graylog alert for stream: ${stream.title}: ${check_result.resultDescription}"; } else { template = pluginConfig.getString("subject"); } Map<String, Object> model = getModel(stream, checkResult, backlog); return templateEngine.transform(template, model); }
@Override public String generate(Request request, int status, String reasonPhrase, String description, Throwable exception) { final ImmutableMap.Builder<String, Object> modelBuilder = ImmutableMap.builder(); modelBuilder.put("reason", StringEscapeUtils.escapeHtml4(reasonPhrase)); if (description != null) { modelBuilder.put("description", StringEscapeUtils.escapeHtml4(description)); } if (exception != null) { String stacktrace = ""; try (final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter)) { exception.printStackTrace(printWriter); printWriter.flush(); stringWriter.flush(); stacktrace = stringWriter.toString(); } catch (IOException ignored) { // Ignore } modelBuilder .put("exception", StringEscapeUtils.escapeHtml4(exception.getMessage())) .put("stacktrace", StringEscapeUtils.escapeHtml4(stacktrace)); } return engine.transform(template, modelBuilder.build()); } }
@GET @Produces(MediaType.TEXT_HTML) @Path("index.html") public String index(@Context HttpHeaders httpHeaders) throws IOException { final URL templateUrl = this.getClass().getResource("/swagger/index.html.template"); final String template = Resources.toString(templateUrl, StandardCharsets.UTF_8); final Map<String, Object> model = ImmutableMap.of( "baseUri", RestTools.buildExternalUri(httpHeaders.getRequestHeaders(), httpConfiguration.getHttpExternalUri()).resolve(HttpConfiguration.PATH_API).toString()); return templateEngine.transform(template, model); }
@GET @Produces(MoreMediaTypes.APPLICATION_JAVASCRIPT) public String get(@Context HttpHeaders headers) { final URL templateUrl = this.getClass().getResource("/web-interface/config.js.template"); final String template; try { template = Resources.toString(templateUrl, StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException("Unable to read AppConfig template while generating web interface configuration: ", e); } final URI baseUri = RestTools.buildExternalUri(headers.getRequestHeaders(), httpConfiguration.getHttpExternalUri()); final Map<String, Object> model = ImmutableMap.of( "rootTimeZone", configuration.getRootTimeZone(), "serverUri", baseUri.resolve(HttpConfiguration.PATH_API), "appPathPrefix", baseUri.getPath()); return templateEngine.transform(template, model); } }
final String urlString = templateEngine.transform(config.url(), ImmutableMap.of("key", encodedKey)); final HttpUrl url = HttpUrl.parse(urlString);