Search in sources :

Example 36 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class TemplateBuilderUtil method addWebSocketResourceEndpoints.

public static void addWebSocketResourceEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    Map<String, Map<String, String>> topicMappings = api.getWebSocketTopicMappingConfiguration().getMappings();
    List<GatewayContentDTO> endpointsToAdd = new ArrayList<>();
    for (URITemplate resource : uriTemplates) {
        String topic = resource.getUriTemplate();
        Map<String, String> endpoints = topicMappings.get(topic);
        // Production and Sandbox endpoints
        for (Map.Entry<String, String> endpointData : endpoints.entrySet()) {
            if (!"resourceKey".equals(endpointData.getKey())) {
                String endpointType = endpointData.getKey();
                String endpointUrl = endpointData.getValue();
                String endpointConfigContext = builder.getConfigStringForWebSocketEndpointTemplate(endpointType, getWebsocketResourceKey(topic), endpointUrl);
                GatewayContentDTO endpoint = new GatewayContentDTO();
                // For WS APIs, resource type is not applicable,
                // so we can just use the uriTemplate/uriMapping to identify the resource
                endpoint.setName(getEndpointName(endpointConfigContext));
                endpoint.setContent(endpointConfigContext);
                endpointsToAdd.add(endpoint);
            }
        }
        // once through resources is enough.
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            break;
        }
    }
    gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentsToList(endpointsToAdd, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 37 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAppLevel.

/**
 * Generate application level policy
 *
 * @param policy policy with level 'app'. Multiple pipelines are not allowed. Can define more than one condition
 *               as set of conditions. all these conditions should be passed as a single pipeline
 * @return the generated execution plan for the policy
 * @throws APITemplateException if failed to generate policy
 */
public String getThrottlePolicyForAppLevel(ApplicationPolicy policy) throws APITemplateException {
    StringWriter writer = new StringWriter();
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for app level :" + policy.toString());
    }
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        velocityengine.init();
        Template template = velocityengine.getTemplate(getTemplatePathForApplication());
        VelocityContext context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 38 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForSubscriptionLevel.

/**
 * Generate policy for subscription level
 *
 * @param policy policy with level 'sub'. Multiple pipelines are not allowed. Can define more than one condition
 *               as set of conditions. all these conditions should be passed as a single pipeline
 * @return the generated execution plan for the policy
 * @throws APITemplateException if failed to generate policy
 */
public String getThrottlePolicyForSubscriptionLevel(SubscriptionPolicy policy) throws APITemplateException {
    StringWriter writer = new StringWriter();
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for subscription Level :" + policy.toString());
    }
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.init();
        Template template;
        if (PolicyConstants.EVENT_COUNT_TYPE.equals(policy.getDefaultLimit().getQuotaType())) {
            template = velocityengine.getTemplate(getTemplatePathForAsyncSubscription());
        } else {
            template = velocityengine.getTemplate(getTemplatePathForSubscription());
        }
        VelocityContext context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Aggregations

StringWriter (java.io.StringWriter)14 Template (org.apache.velocity.Template)14 VelocityContext (org.apache.velocity.VelocityContext)14 VelocityEngine (org.apache.velocity.app.VelocityEngine)14 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)11 HashMap (java.util.HashMap)10 Test (org.testng.annotations.Test)9 API (org.wso2.carbon.apimgt.core.models.API)6 VelocityException (org.apache.velocity.exception.VelocityException)5 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)5 RegistryException (org.wso2.carbon.registry.api.RegistryException)5 UserStoreException (org.wso2.carbon.user.api.UserStoreException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)4 GatewayContentDTO (org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)4 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)4 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)4 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3