Search in sources :

Example 46 with APIAdmin

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

the class EnvironmentsApiServiceImpl method environmentsEnvironmentIdDelete.

/**
 * Delete gateway envirionment
 *
 * @param environmentId environment ID
 * @param messageContext message context
 * @return 200 with empty response body
 * @throws APIManagementException if failed to delete
 */
public Response environmentsEnvironmentIdDelete(String environmentId, MessageContext messageContext) throws APIManagementException {
    APIAdmin apiAdmin = new APIAdminImpl();
    // String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    apiAdmin.deleteEnvironment(organization, environmentId);
    return Response.ok().build();
}
Also used : APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 47 with APIAdmin

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

the class MonetizationApiServiceImpl method monetizationPublishUsagePost.

/**
 * Run the monetization usage publish job
 *
 * @return Response of the server
 */
@Override
public Response monetizationPublishUsagePost(MessageContext messageContext) {
    MonetizationUsagePublishInfo monetizationUsagePublishInfo;
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        monetizationUsagePublishInfo = apiAdmin.getMonetizationUsagePublishInfo();
        if (monetizationUsagePublishInfo == null) {
            monetizationUsagePublishInfo = new MonetizationUsagePublishInfo();
            monetizationUsagePublishInfo.setId(APIConstants.Monetization.USAGE_PUBLISHER_JOB_NAME);
            monetizationUsagePublishInfo.setState(APIConstants.Monetization.INITIATED);
            monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.INPROGRESS);
            // read the number of days to reduce from the current time to derive the from / last publish time
            // when there is no record of the last publish time
            APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
            String gap = configuration.getFirstProperty(APIConstants.Monetization.FROM_TIME_CONFIGURATION_PROPERTY);
            // if the from time / last publish time is not set , set it to default
            if (gap == null) {
                gap = APIConstants.Monetization.USAGE_PUBLISH_DEFAULT_TIME_GAP_IN_DAYS;
            }
            DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
            df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
            Calendar cal = Calendar.getInstance();
            Date currentDate = cal.getTime();
            String formattedCurrentDate = df.format(currentDate);
            long currentTimestamp = apiAdmin.getTimestamp(formattedCurrentDate);
            monetizationUsagePublishInfo.setStartedTime(currentTimestamp);
            // reducing the number of days set to get the last published time when there is no record of
            // the last published time
            cal.add(Calendar.DATE, -Integer.parseInt(gap));
            Date fromDate = cal.getTime();
            String formattedFromDate = df.format(fromDate);
            long lastPublishedTimeStamp = apiAdmin.getTimestamp(formattedFromDate);
            monetizationUsagePublishInfo.setLastPublishTime(lastPublishedTimeStamp);
            apiAdmin.addMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
        }
        if (!monetizationUsagePublishInfo.getState().equals(APIConstants.Monetization.RUNNING)) {
            executor = Executors.newSingleThreadExecutor();
            MonetizationUsagePublishAgent agent = new MonetizationUsagePublishAgent(monetizationUsagePublishInfo);
            executor.execute(agent);
            return Response.accepted().entity(MonetizationAPIMappinUtil.fromStatusToDTO("Request Accepted", "Server is running the usage publisher")).build();
        } else {
            return Response.serverError().entity(MonetizationAPIMappinUtil.fromStatusToDTO("Server could not " + "accept the request", "A job is already running")).build();
        }
    } catch (APIManagementException ex) {
        RestApiUtil.handleInternalServerError("Could not add or derive monetization usage publish info", ex, log);
    }
    return null;
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) MonetizationUsagePublishAgent(org.wso2.carbon.apimgt.impl.monetization.MonetizationUsagePublishAgent) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationUsagePublishInfo(org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)46 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)31 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 BeforeTest (org.testng.annotations.BeforeTest)8 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)8 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)6 ArrayList (java.util.ArrayList)5 Schema (org.everit.json.schema.Schema)5 File (java.io.File)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 BotDetectionData (org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData)4 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)4 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)4 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)4 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)4 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)4 Gson (com.google.gson.Gson)3