Search in sources :

Example 1 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.keymgt.model.entity.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);
        }
    }
}
Also used : ApplicationKeyMapping(org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping)

Example 2 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.keymgt.model.entity.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;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) Gson(com.google.gson.Gson) IOException(java.io.IOException) ApplicationKeyMappingList(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingList) ApplicationKeyMapping(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)

Example 3 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.keymgt.model.entity.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;
}
Also used : SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) ArrayList(java.util.ArrayList)

Example 4 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.keymgt.model.entity.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;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ApplicationKeyMapping(org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping)

Example 5 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.keymgt.model.entity.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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ApplicationKeyMapping(org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping)

Aggregations

ApplicationKeyMapping (org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping)5 ApplicationKeyMapping (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)5 ArrayList (java.util.ArrayList)4 DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)4 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)2 ApiPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy)2 ApplicationKeyMappingList (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingList)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 SubscriptionValidationDAO (org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)1 ApplicationKeyMappingDTO (org.wso2.carbon.apimgt.internal.service.dto.ApplicationKeyMappingDTO)1 ApplicationKeyMappingListDTO (org.wso2.carbon.apimgt.internal.service.dto.ApplicationKeyMappingListDTO)1 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)1 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)1 ApplicationKeyMappingCacheKey (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingCacheKey)1