Search in sources :

Example 1 with CompositeAPIEndpointDTO

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) 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) Template(org.apache.velocity.Template)

Example 2 with CompositeAPIEndpointDTO

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);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) CompositeAPIEndpointDTO(org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Aggregations

StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)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 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 API (org.wso2.carbon.apimgt.core.models.API)1 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)1 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)1 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)1 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)1 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)1 CompositeAPIConfigContext (org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext)1 CompositeAPIEndpointDTO (org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO)1