Search in sources :

Example 56 with APIPolicy

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

the class SubscriptionValidationDAO method getAllApiPolicies.

/*
     * @param tenantDomain : tenant domain name
     * @return {@link List<APIPolicy>}
     * */
public List<APIPolicy> getAllApiPolicies(String tenantDomain) {
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_TENANT_API_POLICIES_SQL)) {
        int tenantId = 0;
        try {
            tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
        } catch (UserStoreException e) {
            log.error("Error in loading ApplicationPolicies for tenantDomain : " + tenantDomain, e);
        }
        ps.setInt(1, tenantId);
        try (ResultSet resultSet = ps.executeQuery()) {
            return populateApiPolicyList(resultSet);
        }
    } catch (SQLException e) {
        log.error("Error in loading api policies for tenantId : " + tenantDomain, e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 57 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy 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
 * @throws UnsupportedThrottleConditionTypeException
 */
public static AdvancedThrottlePolicyInfoDTO fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyInfoDTO policyDTO = new AdvancedThrottlePolicyInfoDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
    if (apiPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
    }
    policyDTO.setType(ADVACNCED_THROTTLING_POLICY_INFO_TYPE);
    return policyDTO;
}
Also used : AdvancedThrottlePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyInfoDTO)

Example 58 with APIPolicy

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

the class ApiMgtDAO method getAPIPolicyByUUID.

/**
 * Retrieves {@link APIPolicy} with name <code>uuid</code>
 * <p>This will retrieve complete details about the APIPolicy with all pipelines and conditions.</p>
 *
 * @param uuid uuid of the policy to retrieve from the database
 * @return {@link APIPolicy}
 * @throws APIManagementException
 */
public APIPolicy getAPIPolicyByUUID(String uuid) throws APIManagementException {
    APIPolicy policy = null;
    Connection connection = null;
    PreparedStatement selectStatement = null;
    ResultSet resultSet = null;
    String sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_BY_UUID_SQL;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_BY_UUID_SQL;
    }
    try {
        connection = APIMgtDBUtil.getConnection();
        selectStatement = connection.prepareStatement(sqlQuery);
        selectStatement.setString(1, uuid);
        // Should return only single result
        resultSet = selectStatement.executeQuery();
        if (resultSet.next()) {
            policy = new APIPolicy(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
            setCommonPolicyDetails(policy, resultSet);
            policy.setUserLevel(resultSet.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
            policy.setPipelines(getPipelines(policy.getPolicyId()));
        }
    } catch (SQLException e) {
        handleException("Failed to get api policy: " + uuid, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(selectStatement, connection, resultSet);
    }
    return policy;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy)

Example 59 with APIPolicy

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

the class ApiMgtDAO method addAPIPolicy.

/**
 * Add a API level throttling policy to database.
 * <p>
 * If valid policy Id (not -1) is present in the <code>policy</code> object,
 * policy will be inserted with that policy Id.
 * Otherwise policy Id will be auto incremented.
 * </p>
 *
 * @param policy policy object defining the throttle policy
 * @throws SQLException
 */
private void addAPIPolicy(APIPolicy policy, Connection conn) throws SQLException {
    ResultSet resultSet = null;
    PreparedStatement policyStatement = null;
    String addQuery = SQLConstants.ThrottleSQLConstants.INSERT_API_POLICY_SQL;
    int policyId;
    try {
        String dbProductName = conn.getMetaData().getDatabaseProductName();
        policyStatement = conn.prepareStatement(addQuery, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "POLICY_ID") });
        setCommonParametersForPolicy(policyStatement, policy);
        policyStatement.setString(12, policy.getUserLevel());
        policyStatement.executeUpdate();
        // Get the inserted POLICY_ID (auto incremented value)
        resultSet = policyStatement.getGeneratedKeys();
        // Returns only single row
        if (resultSet.next()) {
            /*
                 *  H2 doesn't return generated keys when key is provided (not generated).
                   Therefore policyId should be policy parameter's policyId when it is provided.
                 */
            policyId = resultSet.getInt(1);
            List<Pipeline> pipelines = policy.getPipelines();
            if (pipelines != null) {
                for (Pipeline pipeline : pipelines) {
                    // add each pipeline data to AM_CONDITION_GROUP table
                    addPipeline(pipeline, policyId, conn);
                }
            }
        }
    } finally {
        APIMgtDBUtil.closeAllConnections(policyStatement, null, resultSet);
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline)

Example 60 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy 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;
}
Also used : HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15