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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations