use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute in project carbon-apimgt by wso2.
the class OperationPolicyMappingUtil method fromOperationPolicySpecAttributesToDTO.
public static OperationPolicySpecAttributeDTO fromOperationPolicySpecAttributesToDTO(OperationPolicySpecAttribute specAttribute) {
OperationPolicySpecAttributeDTO specAttributeDTO = new OperationPolicySpecAttributeDTO();
specAttributeDTO.setName(specAttribute.getName());
specAttributeDTO.setDisplayName(specAttribute.getDisplayName());
specAttributeDTO.setDescription(specAttribute.getDescription());
specAttributeDTO.setType(specAttribute.getType().toString());
specAttributeDTO.setValidationRegex(specAttribute.getValidationRegex());
specAttributeDTO.setRequired(specAttribute.isRequired());
specAttributeDTO.setDefaultValue(specAttribute.getDefaultValue());
if (specAttribute.getType().equals(OperationPolicySpecAttribute.AttributeType.Enum)) {
specAttributeDTO.setAllowedValues(specAttribute.getAllowedValues());
}
return specAttributeDTO;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute 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.OperationPolicySpecAttribute in project carbon-apimgt by wso2.
the class OperationPolicyMappingUtil method fromOperationPolicyDataToDTO.
public static OperationPolicyDataDTO fromOperationPolicyDataToDTO(OperationPolicyData policyData) {
OperationPolicyDataDTO policyDataDTO = new OperationPolicyDataDTO();
OperationPolicySpecification policySpecification = policyData.getSpecification();
policyDataDTO.setId(policyData.getPolicyId());
policyDataDTO.setMd5(policyData.getMd5Hash());
policyDataDTO.setIsAPISpecific(policyData.isApiSpecificPolicy());
policyDataDTO.setName(policySpecification.getName());
policyDataDTO.setDisplayName(policySpecification.getDisplayName());
policyDataDTO.setDescription(policySpecification.getDescription());
policyDataDTO.setSupportedGateways(policySpecification.getSupportedGateways());
policyDataDTO.setSupportedApiTypes(policySpecification.getSupportedApiTypes());
policyDataDTO.setApplicableFlows(policySpecification.getApplicableFlows());
policyDataDTO.setMultipleAllowed(policySpecification.isMultipleAllowed());
policyDataDTO.setCategory(policySpecification.getCategory().toString());
if (policySpecification.getPolicyAttributes() != null) {
List<OperationPolicySpecAttributeDTO> specAttributeDtoList = new ArrayList<>();
for (OperationPolicySpecAttribute specAttribute : policySpecification.getPolicyAttributes()) {
OperationPolicySpecAttributeDTO specAttributeDTO = fromOperationPolicySpecAttributesToDTO(specAttribute);
specAttributeDtoList.add(specAttributeDTO);
}
policyDataDTO.setPolicyAttributes(specAttributeDtoList);
}
return policyDataDTO;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute in project carbon-apimgt by wso2.
the class ApiMgtDAO method populatePolicySpecificationFromRS.
/**
* This method will read the result set and populate OperationPolicySpecification object.
*
* @param rs Result set
* @return OperationPolicySpecification object
* @throws APIManagementException
* @throws SQLException
*/
private OperationPolicySpecification populatePolicySpecificationFromRS(ResultSet rs) throws SQLException {
OperationPolicySpecification policySpecification = new OperationPolicySpecification();
policySpecification.setName(rs.getString("POLICY_NAME"));
policySpecification.setDisplayName(rs.getString("DISPLAY_NAME"));
policySpecification.setDescription(rs.getString("POLICY_DESCRIPTION"));
policySpecification.setApplicableFlows(getListFromString(rs.getString("APPLICABLE_FLOWS")));
policySpecification.setSupportedApiTypes(getListFromString(rs.getString("API_TYPES")));
policySpecification.setSupportedGateways(getListFromString(rs.getString("GATEWAY_TYPES")));
policySpecification.setCategory(OperationPolicySpecification.PolicyCategory.valueOf(rs.getString("POLICY_CATEGORY")));
policySpecification.setMultipleAllowed(rs.getBoolean("MULTIPLE_ALLOWED"));
List<OperationPolicySpecAttribute> policySpecAttributes = null;
try (InputStream policyParametersStream = rs.getBinaryStream("POLICY_PARAMETERS")) {
String policyParametersString = IOUtils.toString(policyParametersStream);
policySpecAttributes = new Gson().fromJson(policyParametersString, new TypeToken<List<OperationPolicySpecAttribute>>() {
}.getType());
} catch (IOException e) {
log.error("Error while converting policy specification attributes for the policy " + policySpecification.getName(), e);
}
policySpecification.setPolicyAttributes(policySpecAttributes);
return policySpecification;
}
Aggregations