Search in sources :

Example 36 with VelocityEngine

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());
        }
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Example 37 with VelocityEngine

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);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ValueCompositeObjectWrapper(org.qi4j.library.rest.server.restlet.freemarker.ValueCompositeObjectWrapper) ResponseWriterDelegator(org.qi4j.library.rest.server.restlet.ResponseWriterDelegator) Properties(java.util.Properties) AssemblyException(org.qi4j.bootstrap.AssemblyException) AssemblyException(org.qi4j.bootstrap.AssemblyException) NewObjectImporter(org.qi4j.api.service.importer.NewObjectImporter) RequestReaderDelegator(org.qi4j.library.rest.server.restlet.RequestReaderDelegator)

Example 38 with VelocityEngine

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();
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) InputStreamReader(java.io.InputStreamReader) EscapeXmlReference(org.apache.velocity.app.event.implement.EscapeXmlReference) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 39 with VelocityEngine

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);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) FileResourceLoader(org.apache.velocity.runtime.resource.loader.FileResourceLoader) StringResourceLoader(org.apache.velocity.runtime.resource.loader.StringResourceLoader) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Properties(java.util.Properties) Lazy(org.springframework.context.annotation.Lazy) OpenSamlConfigBean(org.apereo.cas.support.saml.OpenSamlConfigBean) Bean(org.springframework.context.annotation.Bean)

Example 40 with VelocityEngine

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;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) Properties(java.util.Properties) Template(org.apache.velocity.Template)

Aggregations

VelocityEngine (org.apache.velocity.app.VelocityEngine)49 VelocityContext (org.apache.velocity.VelocityContext)23 StringWriter (java.io.StringWriter)21 Template (org.apache.velocity.Template)21 Properties (java.util.Properties)15 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)14 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)9 IOException (java.io.IOException)8 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 File (java.io.File)4 Writer (java.io.Writer)4 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HashMap (java.util.HashMap)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2