Search in sources :

Example 1 with ResponseCacheConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResponseCacheConfigContext 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 2 with ResponseCacheConfigContext

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResponseCacheConfigContext in project carbon-apimgt by wso2.

the class ResponseCacheConfigContextTest method testResponseCacheConfigContext.

@Test
public void testResponseCacheConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setResponseCache(APIConstants.DISABLED);
    ConfigContext configcontext = new APIConfigContext(api);
    ResponseCacheConfigContext responseCacheConfigContext = new ResponseCacheConfigContext(configcontext, api);
    Assert.assertFalse((Boolean) responseCacheConfigContext.getContext().get("responseCacheEnabled"));
    api.setResponseCache(APIConstants.ENABLED);
    api.setCacheTimeout(100);
    responseCacheConfigContext = new ResponseCacheConfigContext(configcontext, api);
    Assert.assertTrue((Boolean) responseCacheConfigContext.getContext().get("responseCacheEnabled"));
    Assert.assertEquals(100, responseCacheConfigContext.getContext().get("responseCacheTimeOut"));
}
Also used : ResponseCacheConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResponseCacheConfigContext) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) ConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext) ResponseCacheConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResponseCacheConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1 Test (org.junit.Test)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)1 APIConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext)1 ConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ConfigContext)1 ResponseCacheConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.ResponseCacheConfigContext)1 RegistryException (org.wso2.carbon.registry.api.RegistryException)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1