Search in sources :

Example 26 with GlobalPolicy

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

the class ApiMgtDAO method getGlobalPolicies.

/**
 * Get all Global level policeis belongs to specific tenant
 *
 * @param tenantID
 * @return
 * @throws APIManagementException
 */
public GlobalPolicy[] getGlobalPolicies(int tenantID) throws APIManagementException {
    List<GlobalPolicy> policies = new ArrayList<GlobalPolicy>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sqlQuery = SQLConstants.GET_GLOBAL_POLICIES;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.GET_GLOBAL_POLICIES;
    }
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, tenantID);
        rs = ps.executeQuery();
        while (rs.next()) {
            String siddhiQuery = null;
            GlobalPolicy globalPolicy = new GlobalPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
            globalPolicy.setDescription(rs.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION));
            globalPolicy.setPolicyId(rs.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            globalPolicy.setUUID(rs.getString(ThrottlePolicyConstants.COLUMN_UUID));
            globalPolicy.setTenantId(rs.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            globalPolicy.setKeyTemplate(rs.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
            globalPolicy.setDeployed(rs.getBoolean(ThrottlePolicyConstants.COLUMN_DEPLOYED));
            InputStream siddhiQueryBlob = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
            if (siddhiQueryBlob != null) {
                siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
            }
            globalPolicy.setSiddhiQuery(siddhiQuery);
            policies.add(globalPolicy);
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return policies.toArray(new GlobalPolicy[policies.size()]);
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 27 with GlobalPolicy

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

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomGet.

/**
 * Retrieves all Global level policies
 *
 * @param accept          Accept header value
 * @return All matched Global Throttle policies to the given request
 */
@Override
public Response throttlingPoliciesCustomGet(String accept, MessageContext messageContext) {
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        int tenantId = APIUtil.getTenantId(userName);
        // only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();
        Policy[] globalPolicies = apiAdmin.getPolicies(tenantId, PolicyConstants.POLICY_LEVEL_GLOBAL);
        List<GlobalPolicy> policies = new ArrayList<>();
        for (Policy policy : globalPolicies) {
            policies.add((GlobalPolicy) policy);
        }
        CustomRuleListDTO listDTO = GlobalThrottlePolicyMappingUtil.fromGlobalPolicyArrayToListDTO(policies.toArray(new GlobalPolicy[policies.size()]));
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Global level policies";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) ArrayList(java.util.ArrayList) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 28 with GlobalPolicy

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

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdDelete.

/**
 * Delete a Global level policy/custom rule specified by uuid
 *
 * @param ruleId            uuid of the policy
 * @return 200 OK response if successfully deleted the policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdDelete(String ruleId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_GLOBAL, existingPolicy.getPolicyName());
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while deleting custom rule : " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 29 with GlobalPolicy

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

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomPost.

/**
 * Add an Global Level Throttle Policy
 *
 * @param body        DTO of new policy to be created
 * @param contentType Content-Type header
 * @return Created policy along with the location of it with Location header
 */
@Override
public Response throttlingPoliciesCustomPost(String contentType, CustomRuleDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();
        GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(body);
        // Check if there's a policy exists before adding the new policy
        try {
            Policy policyIfExists = apiProvider.getGlobalPolicy(globalPolicy.getPolicyName());
            if (policyIfExists != null) {
                RestApiUtil.handleResourceAlreadyExistsError("Custom rule with name " + globalPolicy.getPolicyName() + " already exists", log);
            }
        } catch (PolicyNotFoundException ignore) {
        }
        // Add the policy
        apiProvider.addPolicy(globalPolicy);
        // retrieve the new policy and send back as the response
        GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicy(body.getPolicyName());
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
        return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_GLOBAL + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding a custom rule: " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving Global Throttle policy location : " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 30 with GlobalPolicy

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

the class GlobalThrottlePolicyMappingUtil method fromGlobalThrottlePolicyDTOToModel.

/**
 * Converts a single Global policy DTO object into model object
 *
 * @param dto Global policy DTO object
 * @return Model object derived from DTO
 * @throws UnsupportedThrottleLimitTypeException
 */
public static GlobalPolicy fromGlobalThrottlePolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
    // update mandatory fields such as tenantDomain etc.
    dto = CommonThrottleMappingUtil.updateDefaultMandatoryFieldsOfThrottleDTO(dto);
    GlobalPolicy globalPolicy = new GlobalPolicy(dto.getPolicyName());
    globalPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, globalPolicy);
    globalPolicy.setKeyTemplate(dto.getKeyTemplate());
    globalPolicy.setSiddhiQuery(dto.getSiddhiQuery());
    return globalPolicy;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)

Aggregations

GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)19 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)9 InputStream (java.io.InputStream)7 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ResultSet (java.sql.ResultSet)5 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)4 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)4