Search in sources :

Example 1 with OperationPolicySpecAttribute

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;
}
Also used : OperationPolicySpecAttributeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicySpecAttributeDTO)

Example 2 with OperationPolicySpecAttribute

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;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) JSONObject(org.json.simple.JSONObject)

Example 3 with OperationPolicySpecAttribute

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;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ArrayList(java.util.ArrayList) OperationPolicySpecAttributeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicySpecAttributeDTO) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 4 with OperationPolicySpecAttribute

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;
}
Also used : OperationPolicySpecAttribute(org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) CommentList(org.wso2.carbon.apimgt.api.model.CommentList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

OperationPolicySpecAttribute (org.wso2.carbon.apimgt.api.model.OperationPolicySpecAttribute)3 ArrayList (java.util.ArrayList)2 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)2 OperationPolicySpecAttributeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicySpecAttributeDTO)2 Gson (com.google.gson.Gson)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JSONObject (org.json.simple.JSONObject)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)1 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)1