use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method validateAppliedPolicyWithSpecification.
private boolean validateAppliedPolicyWithSpecification(OperationPolicySpecification policySpecification, OperationPolicy appliedPolicy, API api) throws APIManagementException {
// Validate the policy applied direction
if (!policySpecification.getApplicableFlows().contains(appliedPolicy.getDirection())) {
if (log.isDebugEnabled()) {
log.debug("The policy " + policySpecification.getName() + " is not support in the " + appliedPolicy.getDirection() + " flow. Hence skipped.");
}
throw new APIManagementException(policySpecification.getName() + " cannot be used in the " + appliedPolicy.getDirection() + " flow.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
// Validate the API type
if (!policySpecification.getSupportedApiTypes().contains(api.getType())) {
if (log.isDebugEnabled()) {
log.debug("The policy " + policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.");
}
throw new APIManagementException(policySpecification.getName() + " cannot be used for the " + api.getType() + " API type.", ExceptionCodes.OPERATION_POLICY_NOT_ALLOWED_IN_THE_APPLIED_FLOW);
}
// Validate policy Attributes
if (policySpecification.getPolicyAttributes() != null) {
for (OperationPolicySpecAttribute attribute : policySpecification.getPolicyAttributes()) {
if (attribute.isRequired()) {
Object appliedPolicyAttribute = appliedPolicy.getParameters().get(attribute.getName());
if (appliedPolicyAttribute != null) {
if (attribute.getValidationRegex() != null) {
Pattern pattern = Pattern.compile(attribute.getValidationRegex(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher((String) appliedPolicyAttribute);
if (!matcher.matches()) {
throw new APIManagementException("Policy attribute " + attribute.getName() + " regex validation error.", ExceptionCodes.INVALID_OPERATION_POLICY_PARAMETERS);
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName());
}
throw new APIManagementException("Required policy attribute " + attribute.getName() + " is not found for the the policy " + policySpecification.getName() + appliedPolicy.getDirection() + " flow.", ExceptionCodes.MISSING_MANDATORY_POLICY_ATTRIBUTES);
}
}
}
}
return true;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method setOperationPoliciesToURITemplates.
@Override
public void setOperationPoliciesToURITemplates(String apiId, Set<URITemplate> uriTemplates) throws APIManagementException {
Set<URITemplate> uriTemplatesWithPolicies = apiMgtDAO.getURITemplatesWithOperationPolicies(apiId);
if (!uriTemplatesWithPolicies.isEmpty()) {
// This is a temporary map to keep operation policies list of URI Templates against the URI mapping ID
Map<String, List<OperationPolicy>> operationPoliciesMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplatesWithPolicies) {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
if (!operationPolicies.isEmpty()) {
operationPoliciesMap.put(key, operationPolicies);
}
}
for (URITemplate uriTemplate : uriTemplates) {
String key = uriTemplate.getHTTPVerb() + ":" + uriTemplate.getUriTemplate();
if (operationPoliciesMap.containsKey(key)) {
uriTemplate.setOperationPolicies(operationPoliciesMap.get(key));
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class APIMappingUtil method setOperationPoliciesToOperationsDTO.
/**
* Reads the operationPolicies from the API object passed in, and sets them back to the API Operations DTO
*
* @param api API object
* @param apiOperationsDTO List of API Operations DTO
*/
private static void setOperationPoliciesToOperationsDTO(API api, List<APIOperationsDTO> apiOperationsDTO) {
Set<URITemplate> uriTemplates = api.getUriTemplates();
Map<String, URITemplate> uriTemplateMap = new HashMap<>();
for (URITemplate uriTemplate : uriTemplates) {
String key = uriTemplate.getUriTemplate() + ":" + uriTemplate.getHTTPVerb();
uriTemplateMap.put(key, uriTemplate);
}
for (APIOperationsDTO operationsDTO : apiOperationsDTO) {
String key = operationsDTO.getTarget() + ":" + operationsDTO.getVerb();
List<OperationPolicy> operationPolicies = uriTemplateMap.get(key).getOperationPolicies();
if (!operationPolicies.isEmpty()) {
operationsDTO.setOperationPolicies(OperationPolicyMappingUtil.fromOperationPolicyListToDTO(operationPolicies));
}
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class OperationPolicyMappingUtil method fromDTOToOperationPolicy.
public static OperationPolicy fromDTOToOperationPolicy(OperationPolicyDTO operationPolicyDTO) {
OperationPolicy operationPolicy = new OperationPolicy();
operationPolicy.setPolicyName(operationPolicyDTO.getPolicyName());
operationPolicy.setPolicyId(operationPolicyDTO.getPolicyId());
operationPolicy.setParameters(operationPolicyDTO.getParameters());
return operationPolicy;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicy in project carbon-apimgt by wso2.
the class OperationPolicyMappingUtil method fromOperationPolicyToDTO.
public static OperationPolicyDTO fromOperationPolicyToDTO(OperationPolicy operationPolicy) {
OperationPolicyDTO dto = new OperationPolicyDTO();
dto.setPolicyName(operationPolicy.getPolicyName());
dto.setPolicyId(operationPolicy.getPolicyId());
dto.setOrder(operationPolicy.getOrder());
dto.setParameters(operationPolicy.getParameters());
return dto;
}
Aggregations