Search in sources :

Example 6 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method fromApplicationKeyMappingToApplicationKeyMappingListDTO.

public static ApplicationKeyMappingListDTO fromApplicationKeyMappingToApplicationKeyMappingListDTO(List<ApplicationKeyMapping> model) {
    ApplicationKeyMappingListDTO applicationKeyMappingListDTO = new ApplicationKeyMappingListDTO();
    if (model != null) {
        for (ApplicationKeyMapping applicationKeyMapping : model) {
            ApplicationKeyMappingDTO applicationKeyMappingDTO = new ApplicationKeyMappingDTO();
            applicationKeyMappingDTO.setApplicationId(applicationKeyMapping.getApplicationId());
            applicationKeyMappingDTO.setConsumerKey(applicationKeyMapping.getConsumerKey());
            applicationKeyMappingDTO.setKeyType(applicationKeyMapping.getKeyType());
            applicationKeyMappingDTO.setKeyManager(applicationKeyMapping.getKeyManager());
            applicationKeyMappingDTO.setApplicationUUID(applicationKeyMapping.getApplicationUUID());
            applicationKeyMappingListDTO.getList().add(applicationKeyMappingDTO);
        }
        applicationKeyMappingListDTO.setCount(model.size());
    } else {
        applicationKeyMappingListDTO.setCount(0);
    }
    return applicationKeyMappingListDTO;
}
Also used : ApplicationKeyMappingListDTO(org.wso2.carbon.apimgt.internal.service.dto.ApplicationKeyMappingListDTO) ApplicationKeyMappingDTO(org.wso2.carbon.apimgt.internal.service.dto.ApplicationKeyMappingDTO) ApplicationKeyMapping(org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping)

Example 7 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.

the class AbstractKeyValidationHandler method validateSubscriptionDetails.

private APIKeyValidationInfoDTO validateSubscriptionDetails(APIKeyValidationInfoDTO infoDTO, String context, String version, String consumerKey, String keyManager, boolean defaultVersionInvoked) {
    String apiTenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(context);
    if (apiTenantDomain == null) {
        apiTenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    int tenantId = APIUtil.getTenantIdFromTenantDomain(apiTenantDomain);
    API api = null;
    ApplicationKeyMapping key = null;
    Application app = null;
    Subscription sub = null;
    SubscriptionDataStore datastore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(apiTenantDomain);
    // TODO add a check to see whether datastore is initialized an load data using rest api if it is not loaded
    if (datastore != null) {
        api = datastore.getApiByContextAndVersion(context, version);
        if (api != null) {
            key = datastore.getKeyMappingByKeyAndKeyManager(consumerKey, keyManager);
            if (key != null) {
                app = datastore.getApplicationById(key.getApplicationId());
                if (app != null) {
                    sub = datastore.getSubscriptionById(app.getId(), api.getApiId());
                    if (sub != null) {
                        if (log.isDebugEnabled()) {
                            log.debug("All information is retrieved from the inmemory data store.");
                        }
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("Valid subscription not found for appId " + app.getId() + " and apiId " + api.getApiId());
                        }
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Application not found in the datastore for id " + key.getApplicationId());
                    }
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Application keymapping not found in the datastore for id consumerKey " + consumerKey);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("API not found in the datastore for " + context + ":" + version);
            }
        }
    } else {
        log.error("Subscription datastore is not initialized for tenant domain " + apiTenantDomain);
    }
    if (api != null && app != null && key != null && sub != null) {
        validate(infoDTO, apiTenantDomain, tenantId, datastore, api, key, app, sub, keyManager);
    } else if (!infoDTO.isAuthorized() && infoDTO.getValidationStatus() == 0) {
        // Scenario where validation failed and message is not set
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_AUTH_RESOURCE_FORBIDDEN);
    } else {
        infoDTO.setAuthorized(false);
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_AUTH_RESOURCE_FORBIDDEN);
    }
    return infoDTO;
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription) Application(org.wso2.carbon.apimgt.keymgt.model.entity.Application) ApplicationKeyMapping(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)

Example 8 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.

the class ApplicationKeyMappingsApiServiceImpl method applicationKeyMappingsGet.

