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;
}
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();
}
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();
}
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;
}
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;
}
Aggregations