Search in sources :

Example 41 with Policy

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

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToInfoDTO.

/**
 * Converts a single Advanced Policy model into REST API DTO
 *
 * @param apiPolicy Advanced Policy model object
 * @return Converted Advanced policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
    if (apiPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)

Example 42 with Policy

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

the class ApplicationThrottlePolicyMappingUtil method fromApplicationThrottlePolicyToDTO.

/**
 * Converts a single Application Policy model into REST API DTO
 *
 * @param appPolicy An Application Policy model object
 * @return Converted Application policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static ApplicationThrottlePolicyDTO fromApplicationThrottlePolicyToDTO(Policy appPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    ApplicationThrottlePolicyDTO policyDTO = new ApplicationThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(appPolicy, policyDTO);
    if (appPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(appPolicy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : ApplicationThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO)

Example 43 with Policy

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

the class ApplicationThrottlePolicyMappingUtil method fromApplicationPolicyArrayToListDTO.

/**
 * Converts an array of Application Policy objects into a List DTO
 *
 * @param appPolicies Array of Application Policies
 * @return A List DTO of converted Application Policies
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static ApplicationThrottlePolicyListDTO fromApplicationPolicyArrayToListDTO(List<ApplicationPolicy> appPolicies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    ApplicationThrottlePolicyListDTO listDTO = new ApplicationThrottlePolicyListDTO();
    List<ApplicationThrottlePolicyDTO> appPolicyDTOList = new ArrayList<>();
    if (appPolicies != null) {
        for (Policy policy : appPolicies) {
            ApplicationThrottlePolicyDTO dto = fromApplicationThrottlePolicyToDTO(policy);
            appPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(appPolicyDTOList.size());
    listDTO.setList(appPolicyDTOList);
    return listDTO;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) ApplicationThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyListDTO) ApplicationThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO)

Example 44 with Policy

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

the class CommonThrottleMappingUtil method fromQuotaPolicyToDTO.

/**
 * Converts a Quota Policy object into a Throttle Limit DTO object
 *
 * @param quotaPolicy Quota Policy object
 * @return Throttle Limit DTO object derived from the Quota Policy object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static ThrottleLimitDTO fromQuotaPolicyToDTO(QuotaPolicy quotaPolicy) throws UnsupportedThrottleLimitTypeException {
    Limit limit = quotaPolicy.getLimit();
    String throttleLimitType = quotaPolicy.getType();
    if (PolicyConstants.REQUEST_COUNT_TYPE.equals(throttleLimitType)) {
        if (limit instanceof RequestCountLimit) {
            RequestCountLimit requestCountLimit = (RequestCountLimit) limit;
            return fromRequestCountLimitToDTO(requestCountLimit);
        } else {
            String msg = "Throttle limit type " + throttleLimitType + " is not of type RequestCountLimit";
            log.error(msg);
            throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
        }
    } else if (PolicyConstants.BANDWIDTH_TYPE.equals(throttleLimitType)) {
        if (limit instanceof BandwidthLimit) {
            BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
            return fromBandwidthLimitToDTO(bandwidthLimit);
        } else {
            String msg = "Throttle limit type " + throttleLimitType + " is not of type BandwidthLimit";
            log.error(msg);
            throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
        }
    } else {
        String msg = "Throttle limit type " + throttleLimitType + " is not supported";
        log.error(msg);
        throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
    }
}
Also used : UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 45 with Policy

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

the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdDelete.

/**
 * Delete a custom rule specified by uuid.
 *
 * @param ruleId            uuid of the policy
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK response if successfully deleted the policy
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomRuleIdDelete(String ruleId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy DELETE request with rule ID = " + ruleId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        apiMgtAdminService.deleteCustomRule(ruleId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting a custom policy uuid : " + ruleId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER, ruleId);
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Test (org.testng.annotations.Test)106 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)79 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)73 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)64 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)63 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)62 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)58 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)54 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)40 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)36 ArrayList (java.util.ArrayList)35 PreparedStatement (java.sql.PreparedStatement)32 SQLException (java.sql.SQLException)32 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)30 Connection (java.sql.Connection)27 API (org.wso2.carbon.apimgt.core.models.API)27 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)26 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)25 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)23