Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#getAPIProduct()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#getAPIProduct() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: APIMappingUtil.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public static APIMonetizationInfoDTO getMonetizationInfoDTO(APIProductIdentifier apiProductIdentifier)
        throws APIManagementException {

    APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
    APIProduct apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
    APIMonetizationInfoDTO apiMonetizationInfoDTO = new APIMonetizationInfoDTO();
    //set the information related to monetization to the DTO
    apiMonetizationInfoDTO.setEnabled(apiProduct.getMonetizationStatus());
    Map<String, String> monetizationPropertiesMap = new HashMap<>();
    if (apiProduct.getMonetizationProperties() != null) {
        JSONObject monetizationProperties = apiProduct.getMonetizationProperties();
        for (Object propertyKey : monetizationProperties.keySet()) {
            String key = (String) propertyKey;
            monetizationPropertiesMap.put(key, (String) monetizationProperties.get(key));
        }
    }
    apiMonetizationInfoDTO.setProperties(monetizationPropertiesMap);
    return apiMonetizationInfoDTO;
}
 
Example 2
Source File: APIMappingUtil.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public static APIMonetizationInfoDTO getMonetizedTiersDTO(APIProductIdentifier apiProductIdentifier,
                                                          Map<String, String> monetizedPoliciesToPlanMapping)
        throws APIManagementException {

    APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
    APIProduct apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
    APIMonetizationInfoDTO apiMonetizationInfoDTO = new APIMonetizationInfoDTO();
    apiMonetizationInfoDTO.setEnabled(apiProduct.getMonetizationStatus());
    apiMonetizationInfoDTO.setProperties(monetizedPoliciesToPlanMapping);
    return apiMonetizationInfoDTO;
}
 
Example 3
Source File: SubscriptionsApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Blocks a subscription
 *
 * @param subscriptionId Subscription identifier
 * @param blockState block state; either BLOCKED or PROD_ONLY_BLOCKED
 * @param ifMatch If-Match header value
 * @return 200 response and the updated subscription if subscription block is successful
 */
public Response subscriptionsBlockSubscriptionPost(String subscriptionId, String blockState, String ifMatch,
        MessageContext messageContext) {
    String username = RestApiUtil.getLoggedInUsername();
    try {
        APIProvider apiProvider = RestApiUtil.getProvider(username);
        // validates the subscriptionId if it exists
        SubscribedAPI currentSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);

        if (currentSubscription == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION, subscriptionId, log);
        }

        Application subscribedApp = currentSubscription.getApplication();
        String applicationTokenType = "OAUTH";
        if (subscribedApp != null) {
            applicationTokenType = subscribedApp.getTokenType();
        }

        //in case of a JWT type application add a subscription blocking condition as well.
        if (APIConstants.APPLICATION_TOKEN_TYPE_JWT.equals(applicationTokenType)) {
            Identifier apiId = currentSubscription.getApiId();
            if (apiId == null) {
                apiId = currentSubscription.getProductId();
            }

            String apiContext = "";
            String apiVersion = "";
            if (apiId instanceof APIIdentifier) {
                apiContext = apiProvider.getAPIContext((APIIdentifier) apiId);
                apiVersion = apiId.getVersion();
            } else if (apiId instanceof APIProductIdentifier) {
                APIProduct product = apiProvider.getAPIProduct((APIProductIdentifier) apiId);
                apiContext = product.getContext();
                //until product versioning is supported, we will be adding default api product version to the
                // blacklist condition key
                apiVersion = APIConstants.API_PRODUCT_VERSION;
            }

            String appId = subscribedApp.getOwner() + "-" + subscribedApp.getName();
            String substatus = currentSubscription.getSubStatus();

            String productionBlockConditionKey =
                    apiContext + ":" + apiVersion + ":" + appId + ":" + APIConstants.API_KEY_TYPE_PRODUCTION;
            String sandboxBlockConditionKey =
                    apiContext + ":" + apiVersion + ":" + appId + ":" + APIConstants.API_KEY_TYPE_SANDBOX;

            //delete existing block conditions
            apiProvider.deleteSubscriptionBlockCondition(productionBlockConditionKey);
            apiProvider.deleteSubscriptionBlockCondition(sandboxBlockConditionKey);

            if (APIConstants.SubscriptionStatus.BLOCKED.equals(substatus)) {
                /*In case all subscriptions blocked, add block conditions for both sandbox and production
                key types*/
                apiProvider.addBlockCondition(APIConstants.BLOCKING_CONDITIONS_SUBSCRIPTION,
                        productionBlockConditionKey);
                apiProvider
                        .addBlockCondition(APIConstants.BLOCKING_CONDITIONS_SUBSCRIPTION, sandboxBlockConditionKey);
            } else {
                /*In case production only blocked add a blocking condition only for production type*/
                apiProvider.addBlockCondition(APIConstants.BLOCKING_CONDITIONS_SUBSCRIPTION,
                        productionBlockConditionKey);
            }
        }

        SubscribedAPI subscribedAPI = new SubscribedAPI(subscriptionId);
        subscribedAPI.setSubStatus(blockState);
        apiProvider.updateSubscription(subscribedAPI);

        SubscribedAPI updatedSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
        SubscriptionDTO subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(updatedSubscription);

        return Response.ok().entity(subscriptionDTO).build();
    } catch (APIManagementException e) {
        String msg = "Error while blocking the subscription " + subscriptionId;
        RestApiUtil.handleInternalServerError(msg, e, log);
    }

    return null;
}
 
