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;
}
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;
}
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();
}
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();
}
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();
}
Aggregations