use of org.wso2.carbon.apimgt.core.template.ResourceConfigContext 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();
}
use of org.wso2.carbon.apimgt.core.template.ResourceConfigContext in project carbon-apimgt by wso2.
the class APIConfigContextTestCase method testResourceConfigContext.
@Test
public void testResourceConfigContext() {
APIConfigContext apiConfigContext = new APIConfigContext(SampleTestObjectCreator.createDefaultAPI().build(), "org.test");
TemplateBuilderDTO templateBuilderDTO = new TemplateBuilderDTO();
templateBuilderDTO.setTemplateId("t1");
List<TemplateBuilderDTO> templateList = new ArrayList<TemplateBuilderDTO>();
templateList.add(templateBuilderDTO);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(apiConfigContext, templateList);
List<TemplateBuilderDTO> templatesFromContext = (List<TemplateBuilderDTO>) resourceConfigContext.getContext().get("apiResources");
String templateIdFromContext = templatesFromContext.get(0).getTemplateId();
Assert.assertEquals(templateIdFromContext, "t1");
}
Aggregations