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