Search in sources :

Example 21 with Condition

use of org.wso2.carbon.apimgt.api.model.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 22 with Condition

use of org.wso2.carbon.apimgt.api.model.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 23 with Condition

use of org.wso2.carbon.apimgt.api.model.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 24 with Condition

use of org.wso2.carbon.apimgt.api.model.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)

Example 25 with Condition

use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.

the class BlockingConditionMappingUtil method fromBlockingConditionToDTO.

/**
 * Converts a single Block Condition model object into REST API DTO object.
 *
 * @param blockCondition Block condition model object
 * @return Block condition DTO object derived from block condition model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditions blockCondition) throws UnsupportedThrottleLimitTypeException {
    if (blockCondition.getUuid() == null) {
        return null;
    }
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setConditionId(blockCondition.getUuid());
    dto.setConditionType(blockCondition.getConditionType());
    dto.setStatus(blockCondition.isEnabled());
    if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockCondition.getConditionType())) {
        dto.setIpCondition(fromBlockConditionToIpConditionDTO(blockCondition));
    }
    String conditionValue = blockCondition.getConditionValue();
    dto.setConditionValue(conditionValue);
    return dto;
}
Also used : BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)

Aggregations

HashMap (java.util.HashMap)39 Test (org.junit.Test)32 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)30 List (java.util.List)26 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 PreparedStatement (java.sql.PreparedStatement)23 Map (java.util.Map)22 ResultSet (java.sql.ResultSet)20 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 Connection (java.sql.Connection)16 SQLException (java.sql.SQLException)16 TreeMap (java.util.TreeMap)16 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)15 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)15 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)15 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)15