use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDataUtil method fromApiPolicyToApiPolicyListDTO.
public static ApiPolicyListDTO fromApiPolicyToApiPolicyListDTO(List<APIPolicy> model) {
ApiPolicyListDTO apiPolicyListDTO = new ApiPolicyListDTO();
if (model != null) {
for (APIPolicy apiPolicyModel : model) {
ApiPolicyDTO policyDTO = new ApiPolicyDTO();
policyDTO.setName(apiPolicyModel.getName());
policyDTO.setQuotaType(apiPolicyModel.getQuotaType());
policyDTO.setTenantId(apiPolicyModel.getTenantId());
policyDTO.setTenantDomain(apiPolicyModel.getTenantDomain());
policyDTO.setApplicableLevel(apiPolicyModel.getApplicableLevel());
policyDTO.setDefaultLimit(getThrottleLimitDTO(apiPolicyModel));
apiPolicyListDTO.getList().add(policyDTO);
List<APIPolicyConditionGroup> retrievedGroups = apiPolicyModel.getConditionGroups();
List<ApiPolicyConditionGroupDTO> condGroups = new ArrayList<ApiPolicyConditionGroupDTO>();
for (APIPolicyConditionGroup retGroup : retrievedGroups) {
ApiPolicyConditionGroupDTO group = new ApiPolicyConditionGroupDTO();
group.setConditionGroupId(retGroup.getConditionGroupId());
group.setQuotaType(retGroup.getQuotaType());
group.setDefaultLimit(getThrottleLimitDTO(retGroup));
group.setPolicyId(retGroup.getPolicyId());
List<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO> condition = new ArrayList<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO>();
List<ConditionDTO> retrievedConditions = retGroup.getConditionDTOS();
for (ConditionDTO retrievedCondition : retrievedConditions) {
org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO conditionDTO = new org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO();
conditionDTO.setConditionType(retrievedCondition.getConditionType());
conditionDTO.setIsInverted(retrievedCondition.isInverted());
conditionDTO.setName(retrievedCondition.getConditionName());
conditionDTO.setValue(retrievedCondition.getConditionValue());
condition.add(conditionDTO);
}
group.setCondition(condition);
condGroups.add(group);
}
policyDTO.setConditionGroups(condGroups);
}
apiPolicyListDTO.setCount(model.size());
} else {
apiPolicyListDTO.setCount(0);
}
return apiPolicyListDTO;
}
use of org.wso2.carbon.apimgt.core.models.policy.APIPolicy 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.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class ApiPoliciesApiServiceImpl method apiPoliciesGet.
public Response apiPoliciesGet(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.fromApiPolicyToApiPolicyListDTO(subscriptionValidationDAO.getAllApiPolicies())).build();
} else if (StringUtils.isNotEmpty(policyName)) {
List<APIPolicy> model = new ArrayList<>();
APIPolicy apiPolicy = subscriptionValidationDAO.getApiPolicyByNameForTenant(policyName, xWSO2Tenant);
if (apiPolicy != null) {
model.add(apiPolicy);
}
return Response.ok().entity(SubscriptionValidationDataUtil.fromApiPolicyToApiPolicyListDTO(model)).build();
} else {
return Response.ok().entity(SubscriptionValidationDataUtil.fromApiPolicyToApiPolicyListDTO(subscriptionValidationDAO.getAllApiPolicies(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.fromApiPolicyToApiPolicyListDTO(subscriptionValidationDAO.getAllApiPolicies())).build();
}
use of org.wso2.carbon.apimgt.core.models.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.core.models.policy.APIPolicy in project carbon-apimgt by wso2.
the class AbstractAPIManagerTestCase method testGetPolicies.
@Test
public void testGetPolicies() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException, XMLStreamException {
APIPolicy[] policies1 = { new APIPolicy("policy1") };
ApplicationPolicy[] policies2 = { new ApplicationPolicy("policy2"), new ApplicationPolicy("policy3") };
SubscriptionPolicy[] policies3 = { new SubscriptionPolicy("policy4"), new SubscriptionPolicy("policy5"), new SubscriptionPolicy("policy6") };
GlobalPolicy[] policies4 = { new GlobalPolicy("policy7"), new GlobalPolicy("policy8"), new GlobalPolicy("policy9"), new GlobalPolicy("policy0") };
PowerMockito.mockStatic(APIUtil.class);
BDDMockito.when(APIUtil.getTenantId(Mockito.anyString())).thenReturn(-1234);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
Mockito.when(apiMgtDAO.getAPIPolicies(Mockito.anyInt())).thenReturn(policies1);
Mockito.when(apiMgtDAO.getApplicationPolicies(Mockito.anyInt())).thenReturn(policies2);
Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies3);
Mockito.when(apiMgtDAO.getGlobalPolicies(Mockito.anyInt())).thenReturn(policies4);
ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class, Mockito.RETURNS_MOCKS);
PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
Assert.assertEquals(abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_API).length, 1);
Assert.assertEquals(abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_APP).length, 2);
PowerMockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(false);
Assert.assertEquals(3, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_SUB).length);
Assert.assertEquals(4, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_GLOBAL).length);
Assert.assertEquals(0, abstractAPIManager.getPolicies(API_PROVIDER, "Test").length);
}
Aggregations