use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingDenyPoliciesPost.
/**
* Add a Block Condition
*
* @param body DTO of new block condition to be created
* @param contentType Content-Type header
* @return Created block condition along with the location of it with Location header
*/
@Override
public Response throttlingDenyPoliciesPost(String contentType, BlockingConditionDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// Add the block condition. It will throw BlockConditionAlreadyExistsException if the condition already
// exists in the system
String uuid = null;
if (ConditionTypeEnum.API.equals(body.getConditionType()) || ConditionTypeEnum.APPLICATION.equals(body.getConditionType()) || ConditionTypeEnum.USER.equals(body.getConditionType())) {
uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), (String) body.getConditionValue(), body.isConditionStatus());
} else if (ConditionTypeEnum.IP.equals(body.getConditionType()) || ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
if (body.getConditionValue() instanceof Map) {
JSONObject jsonObject = new JSONObject();
jsonObject.putAll((Map) body.getConditionValue());
if (ConditionTypeEnum.IP.equals(body.getConditionType())) {
RestApiAdminUtils.validateIPAddress(jsonObject.get("fixedIp").toString());
}
if (ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
RestApiAdminUtils.validateIPAddress(jsonObject.get("startingIp").toString());
RestApiAdminUtils.validateIPAddress(jsonObject.get("endingIp").toString());
}
uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), jsonObject.toJSONString(), body.isConditionStatus());
}
}
// retrieve the new blocking condition and send back as the response
BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(uuid);
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_BLOCK_CONDITIONS + "/" + uuid)).entity(dto).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceAlreadyExists(e)) {
RestApiUtil.handleResourceAlreadyExistsError("A black list item with type: " + body.getConditionType() + ", value: " + body.getConditionValue() + " already exists", e, log);
} else {
String errorMessage = "Error while adding Blocking Condition. Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException | ParseException e) {
String errorMessage = "Error while retrieving Blocking Condition resource location: Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtil method fromBlockConditionListToListDTO.
/**
* Converts a List of Block Condition in to REST API LIST DTO Object
*
* @param blockConditionList A List of Block Conditions
* @return REST API List DTO object derived from Block Condition list
*/
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditionsDTO> blockConditionList) throws ParseException {
BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
if (blockConditionList != null) {
for (BlockConditionsDTO blockCondition : blockConditionList) {
BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
blockingConditionDTOList.add(dto);
}
}
listDTO.setCount(blockingConditionDTOList.size());
listDTO.setList(blockingConditionDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class ApiMgtDAO method getBlockConditionByUUID.
/**
* Get details of a block condition by UUID
*
* @param uuid uuid of the block condition
* @return Block condition represented by the UUID
* @throws APIManagementException
*/
public BlockConditionsDTO getBlockConditionByUUID(String uuid) throws APIManagementException {
Connection connection = null;
PreparedStatement selectPreparedStatement = null;
ResultSet resultSet = null;
BlockConditionsDTO blockCondition = null;
try {
String query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITION_BY_UUID_SQL;
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(true);
selectPreparedStatement = connection.prepareStatement(query);
selectPreparedStatement.setString(1, uuid);
resultSet = selectPreparedStatement.executeQuery();
if (resultSet.next()) {
blockCondition = new BlockConditionsDTO();
blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
blockCondition.setConditionType(resultSet.getString("TYPE"));
blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
blockCondition.setUUID(resultSet.getString("UUID"));
}
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
handleException("Failed to rollback getting Block condition by uuid " + uuid, ex);
}
}
handleException("Failed to get Block condition by uuid " + uuid, e);
} finally {
APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
}
return blockCondition;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class APIProviderImplTest method getPolicyAPILevelPerUser.
private APIPolicy getPolicyAPILevelPerUser() {
APIPolicy policy = new APIPolicy("custom1");
policy.setUserLevel(PolicyConstants.PER_USER);
policy.setDescription("Description");
// policy.setPolicyLevel("api");
policy.setTenantDomain("carbon.super");
RequestCountLimit defaultLimit = new RequestCountLimit();
defaultLimit.setTimeUnit("min");
defaultLimit.setUnitTime(5);
defaultLimit.setRequestCount(400);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setLimit(defaultLimit);
defaultQuotaPolicy.setType("RequestCount");
policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
List<Pipeline> pipelines;
Pipeline p;
QuotaPolicy quotaPolicy;
List<Condition> condition;
RequestCountLimit countlimit;
Condition cond;
pipelines = new ArrayList<Pipeline>();
// /////////pipeline item start//////
p = new Pipeline();
quotaPolicy = new QuotaPolicy();
quotaPolicy.setType("RequestCount");
countlimit = new RequestCountLimit();
countlimit.setTimeUnit("min");
countlimit.setUnitTime(5);
countlimit.setRequestCount(100);
quotaPolicy.setLimit(countlimit);
condition = new ArrayList<Condition>();
HTTPVerbCondition verbCond = new HTTPVerbCondition();
verbCond.setHttpVerb("POST");
condition.add(verbCond);
p.setQuotaPolicy(quotaPolicy);
p.setConditions(condition);
pipelines.add(p);
// /////////pipeline item end//////
policy.setPipelines(pipelines);
return policy;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.
the class ApiMgtDAO method getSubscriptionBlockCondition.
/**
* Get details of the subscription block condition by condition value and tenant domain
*
* @param conditionValue condition value of the block condition
* @param tenantDomain tenant domain of the block condition
* @return Block condition
* @throws APIManagementException
*/
public BlockConditionsDTO getSubscriptionBlockCondition(String conditionValue, String tenantDomain) throws APIManagementException {
Connection connection = null;
PreparedStatement selectPreparedStatement = null;
ResultSet resultSet = null;
BlockConditionsDTO blockCondition = null;
try {
String query = SQLConstants.ThrottleSQLConstants.GET_SUBSCRIPTION_BLOCK_CONDITION_BY_VALUE_AND_DOMAIN_SQL;
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(true);
selectPreparedStatement = connection.prepareStatement(query);
selectPreparedStatement.setString(1, conditionValue);
selectPreparedStatement.setString(2, tenantDomain);
resultSet = selectPreparedStatement.executeQuery();
if (resultSet.next()) {
blockCondition = new BlockConditionsDTO();
blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
blockCondition.setConditionType(resultSet.getString("TYPE"));
blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
blockCondition.setUUID(resultSet.getString("UUID"));
}
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
handleException("Failed to rollback getting Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, ex);
}
}
handleException("Failed to get Subscription Block condition with condition value " + conditionValue + " of tenant " + tenantDomain, e);
} finally {
APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
}
return blockCondition;
}
Aggregations