Search in sources :

Example 26 with SubscribedAPI

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

the class SubscriptionsApiServiceImpl method unBlockSubscription.

/**
 * Unblocks a subscription
 *
 * @param subscriptionId subscription identifier
 * @param ifMatch If-Match header value
 * @return 200 response and the updated subscription if subscription block is successful
 */
public Response unBlockSubscription(String subscriptionId, String ifMatch, MessageContext messageContext) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
        // validates the subscriptionId if it exists
        SubscribedAPI currentSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
        if (currentSubscription == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION, subscriptionId, log);
        }
        SubscribedAPI subscribedAPI = new SubscribedAPI(subscriptionId);
        subscribedAPI.setSubStatus(APIConstants.SubscriptionStatus.UNBLOCKED);
        apiProvider.updateSubscription(subscribedAPI);
        SubscribedAPI updatedSubscribedAPI = apiProvider.getSubscriptionByUUID(subscriptionId);
        SubscriptionDTO subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(updatedSubscribedAPI);
        return Response.ok().entity(subscriptionDTO).build();
    } catch (APIManagementException e) {
        String msg = "Error while unblocking the subscription " + subscriptionId;
        RestApiUtil.handleInternalServerError(msg, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO)

Example 27 with SubscribedAPI

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

the class SubscriptionsApiServiceImpl method getSubscriptions.

/**
 * Retrieves all subscriptions or retrieves subscriptions for a given API Id
 *
 * @param apiId API identifier
 * @param limit max number of objects returns
 * @param offset starting index
 * @param ifNoneMatch If-None-Match header value
 * @return Response object containing resulted subscriptions
 */
public Response getSubscriptions(String apiId, Integer limit, Integer offset, String ifNoneMatch, String query, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset if they are null
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        SubscriptionListDTO subscriptionListDTO;
        List<SubscribedAPI> apiUsages;
        if (apiId != null) {
            String apiUuid = apiId;
            APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                apiUuid = apiRevision.getApiUUID();
            }
            apiUsages = apiProvider.getAPIUsageByAPIId(apiUuid, organization);
        } else {
            UserApplicationAPIUsage[] allApiUsage = apiProvider.getAllAPIUsageByProvider(username);
            apiUsages = SubscriptionMappingUtil.fromUserApplicationAPIUsageArrayToSubscribedAPIList(allApiUsage);
        }
        if (query != null && !query.isEmpty()) {
            SubscriptionListDTO filteredSubscriptionList = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, query);
            subscriptionListDTO = SubscriptionMappingUtil.getPaginatedSubscriptions(filteredSubscriptionList, limit, offset);
            SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, filteredSubscriptionList.getCount());
        } else {
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, limit, offset);
            SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, apiUsages.size());
        }
        return Response.ok().entity(subscriptionListDTO).build();
    } catch (APIManagementException e) {
        // existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            String msg = "Error while retrieving subscriptions of API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    return null;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)

Example 28 with SubscribedAPI

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

the class SolaceApplicationNotifier method renameSolaceApplication.

/**
 * Rename applications on the Solace broker
 *
 * @param event ApplicationEvent to rename Solace applications
 * @throws NotifierException if error occurs when renaming applications on the Solace broker
 */
private void renameSolaceApplication(ApplicationEvent event) throws NotifierException {
    // get list of subscribed APIs in the application
    Subscriber subscriber = new Subscriber(event.getSubscriber());
    try {
        Application application = apiMgtDAO.getApplicationByUUID(event.getUuid());
        Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
        Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
        boolean isContainsSolaceApis = false;
        String organizationNameOfSolaceDeployment = null;
        labelOne: // Check whether the application needs to be updated has a Solace API subscription
        for (SubscribedAPI api : subscriptions) {
            List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
            for (APIRevisionDeployment deployment : deployments) {
                if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
                    if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
                        isContainsSolaceApis = true;
                        organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                        break labelOne;
                    }
                }
            }
        }
        // Renaming application using Solace Admin Apis
        if (isContainsSolaceApis) {
            SolaceNotifierUtils.renameSolaceApplication(organizationNameOfSolaceDeployment, application);
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Environment(org.wso2.carbon.apimgt.api.model.Environment) ArrayList(java.util.ArrayList) List(java.util.List) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) Application(org.wso2.carbon.apimgt.api.model.Application) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 29 with SubscribedAPI

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

the class SolaceApplicationNotifier method removeSolaceApplication.

/**
 * Remove applications from Solace broker
 *
 * @param event ApplicationEvent to remove Solace applications
 * @throws NotifierException if error occurs when removing applications from Solace broker
 */
private void removeSolaceApplication(ApplicationEvent event) throws NotifierException {
    // get list of subscribed APIs in the application
    Subscriber subscriber = new Subscriber(event.getSubscriber());
    try {
        Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
        List<SubscribedAPI> subscribedApiList = new ArrayList<>(subscriptions);
        boolean hasSubscribedAPIDeployedInSolace = false;
        String organizationNameOfSolaceDeployment = null;
        Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
        labelOne: for (SubscribedAPI api : subscribedApiList) {
            List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUUID());
            for (APIRevisionDeployment deployment : deployments) {
                if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
                    if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
                        hasSubscribedAPIDeployedInSolace = true;
                        organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                        break labelOne;
                    }
                }
            }
        }
        boolean applicationFoundInSolaceBroker = false;
        if (hasSubscribedAPIDeployedInSolace) {
            SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
            // check existence of application in Solace Broker
            CloseableHttpResponse response1 = solaceAdminApis.applicationGet(organizationNameOfSolaceDeployment, event.getUuid(), "default");
            if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                applicationFoundInSolaceBroker = true;
                if (log.isDebugEnabled()) {
                    log.info("Found application '" + event.getApplicationName() + "' in Solace broker");
                    log.info("Waiting until application removing workflow gets finished");
                }
            } else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                throw new NotifierException("Application '" + event.getApplicationName() + "' cannot be found in " + "Solace Broker");
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while searching for application '" + event.getApplicationName() + "'" + " in Solace Broker. : " + response1.getStatusLine().toString());
                }
                throw new NotifierException("Error while searching for application '" + event.getApplicationName() + "' in Solace Broker");
            }
        }
        if (applicationFoundInSolaceBroker) {
            log.info("Deleting application from Solace Broker");
            // delete application from solace
            SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
            CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(organizationNameOfSolaceDeployment, event.getUuid());
            if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
                log.info("Successfully deleted application '" + event.getApplicationName() + "' " + "in Solace Broker");
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while deleting application " + event.getApplicationName() + " in Solace. :" + response2.getStatusLine().toString());
                }
                throw new NotifierException("Error while deleting application '" + event.getApplicationName() + "' in Solace");
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Environment(org.wso2.carbon.apimgt.api.model.Environment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with SubscribedAPI

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

the class AdditionalSubscriptionInfoMappingUtil method fromAdditionalSubscriptionInfoListToDTO.

/**
 * Converts a List object of SubscribedAPIs into a DTO
 *
 * @param subscriptions a list of SubscribedAPI objects
 * @param limit         max number of objects returned
 * @param offset        starting index
 * @param organization  identifier of the organization
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 * @throws APIManagementException if error occurred when creating AdditionalSubscriptionInfoListDTO
 */
public static AdditionalSubscriptionInfoListDTO fromAdditionalSubscriptionInfoListToDTO(List<SubscribedAPI> subscriptions, Integer limit, Integer offset, String organization) throws APIManagementException {
    AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO = new AdditionalSubscriptionInfoListDTO();
    List<AdditionalSubscriptionInfoDTO> additionalSubscriptionInfoDTOs = additionalSubscriptionInfoListDTO.getList();
    if (additionalSubscriptionInfoDTOs == null) {
        additionalSubscriptionInfoDTOs = new ArrayList<>();
        additionalSubscriptionInfoListDTO.setList(additionalSubscriptionInfoDTOs);
    }
    // Identifying the proper start and end indexes
    int size = subscriptions.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        try {
            SubscribedAPI subscription = subscriptions.get(i);
            additionalSubscriptionInfoDTOs.add(fromAdditionalSubscriptionInfoToDTO(subscription, organization));
        } catch (APIManagementException e) {
            log.error("Error while obtaining additional info of subscriptions", e);
        }
    }
    // Set count for list
    additionalSubscriptionInfoListDTO.setCount(additionalSubscriptionInfoDTOs.size());
    return additionalSubscriptionInfoListDTO;
}
Also used : AdditionalSubscriptionInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoDTO) AdditionalSubscriptionInfoListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoListDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 Application (org.wso2.carbon.apimgt.api.model.Application)28 Tier (org.wso2.carbon.apimgt.api.model.Tier)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ArrayList (java.util.ArrayList)12 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)12 HashSet (java.util.HashSet)11 TreeMap (java.util.TreeMap)11 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)9 JSONObject (net.minidev.json.JSONObject)9 API (org.wso2.carbon.apimgt.api.model.API)9