use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TemplateUtilContext 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.TemplateUtilContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForEndpointTemplate.
/**
* Sets the necessary variables to velocity context.
*
* @param endpointType Type of the endpoint : production or sandbox
* @return The string of endpoint file content
* @throws APITemplateException Thrown if an error occurred
*/
@Override
public String getConfigStringForEndpointTemplate(String endpointType) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new EndpointConfigContext(configcontext, this.apiProduct, api);
configcontext = new TemplateUtilContext(configcontext);
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
context.put("type", endpointType);
Template template = velocityengine.getTemplate(this.getEndpointTemplatePath());
template.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error");
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TemplateUtilContext in project carbon-apimgt by wso2.
the class APITemplateBuilderImpl method getConfigStringForWebSocketEndpointTemplate.
@Override
public String getConfigStringForWebSocketEndpointTemplate(String endpointType, String resourceKey, String endpointUrl) throws APITemplateException {
StringWriter writer = new StringWriter();
try {
ConfigContext configcontext = new APIConfigContext(this.api);
configcontext = new EndpointBckConfigContext(configcontext, api);
configcontext = new EndpointConfigContext(configcontext, api);
configcontext = new TemplateUtilContext(configcontext);
configcontext.validate();
VelocityContext context = configcontext.getContext();
context.internalGetKeys();
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
initVelocityEngine(velocityengine);
context.put("type", endpointType + "_endpoints");
context.put("websocketResourceKey", resourceKey);
context.put("endpointUrl", endpointUrl);
Template template = velocityengine.getTemplate(this.getEndpointTemplatePath());
template.merge(context, writer);
} catch (Exception e) {
log.error("Velocity Error");
throw new APITemplateException("Velocity Error", e);
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.TemplateUtilContext in project carbon-apimgt by wso2.
the class TemplateUtilContextTest method testTemplateUtilContext.
@Test
public void testTemplateUtilContext() throws Exception {
API api = new API(new APIIdentifier("admin", "TestAPI", "1.0.0"));
api.setStatus(APIConstants.CREATED);
api.setContextTemplate("/");
ConfigContext configcontext = new APIConfigContext(api);
TemplateUtilContext templateUtilContext = new TemplateUtilContext(configcontext);
String xmlSampleText = "<data>TemplateUtilContextTest Class</data>";
String xmlEscapedText = "<data>TemplateUtilContextTest Class</data>";
String result = templateUtilContext.escapeXml(xmlSampleText);
Assert.assertTrue("Failed to escape XML tags in the provided string : " + xmlSampleText, xmlEscapedText.equalsIgnoreCase(result));
Assert.assertNotNull(templateUtilContext.getContext().get("util"));
}
Aggregations