Search in sources :

Example 21 with BlockConditionsDTO

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

the class APIProviderImpl method deleteBlockConditionByUUID.

@Override
public boolean deleteBlockConditionByUUID(String uuid) throws APIManagementException {
    boolean deleteState = false;
    BlockConditionsDTO blockCondition = apiMgtDAO.getBlockConditionByUUID(uuid);
    if (blockCondition != null) {
        deleteState = apiMgtDAO.deleteBlockCondition(blockCondition.getConditionId());
        if (deleteState) {
            unpublishBlockCondition(blockCondition);
        }
    }
    return deleteState;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)

Example 22 with BlockConditionsDTO

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

the class APIProviderImpl method updateBlockConditionByUUID.

@Override
public boolean updateBlockConditionByUUID(String uuid, String state) throws APIManagementException {
    boolean updateState = apiMgtDAO.updateBlockConditionStateByUUID(uuid, state);
    BlockConditionsDTO blockConditionsDTO = apiMgtDAO.getBlockConditionByUUID(uuid);
    if (updateState && blockConditionsDTO != null) {
        publishBlockingEventUpdate(blockConditionsDTO);
    }
    return updateState;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)

Example 23 with BlockConditionsDTO

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

the class APIProviderImpl method publishBlockingEvent.

/**
 * Publishes the changes on blocking conditions.
 * @param blockConditionsDTO Blockcondition Dto event
 */
private void publishBlockingEvent(BlockConditionsDTO blockConditionsDTO, String state) {
    String conditionType = blockConditionsDTO.getConditionType();
    String conditionValue = blockConditionsDTO.getConditionValue();
    if (APIConstants.BLOCKING_CONDITIONS_IP.equals(conditionType) || APIConstants.BLOCK_CONDITION_IP_RANGE.equals(conditionType)) {
        conditionValue = StringEscapeUtils.escapeJava(conditionValue);
    }
    Object[] objects = new Object[] { blockConditionsDTO.getConditionId(), blockConditionsDTO.getConditionType(), conditionValue, state, tenantDomain };
    Event blockingMessage = new Event(APIConstants.BLOCKING_CONDITIONS_STREAM_ID, System.currentTimeMillis(), null, null, objects);
    EventPublisherEvent blockingEvent = new EventPublisherEvent(APIConstants.BLOCKING_CONDITIONS_STREAM_ID, System.currentTimeMillis(), objects, blockingMessage.toString());
    APIUtil.publishEvent(EventPublisherType.BLOCKING_EVENT, blockingEvent, blockingMessage.toString());
}
Also used : EventPublisherEvent(org.wso2.carbon.apimgt.eventing.EventPublisherEvent) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent) ScopeEvent(org.wso2.carbon.apimgt.impl.notifier.events.ScopeEvent) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) LifeCycleEvent(org.wso2.carbon.apimgt.api.model.LifeCycleEvent) Event(org.wso2.carbon.databridge.commons.Event) CertificateEvent(org.wso2.carbon.apimgt.impl.notifier.events.CertificateEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) EventPublisherEvent(org.wso2.carbon.apimgt.eventing.EventPublisherEvent) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) JSONObject(org.json.simple.JSONObject)

Example 24 with BlockConditionsDTO

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

the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdDelete.

/**
 * Delete a block condition specified by the condition Id
 *
 * @param conditionId       Id of the block condition
 * @return 200 OK response if successfully deleted the block condition
 */
@Override
public Response throttlingDenyPolicyConditionIdDelete(String conditionId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give BlockConditionNotFoundException if there's no block condition exists with UUID
        BlockConditionsDTO existingCondition = apiProvider.getBlockConditionByUUID(conditionId);
        if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, existingCondition)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
        }
        apiProvider.deleteBlockConditionByUUID(conditionId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
        } else {
            String errorMessage = "Error while deleting Block Condition. Id : " + conditionId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 25 with BlockConditionsDTO

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

the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdPatch.

/**
 * Updates an existing condition status of a blocking condition
 *
 * @param conditionId       Id of the block condition
 * @param body              content to update
 * @param contentType       Content-Type header
 * @return 200 response if successful
 */
@Override
public Response throttlingDenyPolicyConditionIdPatch(String conditionId, String contentType, BlockingConditionStatusDTO body, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give BlockConditionNotFoundException if there's no block condition exists with UUID
        BlockConditionsDTO existingCondition = apiProvider.getBlockConditionByUUID(conditionId);
        if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, existingCondition)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
        }
        // update the status
        apiProvider.updateBlockConditionByUUID(conditionId, String.valueOf(body.isConditionStatus()));
        // retrieve the new blocking condition and send back as the response
        BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(conditionId);
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
        return Response.ok().entity(dto).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
        } else {
            String errorMessage = "Error while updating Block Condition Status. Id : " + conditionId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

BlockConditionsDTO (org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)24 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 ParseException (org.json.simple.parser.ParseException)4 ArrayList (java.util.ArrayList)3 JSONObject (org.json.simple.JSONObject)2 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1