use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method populateApplicationKeyMappingsList.
private void populateApplicationKeyMappingsList(List<ApplicationKeyMapping> keyMappings, ResultSet resultSet) throws SQLException {
if (keyMappings != null && resultSet != null) {
while (resultSet.next()) {
ApplicationKeyMapping keyMapping = new ApplicationKeyMapping();
keyMapping.setApplicationId(resultSet.getInt("APPLICATION_ID"));
keyMapping.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
keyMapping.setKeyType(resultSet.getString("KEY_TYPE"));
keyMapping.setKeyManager(resultSet.getString("KEY_MANAGER"));
keyMapping.setApplicationUUID(resultSet.getString("UUID"));
keyMappings.add(keyMapping);
}
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.
the class SubscriptionDataLoaderImpl method getKeyMapping.
@Override
public ApplicationKeyMapping getKeyMapping(String consumerKey, String keymanager, String tenantDomain) throws DataLoadingException {
ApplicationKeyMapping application = null;
String responseString;
String endPoint = null;
try {
keymanager = URLEncoder.encode(keymanager, APIConstants.DigestAuthConstants.CHARSET);
keymanager = keymanager.replace("\\+", "%20");
endPoint = APIConstants.SubscriptionValidationResources.APPLICATION_KEY_MAPPINGS + "?consumerKey=" + consumerKey + "&keymanager=" + keymanager;
responseString = invokeService(endPoint, tenantDomain);
} catch (IOException e) {
String msg = "Error while executing the http client " + endPoint;
log.error(msg, e);
throw new DataLoadingException(msg, e);
}
if (responseString != null && !responseString.isEmpty()) {
ApplicationKeyMappingList list = new Gson().fromJson(responseString, ApplicationKeyMappingList.class);
if (list.getList() != null && !list.getList().isEmpty()) {
application = list.getList().get(0);
}
}
return application;
}
use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping 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.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getAllApplicationKeyMappings.
/*
* @param subscriptionId : unique identifier of a subscription
* @return {@link Subscription}
* */
public List<ApplicationKeyMapping> getAllApplicationKeyMappings(String tenantDomain) {
List<ApplicationKeyMapping> keyMappings = new ArrayList<>();
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_TENANT_AM_KEY_MAPPING_SQL)) {
int tenantId = 0;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
log.error("Error in loading ApplicationKeyMappings for tenantDomain : " + tenantDomain, e);
}
ps.setInt(1, tenantId);
try (ResultSet resultSet = ps.executeQuery()) {
populateApplicationKeyMappingsList(keyMappings, resultSet);
}
} catch (SQLException e) {
log.error("Error in loading Application key mappings for tenantId : " + tenantDomain, e);
}
return keyMappings;
}
use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getApplicationKeyMapping.
/*
* @param consumerKey : consumer key of an application
* @return {@link ApplicationKeyMapping}
*
* */
public ApplicationKeyMapping getApplicationKeyMapping(String consumerKey, String keymanager, String tenantDomain) {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_AM_KEY_MAPPING_BY_CONSUMER_KEY_SQL)) {
ps.setString(1, consumerKey);
ps.setString(2, keymanager);
ps.setString(3, tenantDomain);
try (ResultSet resultSet = ps.executeQuery()) {
if (resultSet.next()) {
ApplicationKeyMapping keyMapping = new ApplicationKeyMapping();
keyMapping.setApplicationId(resultSet.getInt("APPLICATION_ID"));
keyMapping.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
keyMapping.setKeyType(resultSet.getString("KEY_TYPE"));
keyMapping.setKeyManager(resultSet.getString("KEY_MANAGER"));
keyMapping.setApplicationUUID(resultSet.getString("UUID"));
return keyMapping;
}
}
} catch (SQLException e) {
log.error("Error in loading Application Key Mapping for consumer key : " + consumerKey, e);
}
return null;
}
Aggregations