use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class ApiMgtDAO method getOperationPolicyByPolicyID.
/**
* Retrieve an operation policy by providing the policy uuid
*
* @param connection DB connection
* @param policyId Policy UUID
* @param isWithPolicyDefinition Include the policy definition to the output or not
* @return operation policy
* @throws SQLException
*/
private OperationPolicyData getOperationPolicyByPolicyID(Connection connection, String policyId, boolean isWithPolicyDefinition) throws SQLException {
String dbQuery = SQLConstants.OperationPolicyConstants.GET_OPERATION_POLICY_FROM_POLICY_ID;
PreparedStatement statement = connection.prepareStatement(dbQuery);
statement.setString(1, policyId);
ResultSet rs = statement.executeQuery();
OperationPolicyData policyData = null;
if (rs.next()) {
policyData = new OperationPolicyData();
policyData.setPolicyId(policyId);
policyData.setOrganization(rs.getString("ORGANIZATION"));
policyData.setMd5Hash(rs.getString("POLICY_MD5"));
policyData.setSpecification(populatePolicySpecificationFromRS(rs));
}
rs.close();
statement.close();
if (isWithPolicyDefinition && policyData != null) {
if (isWithPolicyDefinition && policyData != null) {
populatePolicyDefinitions(connection, policyId, policyData);
}
}
return policyData;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class APIProviderImpl method importOperationPolicy.
/**
* This method will be used to import Operation policy. This will check existing API specific policy first and
* then common policy.
* If API specific policy exists and MD5 hash matches, it will not import and will return the existing API specific policy.
* If the existing API specific policy is different in md5, it will be updated the existing policy
* If a common policy exists and MD5 hash match, it will return the common policy's id. This policy will be imported at the API update.
* If the common policy is different then the imported policy, a new API specific policy will be created.
* If there aren't any existing policies, a new API specific policy will be created.
*
* @param importedPolicyData Imported policy
* @param organization Organization name
* @return corrosponding policy ID for imported data
* @throws APIManagementException if failed to delete APIRevision
*/
@Override
public String importOperationPolicy(OperationPolicyData importedPolicyData, String organization) throws APIManagementException {
OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();
OperationPolicyData existingOperationPolicy = getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedPolicyData.getApiUUID(), null, organization, false);
String policyId = null;
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");
}
} else {
if (log.isDebugEnabled()) {
log.debug("Even though existing API specific policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ".Therefore updating the existing policy");
}
updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);
}
policyId = existingOperationPolicy.getPolicyId();
} else {
existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), organization, false);
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching common policy found for imported policy and Md5 hashes match.");
}
policyId = existingOperationPolicy.getPolicyId();
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("Even though existing common policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ". A new policy created with ID " + policyId);
}
}
} else {
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("There aren't any existing policies for the imported policy. A new policy created with ID " + policyId);
}
}
}
return policyId;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class APIProviderImpl method validateOperationPolicyParameters.
private void validateOperationPolicyParameters(API api, String tenantDomain) throws APIManagementException {
boolean isOperationPoliciesAllowedForAPIType = true;
Set<URITemplate> uriTemplates = api.getUriTemplates();
if (APIConstants.API_TYPE_WS.equals(api.getType()) || APIConstants.API_TYPE_SSE.equals(api.getType()) || APIConstants.API_TYPE_WEBSUB.equals(api.getType())) {
if (log.isDebugEnabled()) {
log.debug("Operation policies are not allowed for " + api.getType() + " APIs");
}
isOperationPoliciesAllowedForAPIType = false;
}
Map<String, Integer> policyOccurrenceMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
List<OperationPolicy> validatedPolicies = new ArrayList<>();
if (operationPolicies != null && !operationPolicies.isEmpty() && isOperationPoliciesAllowedForAPIType) {
for (OperationPolicy policy : operationPolicies) {
String policyId = policy.getPolicyId();
// First check the API specific operation policy list
OperationPolicyData policyData = getAPISpecificOperationPolicyByPolicyId(policyId, api.getUuid(), tenantDomain, false);
if (policyData != null) {
if (log.isDebugEnabled()) {
log.debug("A policy is found for " + policyId + " as " + policyData.getSpecification().getName() + ". Validating the policy");
}
if (policyData.isRevision()) {
throw new APIManagementException("Invalid policy selected. " + policyId + " policy is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
}
OperationPolicySpecification policySpecification = policyData.getSpecification();
if (validateAppliedPolicyWithSpecification(policySpecification, policy, api)) {
String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
int previousOccurrenceCount = 0;
if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
if (previousOccurrenceCount > 0 && !policySpecification.isMultipleAllowed()) {
throw new APIManagementException("Policy multiple allowed property violated. " + policySpecification.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
}
policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
validatedPolicies.add(policy);
}
} else {
// TODO: get policy based on the name
OperationPolicyData commonPolicyData = getCommonOperationPolicyByPolicyId(policyId, tenantDomain, false);
if (commonPolicyData != null) {
// attributes and added to API policy list
if (log.isDebugEnabled()) {
log.debug("A common policy is found for " + policyId + ". Validating the policy");
}
OperationPolicySpecification commonPolicySpec = commonPolicyData.getSpecification();
if (validateAppliedPolicyWithSpecification(commonPolicySpec, policy, api)) {
String policyOccurrenceKey = uriTemplate.getUriTemplate() + "_" + policy.getDirection() + "_" + policy.getPolicyName();
int previousOccurrenceCount = 0;
if (policyOccurrenceMap.get(policyOccurrenceKey) != null) {
previousOccurrenceCount = policyOccurrenceMap.get(policyOccurrenceKey);
if (previousOccurrenceCount > 0 && !commonPolicySpec.isMultipleAllowed()) {
throw new APIManagementException("Policy multiple allowed property violated. " + commonPolicySpec.getDisplayName() + " cannot be applied multiple times.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
}
policyOccurrenceMap.put(policyOccurrenceKey, previousOccurrenceCount + 1);
validatedPolicies.add(policy);
}
} else {
throw new APIManagementException("Selected policy " + policyId + " is not found.", ExceptionCodes.INVALID_OPERATION_POLICY);
}
}
}
}
uriTemplate.setOperationPolicies(validatedPolicies);
}
}
Aggregations