Search in sources :

Example 1 with TransportConfigContext

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

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

the class TransportConfigContextTest method testTransportConfigContext.

@Test
public void testTransportConfigContext() throws Exception {
    API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
    api.setStatus(APIConstants.CREATED);
    api.setContextTemplate("/");
    api.setTransports(Constants.TRANSPORT_HTTP);
    ConfigContext configcontext = new APIConfigContext(api);
    TransportConfigContext transportConfigContext = new TransportConfigContext(configcontext, api);
    transportConfigContext.validate();
    Assert.assertTrue(Constants.TRANSPORT_HTTP.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
    api.setTransports(Constants.TRANSPORT_HTTP + "," + Constants.TRANSPORT_HTTPS);
    configcontext = new APIConfigContext(api);
    transportConfigContext = new TransportConfigContext(configcontext, api);
    Assert.assertTrue(StringUtils.EMPTY.equalsIgnoreCase(transportConfigContext.getContext().get("transport").toString()));
}
Also used : API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) TransportConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TransportConfigContext) APIConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APIConfigContext) TransportConfigContext(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TransportConfigContext) 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

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 TransportConfigContext (org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TransportConfigContext)1 RegistryException (org.wso2.carbon.registry.api.RegistryException)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1