use of org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.
@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
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.core.template.dto.CompositeAPIEndpointDTO in project carbon-apimgt by wso2.
the class APIStoreImpl method setGatewayDefinitionSource.
private void setGatewayDefinitionSource(CompositeAPI.Builder apiBuilder) throws APIManagementException {
List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
List<TemplateBuilderDTO> resourceList = new ArrayList<>();
String appId = null;
List<CompositeAPIEndpointDTO> endpointDTOs = new ArrayList<CompositeAPIEndpointDTO>();
try {
appId = apiBuilder.getApplicationId();
List<Subscription> subscriptions = getApiSubscriptionDAO().getAPISubscriptionsByApplication(apiBuilder.getApplicationId(), ApiType.STANDARD);
for (Subscription subscription : subscriptions) {
CompositeAPIEndpointDTO endpointDTO = new CompositeAPIEndpointDTO();
API api = subscription.getApi();
endpointDTO.setEndpointName(api.getName());
// TODO: currently only HTTPS endpoint considered. Websocket APIs and http transport should considered
endpointDTO.setTransportType(APIMgtConstants.HTTPS);
// TODO: replace host with gateway domain host
String endpointUrl = APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + config.getHostname() + "/" + api.getContext() + "/" + api.getVersion();
endpointDTO.setEndpointUrl(endpointUrl);
endpointDTOs.add(endpointDTO);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error while getting subscriptions of the application " + appId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
for (UriTemplate uriTemplate : list) {
TemplateBuilderDTO dto = new TemplateBuilderDTO();
dto.setTemplateId(uriTemplate.getTemplateId());
dto.setUriTemplate(uriTemplate.getUriTemplate());
dto.setHttpVerb(uriTemplate.getHttpVerb());
resourceList.add(dto);
}
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String gatewayConfig = gatewaySourceGenerator.getCompositeAPIConfigStringFromTemplate(resourceList, endpointDTOs);
if (log.isDebugEnabled()) {
log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
}
apiBuilder.gatewayConfig(gatewayConfig);
}
Aggregations