use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.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.rest.api.publisher.v1.common.template.ResourceConfigContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForPrototypeScriptAPI.
@Override
public String getConfigStringForPrototypeScriptAPI(Environment environment) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new TransportConfigContext(configcontext, api);
configcontext = new ResourceConfigContext(configcontext, api);
configcontext = new EndpointBckConfigContext(configcontext, api);
configcontext = new EndpointConfigContext(configcontext, api);
configcontext = new SecurityConfigContext(configcontext, api);
configcontext = new JwtConfigContext(configcontext);
configcontext = new ResponseCacheConfigContext(configcontext, api);
configcontext = new HandlerConfigContex(configcontext, handlers);
configcontext = new EnvironmentConfigContext(configcontext, environment);
configcontext = new TemplateUtilContext(configcontext);
// @todo: this validation might be better to do when the builder is initialized.
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
/* first, initialize velocity engine */
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
Template t = velocityengine.getTemplate(this.getPrototypeTemplatePath());
t.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPIProduct.
@Test
public void testResourceConfigContextForAPIProduct() throws Exception {
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier("admin", "TestAPIProduct", "1.0.0", UUID.randomUUID().toString());
APIProduct apiProduct = new APIProduct(apiProductIdentifier);
apiProduct.setProductResources(getAPIProductResources(apiProductIdentifier));
apiProduct.setType(APIConstants.API_PRODUCT);
ConfigContext configcontext = new APIConfigContext(apiProduct);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, apiProduct);
resourceConfigContext.validate();
VelocityContext context = resourceConfigContext.getContext();
Assert.assertNotNull(context.get("apiType"));
Assert.assertEquals(context.get("apiType"), APIConstants.API_PRODUCT);
Object aggregates = context.get("aggregates");
Assert.assertNotNull(aggregates);
Assert.assertTrue(aggregates instanceof List);
List<APIProductResource> apiProductResources = (List<APIProductResource>) aggregates;
Assert.assertTrue(assertAPIProductResourceList(apiProduct.getProductResources(), apiProductResources));
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.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");
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method testResourceConfigContextForAPI.
@Test
public void testResourceConfigContextForAPI() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
api.setUriTemplates(setAPIUriTemplates());
ConfigContext configcontext = new APIConfigContext(api);
ResourceConfigContext resourceConfigContext = new ResourceConfigContext(configcontext, api);
resourceConfigContext.validate();
Assert.assertNotNull(resourceConfigContext.getContext().get("resources"));
// assign an empty URITemplate set and check the result
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
api.setUriTemplates(uriTemplates);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
String errorClass = "org.wso2.carbon.apimgt.api.APIManagementException";
String expectedErrorMessage = "At least one resource is required";
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
// set a null value for URITemplate and check the result
api.setUriTemplates(null);
configcontext = new APIConfigContext(api);
resourceConfigContext = new ResourceConfigContext(configcontext, api);
try {
resourceConfigContext.validate();
} catch (APIManagementException e) {
Assert.assertTrue(errorClass.equalsIgnoreCase(e.getClass().getName()));
Assert.assertTrue(expectedErrorMessage.equalsIgnoreCase(e.getMessage()));
}
}
Aggregations