Search in sources :

Example 1 with MonetizationException

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

the class DefaultMonetizationImpl method publishMonetizationUsageRecords.

/**
 * Update info about monetization usage publish job
 *
 * @param monetizationUsagePublishInfo
 * @return boolean always return true if there is no exception
 * @throws MonetizationException
 */
@Override
public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException {
    APIAdmin apiAdmin = new APIAdminImpl();
    monetizationUsagePublishInfo.setState(APIConstants.Monetization.COMPLETED);
    monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.SUCCESSFULL);
    DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
    Date dateobj = new Date();
    // get the time in UTC format
    df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
    String currentDate = df.format(dateobj);
    long currentTimestamp = apiAdmin.getTimestamp(currentDate);
    monetizationUsagePublishInfo.setLastPublishTime(currentTimestamp);
    try {
        apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
    } catch (APIManagementException e) {
        throw new MonetizationException("Failed to update the monetization usage publish info", e);
    }
    return true;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with MonetizationException

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

the class ApisApiServiceImpl method addAPIMonetization.

/**
 * Monetize (enable or disable) for a given API
 *
 * @param apiId API ID
 * @param body request body
 * @param messageContext message context
 * @return monetizationDTO
 */
@Override
public Response addAPIMonetization(String apiId, APIMonetizationInfoDTO body, MessageContext messageContext) {
    try {
        if (StringUtils.isBlank(apiId)) {
            String errorMessage = "API ID cannot be empty or null when configuring monetization.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
        if (apiIdentifier == null) {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
        }
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        if (!APIConstants.PUBLISHED.equalsIgnoreCase(api.getStatus())) {
            String errorMessage = "API " + apiIdentifier.getApiName() + " should be in published state to configure monetization.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        // set the monetization status
        boolean monetizationEnabled = body.isEnabled();
        api.setMonetizationStatus(monetizationEnabled);
        // clear the existing properties related to monetization
        api.getMonetizationProperties().clear();
        Map<String, String> monetizationProperties = body.getProperties();
        if (MapUtils.isNotEmpty(monetizationProperties)) {
            String errorMessage = RestApiPublisherUtils.validateMonetizationProperties(monetizationProperties);
            if (!errorMessage.isEmpty()) {
                RestApiUtil.handleBadRequest(errorMessage, log);
            }
            for (Map.Entry<String, String> currentEntry : monetizationProperties.entrySet()) {
                api.addMonetizationProperty(currentEntry.getKey(), currentEntry.getValue());
            }
        }
        Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
        HashMap monetizationDataMap = new Gson().fromJson(api.getMonetizationProperties().toString(), HashMap.class);
        boolean isMonetizationStateChangeSuccessful = false;
        if (MapUtils.isEmpty(monetizationDataMap)) {
            String errorMessage = "Monetization is not configured. Monetization data is empty for " + apiIdentifier.getApiName();
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        try {
            if (monetizationEnabled) {
                isMonetizationStateChangeSuccessful = monetizationImplementation.enableMonetization(organization, api, monetizationDataMap);
            } else {
                isMonetizationStateChangeSuccessful = monetizationImplementation.disableMonetization(organization, api, monetizationDataMap);
            }
        } catch (MonetizationException e) {
            String errorMessage = "Error while changing monetization status for API ID : " + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
        if (isMonetizationStateChangeSuccessful) {
            apiProvider.configureMonetizationInAPIArtifact(api);
            APIMonetizationInfoDTO monetizationInfoDTO = APIMappingUtil.getMonetizationInfoDTO(apiId, organization);
            return Response.ok().entity(monetizationInfoDTO).build();
        } else {
            String errorMessage = "Unable to change monetization status for API : " + apiId;
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while configuring monetization for API ID : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.serverError().build();
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) Gson(com.google.gson.Gson) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) APIMonetizationInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationInfoDTO)

Example 3 with MonetizationException

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

the class ApisApiServiceImpl method getAPIMonetization.

/**
 * Get API monetization status and monetized tier to billing plan mapping
 *
 * @param apiId API ID
 * @param messageContext message context
 * @return API monetization status and monetized tier to billing plan mapping
 */
@Override
public Response getAPIMonetization(String apiId, MessageContext messageContext) {
    try {
        if (StringUtils.isBlank(apiId)) {
            String errorMessage = "API ID cannot be empty or null when retrieving monetized plans.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String uuid;
        APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId);
        if (apiRevision != null && apiRevision.getApiUUID() != null) {
            uuid = apiRevision.getApiUUID();
        } else {
            uuid = apiId;
        }
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
        Map<String, String> monetizedPoliciesToPlanMapping = monetizationImplementation.getMonetizedPoliciesToPlanMapping(api);
        APIMonetizationInfoDTO monetizationInfoDTO = APIMappingUtil.getMonetizedTiersDTO(uuid, organization, monetizedPoliciesToPlanMapping);
        return Response.ok().entity(monetizationInfoDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Failed to retrieve monetized plans for API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (MonetizationException e) {
        String errorMessage = "Failed to fetch monetized plans of API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.serverError().build();
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIMonetizationInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationInfoDTO)

Example 4 with MonetizationException

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

the class ApisApiServiceImpl method getAPIRevenue.

/**
 * Get total revenue for a given API from all its' subscriptions
 *
 * @param apiId API ID
 * @param messageContext message context
 * @return revenue data for a given API
 */
@Override
public Response getAPIRevenue(String apiId, MessageContext messageContext) {
    if (StringUtils.isBlank(apiId)) {
        String errorMessage = "API ID cannot be empty or null when getting revenue details.";
        RestApiUtil.handleBadRequest(errorMessage, log);
    }
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        if (!APIConstants.PUBLISHED.equalsIgnoreCase(api.getStatus())) {
            String errorMessage = "API " + api.getId().getName() + " should be in published state to get total revenue.";
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        Map<String, String> revenueUsageData = monetizationImplementation.getTotalRevenue(api, apiProvider);
        APIRevenueDTO apiRevenueDTO = new APIRevenueDTO();
        apiRevenueDTO.setProperties(revenueUsageData);
        return Response.ok().entity(apiRevenueDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Failed to retrieve revenue data for API ID : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (MonetizationException e) {
        String errorMessage = "Failed to get current revenue data for API ID : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIRevenueDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevenueDTO) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 5 with MonetizationException

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

the class SubscriptionsApiServiceImpl method getSubscriptionUsage.

/**
 * Get monetization usage data for a subscription
 *
 * @param subscriptionId subscription Id
 * @param messageContext message context
 * @return monetization usage data for a subscription
 */
@Override
public Response getSubscriptionUsage(String subscriptionId, MessageContext messageContext) {
    if (StringUtils.isBlank(subscriptionId)) {
        String errorMessage = "Subscription ID cannot be empty or null when getting monetization usage.";
        RestApiUtil.handleBadRequest(errorMessage, log);
    }
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        Monetization monetizationImplementation = apiProvider.getMonetizationImplClass();
        Map<String, String> billingEngineUsageData = monetizationImplementation.getCurrentUsageForSubscription(subscriptionId, apiProvider);
        if (MapUtils.isEmpty(billingEngineUsageData)) {
            String errorMessage = "Billing engine usage data was not found for subscription ID : " + subscriptionId;
            RestApiUtil.handleBadRequest(errorMessage, log);
        }
        APIMonetizationUsageDTO apiMonetizationUsageDTO = new APIMonetizationUsageDTO();
        apiMonetizationUsageDTO.setProperties(billingEngineUsageData);
        return Response.ok().entity(apiMonetizationUsageDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Failed to retrieve billing engine usage data for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (MonetizationException e) {
        String errorMessage = "Failed to get current usage for subscription ID : " + subscriptionId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIMonetizationUsageDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationUsageDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)6 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 API (org.wso2.carbon.apimgt.api.model.API)3 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)3 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)3 APIMonetizationInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMonetizationInfoDTO)2 Gson (com.google.gson.Gson)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)1