001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.monitoring.reporting.web.template; 018 019 import org.apache.commons.monitoring.reporting.web.plugin.PluginRepository; 020 import org.apache.velocity.Template; 021 import org.apache.velocity.VelocityContext; 022 import org.apache.velocity.app.Velocity; 023 import org.apache.velocity.runtime.RuntimeConstants; 024 import org.apache.velocity.runtime.log.JdkLogChute; 025 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 026 027 import java.io.PrintWriter; 028 import java.util.Map; 029 import java.util.Properties; 030 031 public final class Templates { 032 private static String mapping; 033 034 public static void init(final String context, final String filterMapping) { 035 final Properties velocityConfiguration = new Properties(); 036 velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, JdkLogChute.class.getName()); 037 velocityConfiguration.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8"); 038 velocityConfiguration.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8"); 039 velocityConfiguration.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8"); 040 velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE.toString()); 041 velocityConfiguration.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, Boolean.TRUE.toString()); 042 velocityConfiguration.setProperty(RuntimeConstants.RESOURCE_LOADER, "monitoring"); 043 velocityConfiguration.setProperty(RuntimeConstants.VM_LIBRARY, "/templates/macro.vm"); 044 velocityConfiguration.setProperty("monitoring." + RuntimeConstants.RESOURCE_LOADER + ".class", ClasspathResourceLoader.class.getName()); 045 Velocity.init(velocityConfiguration); 046 047 if (filterMapping.isEmpty()) { 048 mapping = context; 049 } else { 050 mapping = context + filterMapping; 051 } 052 } 053 054 public static void htmlRender(final PrintWriter writer, final String template, final Map<String, ?> variables) { 055 final VelocityContext context = newVelocityContext(variables); 056 context.put("mapping", mapping); 057 context.put("currentTemplate", template); 058 context.put("plugins", PluginRepository.PLUGIN_INFO); 059 if (context.get("templateId") == null) { 060 context.put("templateId", template.replace(".vm", "")); 061 } 062 063 final Template velocityTemplate = Velocity.getTemplate("/templates/page.vm", "UTF-8"); 064 velocityTemplate.merge(context, writer); 065 } 066 067 public static void render(final PrintWriter writer, final String template, final Map<String, ?> variables) { 068 final VelocityContext context = newVelocityContext(variables); 069 context.put("mapping", mapping); 070 final Template velocityTemplate = Velocity.getTemplate(template, "UTF-8"); 071 velocityTemplate.merge(context, writer); 072 } 073 074 private static VelocityContext newVelocityContext(final Map<String, ?> variables) { 075 final VelocityContext context; 076 if (variables.isEmpty()) { 077 context = new VelocityContext(); 078 } else { 079 context = new VelocityContext(variables); 080 } 081 return context; 082 } 083 084 private Templates() { 085 // no-op 086 } 087 }