use of org.apache.velocity.app.VelocityEngine in project lucene-solr by apache.
the class VelocityResponseWriter method write.
@Override
public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
// TODO: have HTTP headers available for configuring engine
VelocityEngine engine = createEngine(request);
Template template = getTemplate(engine, request);
VelocityContext context = createContext(request, response);
// for $engine.resourceExists(...)
context.put("engine", engine);
String layoutTemplate = request.getParams().get(LAYOUT);
boolean layoutEnabled = request.getParams().getBool(LAYOUT_ENABLED, true) && layoutTemplate != null;
String jsonWrapper = request.getParams().get(JSON);
boolean wrapResponse = layoutEnabled || jsonWrapper != null;
// create output
if (!wrapResponse) {
// straight-forward template/context merge to output
template.merge(context, writer);
} else {
// merge to a string buffer, then wrap with layout and finally as JSON
StringWriter stringWriter = new StringWriter();
template.merge(context, stringWriter);
if (layoutEnabled) {
context.put("content", stringWriter.toString());
stringWriter = new StringWriter();
try {
engine.getTemplate(layoutTemplate + TEMPLATE_EXTENSION).merge(context, stringWriter);
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
if (jsonWrapper != null) {
writer.write(jsonWrapper + "(");
writer.write(getJSONWrap(stringWriter.toString()));
writer.write(')');
} else {
// using a layout, but not JSON wrapping
writer.write(stringWriter.toString());
}
}
}
use of org.apache.velocity.app.VelocityEngine in project qi4j-sdk by Qi4j.
the class RestServerAssembler method assemble.
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream("/velocity.properties"));
VelocityEngine velocity = new VelocityEngine(props);
module.importedServices(VelocityEngine.class).importedBy(INSTANCE).setMetaInfo(velocity);
} catch (Exception e) {
throw new AssemblyException("Could not load velocity properties", e);
}
freemarker.template.Configuration cfg = new freemarker.template.Configuration();
cfg.setClassForTemplateLoading(AbstractResponseWriter.class, "");
cfg.setObjectWrapper(new ValueCompositeObjectWrapper());
module.importedServices(freemarker.template.Configuration.class).setMetaInfo(cfg);
module.importedServices(MetadataService.class);
module.importedServices(ResponseWriterDelegator.class).identifiedBy("responsewriterdelegator").importedBy(NEW_OBJECT).visibleIn(Visibility.layer);
module.objects(ResponseWriterDelegator.class);
module.importedServices(RequestReaderDelegator.class).identifiedBy("requestreaderdelegator").importedBy(NEW_OBJECT).visibleIn(Visibility.layer);
module.objects(RequestReaderDelegator.class);
module.importedServices(InteractionConstraintsService.class).importedBy(NewObjectImporter.class).visibleIn(Visibility.application);
module.objects(InteractionConstraintsService.class);
// Standard response writers
Iterable<Class<?>> writers = ClassScanner.findClasses(DefaultResponseWriter.class);
Specification<Class<?>> responseWriterClass = isAssignableFrom(ResponseWriter.class);
Specification<Class<?>> isNotAnAbstract = not(hasModifier(Modifier.ABSTRACT));
Iterable<Class<?>> candidates = filter(and(isNotAnAbstract, responseWriterClass), writers);
for (Class<?> responseWriter : candidates) {
module.objects(responseWriter);
}
// Standard request readers
module.objects(DefaultRequestReader.class);
}
use of org.apache.velocity.app.VelocityEngine in project tdi-studio-se by Talend.
the class TemplateProcessor method processTemplate.
public static boolean processTemplate(String logTag, Map<String, Object> contextParams, OutputStream outputStream, InputStream templateStream) throws IOException {
final Reader templateReader = new InputStreamReader(templateStream);
final Writer outputWriter = new OutputStreamWriter(outputStream);
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(TemplateProcessor.class.getClassLoader());
VelocityEngine engine = new VelocityEngine();
// engine.setProperty("resource.loader", "classpath"); //$NON-NLS-1$ //$NON-NLS-2$
// engine.setProperty("classpath.resource.loader.class", //$NON-NLS-1$
// "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); //$NON-NLS-1$
engine.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, EscapeXmlReference.class.getName());
engine.init();
VelocityContext context = new VelocityContext(contextParams);
return engine.evaluate(context, outputWriter, logTag, templateReader);
} catch (VelocityException ve) {
// org.apache.velocity.exception.MethodInvocationException;
throw new IOException(ve);
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
templateReader.close();
outputWriter.close();
}
}
use of org.apache.velocity.app.VelocityEngine in project cas by apereo.
the class CoreSamlConfiguration method velocityEngineFactoryBean.
@Lazy
@Bean(name = "shibboleth.VelocityEngine")
public VelocityEngine velocityEngineFactoryBean() {
final Properties properties = new Properties();
properties.put(RuntimeConstants.INPUT_ENCODING, StandardCharsets.UTF_8.name());
properties.put(RuntimeConstants.ENCODING_DEFAULT, StandardCharsets.UTF_8.name());
properties.put(RuntimeConstants.RESOURCE_LOADER, "file, classpath, string");
properties.put(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FileUtils.getTempDirectory().getAbsolutePath());
properties.put(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, Boolean.FALSE);
properties.put("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
properties.put("string.resource.loader.class", StringResourceLoader.class.getName());
properties.put("file.resource.loader.class", FileResourceLoader.class.getName());
return new VelocityEngine(properties);
}
use of org.apache.velocity.app.VelocityEngine in project cayenne by apache.
the class ClassGenerationAction method getTemplate.
protected Template getTemplate(TemplateType type) {
String templateName = customTemplateName(type);
if (templateName == null) {
templateName = defaultTemplateName(type);
}
// Velocity < 1.5 has some memory problems, so we will create a VelocityEngine every time,
// and store templates in an internal cache, to avoid uncontrolled memory leaks...
// Presumably 1.5 fixes it.
Template template = templateCache.get(templateName);
if (template == null) {
Properties props = new Properties();
props.put("resource.loader", "cayenne");
props.put("cayenne.resource.loader.class", ClassGeneratorResourceLoader.class.getName());
props.put("cayenne.resource.loader.cache", "false");
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init(props);
template = velocityEngine.getTemplate(templateName);
templateCache.put(templateName, template);
}
return template;
}
Aggregations