Search in sources :

Example 11 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getPolicyConditionForDefault.

/**
 * Produces final condition inside a pipeline for default policy with null string
 *
 * @param conditions list of default throttle conditions
 * @return formatted condition list
 */
private static String getPolicyConditionForDefault(List<Condition> conditions) {
    String conditionString = null;
    int i = 0;
    for (Condition condition : conditions) {
        String conditionStringComplete = condition.getCondition();
        if (i == 0) {
            conditionString = conditionStringComplete;
        } else {
            conditionString = conditionString + AND + conditionStringComplete;
        }
        i++;
    }
    return conditionString;
}
Also used : Condition(org.wso2.carbon.apimgt.core.models.policy.Condition)

Example 12 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultBlockCondition.

public static BlockConditions createDefaultBlockCondition(String conditionType) {
    BlockConditions blockConditions = new BlockConditions();
    blockConditions.setConditionType(conditionType);
    blockConditions.setEnabled(true);
    if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP)) {
        blockConditions.setConditionValue(SAMPLE_IP_1);
    } else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        blockConditions.setStartingIP(SAMPLE_IP_1);
        blockConditions.setEndingIP(SAMPLE_IP_2);
    } else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API)) {
        try {
            API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
            API api = apiBuilder.build();
            DAOFactory.getApiDAO().addAPI(api);
            blockConditions.setConditionValue(api.getContext());
        } catch (APIMgtDAOException e) {
            log.error("Error while adding default api in default block condition", e);
        }
    } else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION)) {
        try {
            Application app = createDefaultApplication();
            DAOFactory.getApplicationDAO().addApplication(app);
            blockConditions.setConditionValue(app.getId() + ":" + app.getName());
        } catch (APIMgtDAOException e) {
            log.error("Error while adding default app in default block condition", e);
        }
    } else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER)) {
        blockConditions.setConditionValue(ADMIN);
    }
    return blockConditions;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 13 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToIPCondition.

/**
 * Converts a IP Condition DTO object into a model object
 *
 * @param dto IP Condition DTO object
 * @return IP Condition model object derived from DTO
 */
public static IPCondition fromDTOToIPCondition(ThrottleConditionDTO dto) throws UnsupportedThrottleConditionTypeException {
    String ipConditionType = mapIPConditionTypeFromDTOToModel(dto.getIpCondition().getIpConditionType());
    IPCondition ipCondition = new IPCondition(ipConditionType);
    ipCondition = updateFieldsFromDTOToCondition(dto, ipCondition);
    ipCondition.setSpecificIP(dto.getIpCondition().getSpecificIP());
    ipCondition.setStartingIP(dto.getIpCondition().getStartingIP());
    ipCondition.setEndingIP(dto.getIpCondition().getEndingIP());
    return ipCondition;
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition)

Example 14 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromQueryParameterConditionToDTO.

/**
 * Converts a Query Parameter Condition model object into a DTO
 *
 * @param condition Query Parameter Condition model object
 * @return DTO object that was derived from Query Parameter Condition model object
 */
public static ThrottleConditionDTO fromQueryParameterConditionToDTO(QueryParameterCondition condition) {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.QUERY_PARAMS_CONDITION_TYPE);
    throttleConditionDTO.setQueryParameterCondition(new QueryParameterConditionDTO());
    throttleConditionDTO = updateFieldsFromConditionToDTO(condition, throttleConditionDTO);
    throttleConditionDTO.getQueryParameterCondition().setParameterName(condition.getParameter());
    throttleConditionDTO.getQueryParameterCondition().setParameterValue(condition.getValue());
    return throttleConditionDTO;
}
Also used : QueryParameterConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.QueryParameterConditionDTO) ThrottleConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleConditionDTO)

Example 15 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToQueryParameterCondition.

/**
 * Converts a Query Parameter Condition DTO object into a model object
 *
 * @param dto Query Parameter Condition DTO object
 * @return Query Parameter Condition model object derived from Query Parameter Condition DTO
 */
public static QueryParameterCondition fromDTOToQueryParameterCondition(ThrottleConditionDTO dto) {
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition = updateFieldsFromDTOToCondition(dto, queryParameterCondition);
    queryParameterCondition.setParameter(dto.getQueryParameterCondition().getParameterName());
    queryParameterCondition.setValue(dto.getQueryParameterCondition().getParameterValue());
    return queryParameterCondition;
}
Also used : QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 Test (org.testng.annotations.Test)16 PreparedStatement (java.sql.PreparedStatement)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 ResultSet (java.sql.ResultSet)9 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)4 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)4