Example 4
Source File: SubscriptionsApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * 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 subscriptionsUnblockSubscriptionPost(String subscriptionId, String ifMatch,
        MessageContext messageContext) {
    String username = RestApiUtil.getLoggedInUsername();
    try {
        APIProvider apiProvider = RestApiUtil.getProvider(username);

        // validates the subscriptionId if it exists
        SubscribedAPI currentSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
        if (currentSubscription == null) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION, subscriptionId, log);
        }

        Application subscribedApp = currentSubscription.getApplication();
        String applicationTokenType = "OAUTH";
        if (subscribedApp != null) {
            applicationTokenType = subscribedApp.getTokenType();
        }

        //in case of a JWT type application remove the subscription blocking conditions if exist
        if (APIConstants.APPLICATION_TOKEN_TYPE_JWT.equals(applicationTokenType)) {
            Identifier apiId = currentSubscription.getApiId();
            if (apiId == null) {
                apiId = currentSubscription.getProductId();
            }

            String apiContext = "";
            String apiVersion = "";
            if (apiId instanceof APIIdentifier) {
                apiContext = apiProvider.getAPIContext((APIIdentifier) apiId);
                apiVersion = apiId.getVersion();
            } else if (apiId instanceof  APIProductIdentifier) {
                APIProduct product = apiProvider.getAPIProduct((APIProductIdentifier) apiId);
                apiContext = product.getContext();
                apiVersion = APIConstants.API_PRODUCT_VERSION;
            }

            String appId = subscribedApp.getOwner() + "-" + subscribedApp.getName();

            //delete existing block conditions
            String productionBlockConditionKey =
                    apiContext + ":" + apiVersion + ":" + appId + ":" + APIConstants.API_KEY_TYPE_PRODUCTION;
            String sandboxBlockConditionKey =
                    apiContext + ":" + apiVersion + ":" + appId + ":" + APIConstants.API_KEY_TYPE_SANDBOX;
            apiProvider.deleteSubscriptionBlockCondition(productionBlockConditionKey);
            apiProvider.deleteSubscriptionBlockCondition(sandboxBlockConditionKey);
        }

        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;
}
 
Example 5
Source File: APIProductExportUtil.java    From carbon-apimgt with Apache License 2.0 3 votes vote down vote up
/**
 * Exports an API Product from API Manager for a given API Product ID. Meta information, API icon, documentation, client certificates
 * and dependent APIs are exported. This service generates a zipped archive which contains all the above mentioned
 * resources for a given API Product.
 *
 * @param apiProvider           API provider
 * @param apiProductIdentifier  ID of the requesting API Product
 * @param userName              User name of the user
 * @param preserveStatus        Preserve API status on export
 * @return Zipped file containing exported API Product
 * @throws APIImportExportException If an error occurs while exporting the API Product and creating the archive
 * @throws APIManagementException If an error occurs while retrieving the API Product
 */
public static File exportApiProduct(APIProvider apiProvider, APIProductIdentifier apiProductIdentifier, String userName,
                             ExportFormat exportFormat, Boolean preserveStatus)
        throws APIImportExportException, APIManagementException {
    APIProduct apiProduct;
    APIImportExportManager apiImportExportManager;
    boolean isStatusPreserved = preserveStatus == null || preserveStatus;
    apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
    ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(apiProduct);
    apiImportExportManager = new APIImportExportManager(apiProvider, userName);
    return apiImportExportManager.exportAPIOrAPIProductArchive(apiTypeWrapper, isStatusPreserved, exportFormat);
}