Search in sources :

Example 1 with ConfigContext

use of org.wso2.carbon.apimgt.core.template.ConfigContext 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 2 with ConfigContext

use of org.wso2.carbon.apimgt.core.template.ConfigContext 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 3 with ConfigContext

use of org.wso2.carbon.apimgt.core.template.ConfigContext in project carbon-apimgt by wso2.

the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.

@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
    StringWriter writer = new StringWriter();
    String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = new EndpointContext(endpoint, packageName);
        VelocityContext context = configcontext.getContext();
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        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) StringWriter(java.io.StringWriter) EndpointContext(org.wso2.carbon.apimgt.core.template.EndpointContext) 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) 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)

Aggregations

StringWriter (java.io.StringWriter)3 Template (org.apache.velocity.Template)3 VelocityContext (org.apache.velocity.VelocityContext)3 VelocityEngine (org.apache.velocity.app.VelocityEngine)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)3 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)3 CompositeAPIConfigContext (org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext)3 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)2 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)2 ConfigContext (org.wso2.carbon.apimgt.core.template.ConfigContext)2 ResourceConfigContext (org.wso2.carbon.apimgt.core.template.ResourceConfigContext)2 EndpointContext (org.wso2.carbon.apimgt.core.template.EndpointContext)1