use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionPoliciesApiServiceImpl method subscriptionPoliciesGet.
@Override
public Response subscriptionPoliciesGet(String xWSO2Tenant, String policyName, MessageContext messageContext) {
SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
if (StringUtils.isNotEmpty(xWSO2Tenant)) {
if (APIConstants.CHAR_ASTERIX.equals(xWSO2Tenant)) {
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
} else if (StringUtils.isNotEmpty(policyName)) {
List<SubscriptionPolicy> model = new ArrayList<>();
SubscriptionPolicy subscriptionPolicy = subscriptionValidationDAO.getSubscriptionPolicyByNameForTenant(policyName, xWSO2Tenant);
if (subscriptionPolicy != null) {
model.add(subscriptionPolicy);
}
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(model)).build();
} else {
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies(xWSO2Tenant))).build();
}
} else {
if (StringUtils.isNotEmpty(policyName)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), "X-WSo2-Tenant header is missing.").build();
}
}
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class AbstractKeyValidationHandler method validate.
private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String apiTenantDomain, int tenantId, SubscriptionDataStore datastore, API api, ApplicationKeyMapping key, Application app, Subscription sub, String keyManager) {
String subscriptionStatus = sub.getSubscriptionState();
String type = key.getKeyType();
if (APIConstants.SubscriptionStatus.BLOCKED.equals(subscriptionStatus)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
infoDTO.setAuthorized(false);
return infoDTO;
} else if (APIConstants.SubscriptionStatus.ON_HOLD.equals(subscriptionStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subscriptionStatus)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.SUBSCRIPTION_INACTIVE);
infoDTO.setAuthorized(false);
return infoDTO;
} else if (APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subscriptionStatus) && !APIConstants.API_KEY_TYPE_SANDBOX.equals(type)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
infoDTO.setType(type);
infoDTO.setAuthorized(false);
return infoDTO;
}
infoDTO.setTier(sub.getPolicyId());
infoDTO.setSubscriber(app.getSubName());
infoDTO.setApplicationId(app.getId().toString());
infoDTO.setApiName(api.getApiName());
infoDTO.setApiVersion(api.getApiVersion());
infoDTO.setApiPublisher(api.getApiProvider());
infoDTO.setApplicationName(app.getName());
infoDTO.setApplicationTier(app.getPolicy());
infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setAppAttributes(app.getAttributes());
infoDTO.setType(type);
// Advanced Level Throttling Related Properties
String apiTier = api.getApiTier();
String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), APIUtil.getTenantIdFromTenantDomain(app.getOrganization()));
if (appPolicy == null) {
try {
appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), app.getOrganization());
datastore.addOrUpdateApplicationPolicy(appPolicy);
} catch (DataLoadingException e) {
log.error("Error while loading ApplicationPolicy");
}
}
SubscriptionPolicy subPolicy = datastore.getSubscriptionPolicyByName(sub.getPolicyId(), tenantId);
if (subPolicy == null) {
try {
subPolicy = new SubscriptionDataLoaderImpl().getSubscriptionPolicy(sub.getPolicyId(), apiTenantDomain);
datastore.addOrUpdateSubscriptionPolicy(subPolicy);
} catch (DataLoadingException e) {
log.error("Error while loading SubscriptionPolicy");
}
}
ApiPolicy apiPolicy = datastore.getApiPolicyByName(api.getApiTier(), tenantId);
boolean isContentAware = false;
if (appPolicy.isContentAware() || subPolicy.isContentAware() || (apiPolicy != null && apiPolicy.isContentAware())) {
isContentAware = true;
}
infoDTO.setContentAware(isContentAware);
// TODO this must implement as a part of throttling implementation.
int spikeArrest = 0;
String apiLevelThrottlingKey = "api_level_throttling_key";
if (subPolicy.getRateLimitCount() > 0) {
spikeArrest = subPolicy.getRateLimitCount();
}
String spikeArrestUnit = null;
if (subPolicy.getRateLimitTimeUnit() != null) {
spikeArrestUnit = subPolicy.getRateLimitTimeUnit();
}
boolean stopOnQuotaReach = subPolicy.isStopOnQuotaReach();
int graphQLMaxDepth = 0;
if (subPolicy.getGraphQLMaxDepth() > 0) {
graphQLMaxDepth = subPolicy.getGraphQLMaxDepth();
}
int graphQLMaxComplexity = 0;
if (subPolicy.getGraphQLMaxComplexity() > 0) {
graphQLMaxComplexity = subPolicy.getGraphQLMaxComplexity();
}
List<String> list = new ArrayList<String>();
list.add(apiLevelThrottlingKey);
infoDTO.setSpikeArrestLimit(spikeArrest);
infoDTO.setSpikeArrestUnit(spikeArrestUnit);
infoDTO.setStopOnQuotaReach(stopOnQuotaReach);
infoDTO.setSubscriberTenantDomain(subscriberTenant);
infoDTO.setGraphQLMaxDepth(graphQLMaxDepth);
infoDTO.setGraphQLMaxComplexity(graphQLMaxComplexity);
if (apiTier != null && apiTier.trim().length() > 0) {
infoDTO.setApiTier(apiTier);
}
// We also need to set throttling data list associated with given API. This need to have
// policy id and
// condition id list for all throttling tiers associated with this API.
infoDTO.setThrottlingDataList(list);
infoDTO.setAuthorized(true);
return infoDTO;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class KeyManagerDataServiceImpl method getSubscriptionPolicyFromSubscriptionPolicyEvent.
private SubscriptionPolicy getSubscriptionPolicyFromSubscriptionPolicyEvent(SubscriptionPolicyEvent event) {
SubscriptionPolicy policy = new SubscriptionPolicy();
policy.setId(event.getPolicyId());
policy.setQuotaType(event.getQuotaType());
policy.setRateLimitCount(event.getRateLimitCount());
policy.setRateLimitTimeUnit(event.getRateLimitTimeUnit());
policy.setStopOnQuotaReach(event.isStopOnQuotaReach());
policy.setTenantId(event.getTenantId());
policy.setTierName(event.getPolicyName());
policy.setGraphQLMaxComplexity(event.getGraphQLMaxComplexity());
policy.setGraphQLMaxDepth(event.getGraphQLMaxDepth());
if (log.isDebugEnabled()) {
log.debug("Event: " + event.toString());
log.debug("Converted : " + policy.toString());
}
return policy;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method populateSubscriptionPolicyList.
private List<SubscriptionPolicy> populateSubscriptionPolicyList(ResultSet resultSet) throws SQLException {
List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
if (resultSet != null) {
while (resultSet.next()) {
SubscriptionPolicy subscriptionPolicyDTO = new SubscriptionPolicy();
subscriptionPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
subscriptionPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
subscriptionPolicyDTO.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
subscriptionPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(subscriptionPolicyDTO.getTenantId());
subscriptionPolicyDTO.setTenantDomain(tenantDomain);
subscriptionPolicyDTO.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
subscriptionPolicyDTO.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
subscriptionPolicyDTO.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
subscriptionPolicyDTO.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
subscriptionPolicyDTO.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
setCommonProperties(subscriptionPolicyDTO, resultSet);
subscriptionPolicies.add(subscriptionPolicyDTO);
}
}
return subscriptionPolicies;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getSubscriptionPolicyByNameForTenant.
/*
* @param policyName : name of the subscription level throttling policy
* @return {@link SubscriptionPolicy}
* */
public SubscriptionPolicy getSubscriptionPolicyByNameForTenant(String policyName, String tenantDomain) {
if (StringUtils.isNotEmpty(policyName) && StringUtils.isNotEmpty(tenantDomain)) {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_SUBSCRIPTION_POLICY_SQL)) {
int tenantId = 0;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
log.error("Error in loading ApplicationPolicy for tenantDomain : " + tenantDomain, e);
}
ps.setString(1, policyName);
ps.setInt(2, tenantId);
try (ResultSet resultSet = ps.executeQuery()) {
if (resultSet.next()) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
subscriptionPolicy.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
subscriptionPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
subscriptionPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
subscriptionPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
subscriptionPolicy.setTenantDomain(APIUtil.getTenantDomainFromTenantId(tenantId));
subscriptionPolicy.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
subscriptionPolicy.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
subscriptionPolicy.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
subscriptionPolicy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
subscriptionPolicy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
setCommonProperties(subscriptionPolicy, resultSet);
return subscriptionPolicy;
}
}
} catch (SQLException e) {
log.error("Error in retrieving Subscription policy by id : " + policyName + " for " + tenantDomain, e);
}
}
return null;
}
Aggregations