@Override
public Response applicationKeyMappingsGet(String xWSO2Tenant, String consumerKey, String keymanager, MessageContext messageContext) throws APIManagementException {
    SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    if (StringUtils.isNotEmpty(consumerKey)) {
        ApplicationKeyMapping keyMapping = subscriptionValidationDAO.getApplicationKeyMapping(consumerKey, keymanager, xWSO2Tenant);
        List<ApplicationKeyMapping> applicationKeyMappings = new ArrayList<>();
        if (keyMapping != null) {
            applicationKeyMappings.add(keyMapping);
        }
        return Response.ok().entity(SubscriptionValidationDataUtil.fromApplicationKeyMappingToApplicationKeyMappingListDTO(applicationKeyMappings)).build();
    }
    if (StringUtils.isNotEmpty(xWSO2Tenant)) {
        return Response.ok().entity(SubscriptionValidationDataUtil.fromApplicationKeyMappingToApplicationKeyMappingListDTO(subscriptionValidationDAO.getAllApplicationKeyMappings(xWSO2Tenant))).build();
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ApplicationKeyMapping(org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 9 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method loadAllKeyMappings.

@Override
public List<ApplicationKeyMapping> loadAllKeyMappings(String tenantDomain) throws DataLoadingException {
    String applicationsEP = APIConstants.SubscriptionValidationResources.APPLICATION_KEY_MAPPINGS;
    List<ApplicationKeyMapping> applicationKeyMappings = new ArrayList<>();
    String responseString = null;
    try {
        responseString = invokeService(applicationsEP, tenantDomain);
    } catch (IOException e) {
        String msg = "Error while executing the http client " + applicationsEP;
        log.error(msg, e);
        throw new DataLoadingException(msg, e);
    }
    if (responseString != null && !responseString.isEmpty()) {
        applicationKeyMappings = (new Gson().fromJson(responseString, ApplicationKeyMappingList.class)).getList();
    }
    return applicationKeyMappings;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) ArrayList(java.util.ArrayList) 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 10 with ApplicationKeyMapping

use of org.wso2.carbon.apimgt.api.model.subscription.ApplicationKeyMapping in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method getKeyMappingByKeyAndKeyManager.

@Override
public ApplicationKeyMapping getKeyMappingByKeyAndKeyManager(String key, String keyManager) {
    ApplicationKeyMappingCacheKey applicationKeyMappingCacheKey = new ApplicationKeyMappingCacheKey(key, keyManager);
    String synchronizeKey = "SubscriptionDataStoreImpl-KeyMapping-" + applicationKeyMappingCacheKey;
    ApplicationKeyMapping applicationKeyMapping = applicationKeyMappingMap.get(applicationKeyMappingCacheKey);
    if (applicationKeyMapping == null) {
        synchronized (synchronizeKey.intern()) {
            applicationKeyMapping = applicationKeyMappingMap.get(applicationKeyMappingCacheKey);
            if (applicationKeyMapping != null) {
                return applicationKeyMapping;
            }
            try {
                applicationKeyMapping = new SubscriptionDataLoaderImpl().getKeyMapping(key, keyManager, tenantDomain);
            } catch (DataLoadingException e) {
                log.error("Error while Loading KeyMapping Information from Internal API.", e);
            }
            if (applicationKeyMapping != null && !StringUtils.isEmpty(applicationKeyMapping.getConsumerKey())) {
                // load to the memory
                log.debug("Loading Keymapping to the in-memory datastore.");
                addOrUpdateApplicationKeyMapping(applicationKeyMapping);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving Application information with Consumer Key : " + key + " and keymanager : " + keyManager);
        if (applicationKeyMapping != null) {
            log.debug("Retrieved Application information with Consumer Key : " + key + " and keymanager : " + keyManager + " is " + applicationKeyMapping.toString());
        } else {
            log.debug("Retrieving Application information with Consumer Key : " + key + " and keymanager : " + keyManager + " is empty");
        }
    }
    return applicationKeyMapping;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) ApplicationKeyMappingCacheKey(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingCacheKey) ApplicationKeyMapping(org.wso2.carbon.apimgt.keymgt.model.entity.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