Search in sources :

Example 16 with DataLoadingException

use of org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException in project carbon-apimgt by wso2.

the class AbstractKeyValidationHandler method validate.

private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String apiTenantDomain, int tenantId, SubscriptionDataStore datastore, API api, Application app, Subscription sub) {
    String subscriptionStatus = sub.getSubscriptionState();
    String type = app.getTokenType();
    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 subscriberUserId = sub.getSubscriptionId();
    String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
    ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), tenantId);
    if (appPolicy == null) {
        try {
            appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), apiTenantDomain);
            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 17 with DataLoadingException

use of org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getAPIPolicy.

@Override
public ApiPolicy getAPIPolicy(String policyName, String tenantDomain) throws DataLoadingException {
    String endPoint = APIConstants.SubscriptionValidationResources.API_POLICIES + "?policyName=" + policyName;
    ApiPolicy apiPolicy = new ApiPolicy();
    String responseString;
    if (log.isDebugEnabled()) {
        log.debug("getAPIPolicy for " + policyName + " for tenant " + tenantDomain);
    }
    try {
        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()) {
        APIPolicyList list = new Gson().fromJson(responseString, APIPolicyList.class);
        if (list.getList() != null && !list.getList().isEmpty()) {
            apiPolicy = list.getList().get(0);
        }
    }
    return apiPolicy;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) APIPolicyList(org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 18 with DataLoadingException

use of org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getApplicationPolicy.

@Override
public ApplicationPolicy getApplicationPolicy(String policyName, String tenantDomain) throws DataLoadingException {
    String endPoint = APIConstants.SubscriptionValidationResources.APPLICATION_POLICIES + "?policyName=" + policyName;
    ApplicationPolicy applicationPolicy = new ApplicationPolicy();
    if (log.isDebugEnabled()) {
        log.debug("getApplicationPolicy for " + policyName + " for tenant " + tenantDomain);
    }
    String responseString;
    try {
        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()) {
        ApplicationPolicyList list = new Gson().fromJson(responseString, ApplicationPolicyList.class);
        if (list.getList() != null && !list.getList().isEmpty()) {
            applicationPolicy = list.getList().get(0);
        }
    }
    return applicationPolicy;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) ApplicationPolicyList(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicyList) ApplicationPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 19 with DataLoadingException

use of org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException 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 20 with DataLoadingException

use of org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method invokeService.

private String invokeService(String path, String tenantDomain) throws DataLoadingException, IOException {
    String serviceURLStr = getEventHubConfigurationDto.getServiceUrl().concat(APIConstants.INTERNAL_WEB_APP_EP);
    HttpGet method = new HttpGet(serviceURLStr + path);
    URL serviceURL = new URL(serviceURLStr + path);
    byte[] credentials = getServiceCredentials(getEventHubConfigurationDto);
    int servicePort = serviceURL.getPort();
    String serviceProtocol = serviceURL.getProtocol();
    method.setHeader(APIConstants.AUTHORIZATION_HEADER_DEFAULT, APIConstants.AUTHORIZATION_BASIC + new String(credentials, StandardCharsets.UTF_8));
    if (tenantDomain != null) {
        method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
    }
    HttpClient httpClient = APIUtil.getHttpClient(servicePort, serviceProtocol);
    HttpResponse httpResponse = null;
    int retryCount = 0;
    boolean retry = false;
    do {
        try {
            httpResponse = httpClient.execute(method);
            if (HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()) {
                log.error("Could not retrieve subscriptions for tenantDomain: " + tenantDomain + ". Received response with status code " + httpResponse.getStatusLine().getStatusCode());
                throw new DataLoadingException("Error while retrieving subscription");
            }
            retry = false;
        } catch (IOException | DataLoadingException ex) {
            retryCount++;
            if (retryCount < retrievalRetries) {
                retry = true;
                log.warn("Failed retrieving " + path + " from remote endpoint: " + ex.getMessage() + ". Retrying after " + retrievalTimeoutInSeconds + " seconds.");
                try {
                    Thread.sleep(retrievalTimeoutInSeconds * 1000);
                } catch (InterruptedException e) {
                // Ignore
                }
            } else {
                throw ex;
            }
        }
    } while (retry);
    if (HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()) {
        log.error("Could not retrieve subscriptions for tenantDomain : " + tenantDomain);
        throw new DataLoadingException("Error while retrieving subscription from " + path);
    }
    String responseString = EntityUtils.toString(httpResponse.getEntity(), UTF8);
    if (log.isDebugEnabled()) {
        log.debug("Response : " + responseString);
    }
    return responseString;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)26 IOException (java.io.IOException)16 Gson (com.google.gson.Gson)15 ArrayList (java.util.ArrayList)10 ApiPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy)5 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)4 ApplicationPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy)4 SubscriptionPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)4 SubscriptionDataLoaderImpl (org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl)4 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)3 ApplicationKeyMapping (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)3 Subscription (org.wso2.carbon.apimgt.keymgt.model.entity.Subscription)3 API (org.wso2.carbon.apimgt.common.analytics.publishers.dto.API)2 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)2 APIList (org.wso2.carbon.apimgt.keymgt.model.entity.APIList)2 APIPolicyList (org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList)2 ApplicationKeyMappingList (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingList)2 ApplicationList (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationList)2 ApplicationPolicyList (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicyList)2 SubscriptionList (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionList)2