Search in sources :

Example 21 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.

/**
 * Generate policy for api level throttling
 *
 * @return throttle policies for api level
 * @throws APITemplateException throws if generation failure occur
 */
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy pipeline and generate the template
    Map<String, String> policyArray = new HashMap<String, String>();
    StringWriter writer;
    VelocityContext context;
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPI());
    // Generate template for pipeline conditions if pipelines not null
    if (apiPolicy.getPipelines() != null) {
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            // set values for velocity context
            context = new VelocityContext();
            setConstantContext(context);
            context.put(PIPELINE_ITEM, pipeline);
            context.put(POLICY, apiPolicy);
            context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
            context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
            String conditionString = getPolicyCondition(pipeline.getConditions());
            context.put(CONDITION, AND + conditionString);
            writer = new StringWriter();
            template.merge(context, writer);
            if (log.isDebugEnabled()) {
                log.debug("Generated Siddhi App : " + writer.toString());
            }
            String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
            policyArray.put(policyName, writer.toString());
        }
    }
    return policyArray;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 22 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.

@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) ConfigContext(org.wso2.carbon.apimgt.core.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext) ResourceConfigContext(org.wso2.carbon.apimgt.core.template.ResourceConfigContext) Template(org.apache.velocity.Template)

Example 23 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.

@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
    try {
        // build the context for template and apply the necessary decorators
        apiConfigContext.validate();
        CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
        VelocityContext context = configContext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
        velocityengine.init();
        Template template = velocityengine.getTemplate(templatePath);
        template.merge(context, writer);
    } catch (ResourceNotFoundException e) {
        log.error("Template " + templatePath + " not Found", e);
        throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
    } catch (ParseErrorException e) {
        log.error("Syntax error in " + templatePath, e);
        throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) APITemplateException(org.wso2.carbon.apimgt.core.template.APITemplateException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) CompositeAPIConfigContext(org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext) Template(org.apache.velocity.Template)

Example 24 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project tomee by apache.

the class Container method copyTemplateTo.

private void copyTemplateTo(final File targetDir, final String filename) throws Exception {
    final File file = new File(targetDir, filename);
    if (file.exists()) {
        return;
    }
    // don't break apps using Velocity facade
    final VelocityEngine engine = new VelocityEngine();
    engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
    engine.setProperty(Velocity.RESOURCE_LOADER, "class");
    engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
    engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
    engine.init();
    final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename);
    final VelocityContext context = new VelocityContext();
    context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
    context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
    final Writer writer = new FileWriter(file);
    template.merge(context, writer);
    writer.flush();
    writer.close();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) NullLogChute(org.apache.velocity.runtime.log.NullLogChute) VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) File(java.io.File) Writer(java.io.Writer) FileWriter(java.io.FileWriter) Template(org.apache.velocity.Template)

Example 25 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project gocd by gocd.

the class VelocityTemplateEngineFactory method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Properties properties = new Properties();
    // this is setup so that file takes preference over classpath, so that we can reload stuff in dev environment
    // and fallback to spring classloader in prod environment
    properties.setProperty(RESOURCE_LOADER, "file," + SpringResourceLoader.NAME);
    properties.setProperty(FILE_RESOURCE_LOADER_CACHE, "true");
    properties.setProperty(EVENTHANDLER_REFERENCEINSERTION, org.apache.velocity.app.event.implement.EscapeHtmlReference.class.getName());
    properties.setProperty(FILE_RESOURCE_LOADER_PATH, resourceBasePaths);
    properties.setProperty("file.resource.loader.modificationCheckInterval", String.valueOf(modificationCheckInterval));
    properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
    properties.setProperty(SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
    if (modificationCheckInterval != 0) {
        properties.setProperty(VM_LIBRARY_AUTORELOAD, "true");
    }
    properties.setProperty(VM_LIBRARY, "macros/urls.vm");
    this.velocityEngine = new VelocityEngine(properties);
    this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER, resourceLoader);
    this.velocityEngine.setApplicationAttribute(SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceBasePaths);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) SpringResourceLoader(org.springframework.ui.velocity.SpringResourceLoader) Properties(java.util.Properties)

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