Search in sources :

Example 1 with ResourceConfigContext

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();
}
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 ResourceConfigContext

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) UserStoreException(org.wso2.carbon.user.api.UserStoreException) RegistryException(org.wso2.carbon.registry.api.RegistryException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template) StringWriter(java.io.StringWriter) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException)

Example 3 with ResourceConfigContext

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));
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) VelocityContext(org.apache.velocity.VelocityContext) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) List(java.util.List) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Example 4 with ResourceConfigContext

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");
}
Also used : ArrayList(java.util.ArrayList) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 5 with ResourceConfigContext

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()));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ResourceConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)3 StringWriter (java.io.StringWriter)2 List (java.util.List)2 Template (org.apache.velocity.Template)2 VelocityEngine (org.apache.velocity.app.VelocityEngine)2 Test (org.junit.Test)2 APIConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext)2 ConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext)2 ResourceConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResourceConfigContext)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 ParseErrorException (org.apache.velocity.exception.ParseErrorException)1 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)1 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)1 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)1 Test (org.testng.annotations.Test)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)1