Search in sources :

Example 31 with APITemplateException

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

the class PolicyExportManager method getCustomPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param customPolicies custom policy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private Map<String, String> getCustomPolicySiddhiApps(List<CustomPolicy> customPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for custom policies.");
    }
    Map<String, String> siddhiApps = new HashMap<>();
    String name;
    String executionPlan;
    CustomThrottlePolicyTemplateBuilder templateBuilder;
    for (CustomPolicy policy : customPolicies) {
        templateBuilder = new CustomThrottlePolicyTemplateBuilder(policy);
        name = CUSTOM + policy.getPolicyName();
        executionPlan = templateBuilder.getThrottlePolicyTemplateForCustomPolicy();
        siddhiApps.put(name, executionPlan);
    }
    return siddhiApps;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) HashMap(java.util.HashMap) CustomThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.CustomThrottlePolicyTemplateBuilder)

Example 32 with APITemplateException

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

the class PolicyExportManager method getAppPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param applicationPolicies ApplicationPolicy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private Map<String, String> getAppPolicySiddhiApps(List<ApplicationPolicy> applicationPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for application policy.");
    }
    Map<String, String> siddhiApps = new HashMap<>();
    String name;
    String executionPlan;
    ApplicationThrottlePolicyTemplateBuilder templateBuilder;
    for (ApplicationPolicy policy : applicationPolicies) {
        templateBuilder = new ApplicationThrottlePolicyTemplateBuilder(policy);
        name = APPLICATION + policy.getPolicyName();
        executionPlan = templateBuilder.getThrottlePolicyForAppLevel();
        siddhiApps.put(name, executionPlan);
    }
    return siddhiApps;
}
Also used : HashMap(java.util.HashMap) ApplicationThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.ApplicationThrottlePolicyTemplateBuilder) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Example 33 with APITemplateException

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

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForAPILevelDefaultCondition.

/**
 * Generate default policy for api level throttling
 *
 * @return throttle policies for default api
 * @throws APITemplateException throws if generation failure occur
 */
public String getThrottlePolicyTemplateForAPILevelDefaultCondition() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy and generate the template
    Set<String> conditionsSet = new HashSet<String>();
    List<Pipeline> pipelines = apiPolicy.getPipelines();
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
    StringWriter writer;
    VelocityContext context;
    // when APIPolicy contains pipelines, get template as a string
    if (pipelines != null) {
        for (Pipeline pipeline : pipelines) {
            String conditionString = getPolicyConditionForDefault(pipeline.getConditions());
            if (!StringUtils.isEmpty(conditionString)) {
                conditionsSet.add(conditionString);
            }
        }
    }
    // for default API policy
    context = new VelocityContext();
    setConstantContext(context);
    // default policy is defined as 'elseCondition' , set values for velocity context
    context.put(PIPELINE, ELSE_CONDITION);
    context.put(PIPELINE_ITEM, null);
    context.put(POLICY, apiPolicy);
    context.put(QUOTA_POLICY, apiPolicy.getDefaultQuotaPolicy());
    String conditionSetString = getConditionForDefault(conditionsSet);
    if (!StringUtils.isEmpty(conditionSetString)) {
        context.put(CONDITION, AND + conditionSetString);
    } else {
        context.put(CONDITION, EMPTY_STRING);
    }
    writer = new StringWriter();
    template.merge(context, writer);
    if (log.isDebugEnabled()) {
        log.debug("Generated Siddhi App : " + writer.toString());
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) HashSet(java.util.HashSet) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) Template(org.apache.velocity.Template)

Example 34 with APITemplateException

use of org.wso2.carbon.apimgt.impl.template.APITemplateException 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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) 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)

Example 35 with APITemplateException

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

the class APITemplateBuilderImpl method getConfigStringForTemplate.

@Override
public String getConfigStringForTemplate(Environment environment) throws APITemplateException {
    StringWriter writer = new StringWriter();
    try {
        // build the context for template and apply the necessary decorators
        ConfigContext configcontext = null;
        if (api != null) {
            configcontext = createConfigContext(api, environment);
        } else {
            // API Product scenario
            configcontext = createConfigContext(apiProduct, environment);
        }
        // @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 = null;
        if (api != null) {
            t = velocityengine.getTemplate(getTemplatePath());
            if (APIConstants.APITransportType.WS.toString().equals(api.getType())) {
                context.put("topicMappings", this.api.getWebSocketTopicMappingConfiguration().getMappings());
            } else if (APIConstants.APITransportType.WEBSUB.toString().equals(api.getType())) {
                String signingAlgorithm = api.getWebsubSubscriptionConfiguration().getSigningAlgorithm();
                context.put("signingAlgorithm", signingAlgorithm.toLowerCase() + "=");
                context.put("secret", api.getWebsubSubscriptionConfiguration().getSecret());
                context.put("hmacSignatureGenerationAlgorithm", "Hmac" + signingAlgorithm);
                context.put("signatureHeader", api.getWebsubSubscriptionConfiguration().getSignatureHeader());
                context.put("isSecurityEnabled", !StringUtils.isEmpty(api.getWebsubSubscriptionConfiguration().getSecret()));
            } else if (APIConstants.GRAPHQL_API.equals(api.getType())) {
                boolean isSubscriptionAvailable = false;
                if (api.getWebSocketTopicMappingConfiguration() != null) {
                    isSubscriptionAvailable = true;
                    context.put(APIConstants.VELOCITY_API_WEBSOCKET_TOPIC_MAPPINGS, this.api.getWebSocketTopicMappingConfiguration().getMappings());
                }
                context.put(APIConstants.VELOCITY_GRAPHQL_API_SUBSCRIPTION_AVAILABLE, isSubscriptionAvailable);
            }
        } else {
            t = velocityengine.getTemplate(getApiProductTemplatePath());
        }
        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) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) 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)

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