use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIPolicies.
/**
* Get API level policies. Result only contains basic details of the policy,
* it doesn't contain pipeline information.
*
* @param tenantID policies are selected using tenantID
* @return APIPolicy ArrayList
* @throws APIManagementException
*/
public APIPolicy[] getAPIPolicies(int tenantID) throws APIManagementException {
List<APIPolicy> policies = new ArrayList<APIPolicy>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICIES;
if (forceCaseInsensitiveComparisons) {
sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICIES;
}
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(sqlQuery);
ps.setInt(1, tenantID);
rs = ps.executeQuery();
while (rs.next()) {
APIPolicy apiPolicy = new APIPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(apiPolicy, rs);
apiPolicy.setUserLevel(rs.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
policies.add(apiPolicy);
}
} catch (SQLException e) {
handleException("Error while executing SQL", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, rs);
}
return policies.toArray(new APIPolicy[policies.size()]);
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method setJWTClaimConditions.
/**
* Add JWT claim conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
* provided {@link Condition} array
*
* @param pipelineId Id of the pipeline
* @param conditions condition array to populate
* @throws APIManagementException
*/
private void setJWTClaimConditions(int pipelineId, ArrayList<Condition> conditions) throws APIManagementException {
Connection connection = null;
PreparedStatement conditionsStatement = null;
ResultSet resultSet = null;
try {
connection = APIMgtDBUtil.getConnection();
conditionsStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_JWT_CLAIM_CONDITIONS_SQL);
conditionsStatement.setInt(1, pipelineId);
resultSet = conditionsStatement.executeQuery();
while (resultSet.next()) {
JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
jwtClaimsCondition.setClaimUrl(resultSet.getString(ThrottlePolicyConstants.COLUMN_CLAIM_URI));
jwtClaimsCondition.setAttribute(resultSet.getString(ThrottlePolicyConstants.COLUMN_CLAIM_ATTRIBUTE));
jwtClaimsCondition.setInvertCondition(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_IS_CLAIM_MAPPING));
conditions.add(jwtClaimsCondition);
}
} catch (SQLException e) {
handleException("Failed to get jwt claim conditions for pipelineId: " + pipelineId, e);
} finally {
APIMgtDBUtil.closeAllConnections(conditionsStatement, connection, resultSet);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline in project carbon-apimgt by wso2.
the class ApiMgtDAO method createConditionGroupDTO.
/**
* Converts an {@code Pipeline} object into a {@code ConditionGroupDTO}.{@code ConditionGroupDTO} class tries to
* contain the same information held by {@code Pipeline}, but in a much lightweight fashion.
*
* @param conditionGroup Id of the condition group ({@code Pipeline}) to be converted
* @return An object of {@code ConditionGroupDTO} type.
* @throws APIManagementException
*/
public ConditionGroupDTO createConditionGroupDTO(int conditionGroup) throws APIManagementException {
List<Condition> conditions = getConditions(conditionGroup);
ArrayList<ConditionDTO> conditionDTOs = new ArrayList<ConditionDTO>(conditions.size());
for (Condition condition : conditions) {
ConditionDTO conditionDTO = new ConditionDTO();
conditionDTO.setConditionType(condition.getType());
conditionDTO.isInverted(condition.isInvertCondition());
if (PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
IPCondition ipRangeCondition = (IPCondition) condition;
conditionDTO.setConditionName(ipRangeCondition.getStartingIP());
conditionDTO.setConditionValue(ipRangeCondition.getEndingIP());
} else if (PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType())) {
IPCondition ipCondition = (IPCondition) condition;
conditionDTO.setConditionName(PolicyConstants.IP_SPECIFIC_TYPE);
conditionDTO.setConditionValue(ipCondition.getSpecificIP());
} else if (PolicyConstants.HEADER_TYPE.equals(condition.getType())) {
HeaderCondition headerCondition = (HeaderCondition) condition;
conditionDTO.setConditionName(headerCondition.getHeaderName());
conditionDTO.setConditionValue(headerCondition.getValue());
} else if (PolicyConstants.JWT_CLAIMS_TYPE.equals(condition.getType())) {
JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) condition;
conditionDTO.setConditionName(jwtClaimsCondition.getClaimUrl());
conditionDTO.setConditionValue(jwtClaimsCondition.getAttribute());
} else if (PolicyConstants.QUERY_PARAMETER_TYPE.equals(condition.getType())) {
QueryParameterCondition parameterCondition = (QueryParameterCondition) condition;
conditionDTO.setConditionName(parameterCondition.getParameter());
conditionDTO.setConditionValue(parameterCondition.getValue());
}
conditionDTOs.add(conditionDTO);
}
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditions(conditionDTOs.toArray(new ConditionDTO[] {}));
return conditionGroupDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.Pipeline 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.api.model.policy.Pipeline 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