Search in sources :

Example 41 with Api

use of io.apiman.gateway.engine.beans.Api in project apiman by apiman.

the class ActionResourceImpl method retireApi.

/**
 * Retires an API that is currently published to the Gateway.
 * @param action
 */
private void retireApi(ActionBean action) throws ActionException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.apiAdmin, action.getOrganizationId());
    ApiVersionBean versionBean;
    try {
        versionBean = orgs.getApiVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
    } catch (ApiVersionNotFoundException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
    }
    // Validate that it's ok to perform this action - API must be Published.
    if (versionBean.getStatus() != ApiStatus.Published) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
    }
    Api gatewayApi = new Api();
    gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
    gatewayApi.setApiId(versionBean.getApi().getId());
    gatewayApi.setVersion(versionBean.getVersion());
    // Retire the API from all relevant gateways
    try {
        storage.beginTx();
        Set<ApiGatewayBean> gateways = versionBean.getGateways();
        if (gateways == null) {
            // $NON-NLS-1$
            throw new PublishingException("No gateways specified for API!");
        }
        for (ApiGatewayBean apiGatewayBean : gateways) {
            IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
            gatewayLink.retireApi(gatewayApi);
            gatewayLink.close();
        }
        versionBean.setStatus(ApiStatus.Retired);
        versionBean.setRetiredOn(new Date());
        ApiBean api = storage.getApi(action.getOrganizationId(), action.getEntityId());
        if (api == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new PublishingException("Error: could not find API - " + action.getOrganizationId() + "=>" + action.getEntityId());
        }
        if (api.getNumPublished() == null || api.getNumPublished() == 0) {
            api.setNumPublished(0);
        } else {
            api.setNumPublished(api.getNumPublished() - 1);
        }
        storage.updateApi(api);
        storage.updateApiVersion(versionBean);
        storage.createAuditEntry(AuditUtils.apiRetired(versionBean, securityContext));
        storage.commitTx();
    } catch (PublishingException e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RetireError"), e);
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("RetireError"), e);
    }
    log.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully retired API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
Also used : ApiBean(io.apiman.manager.api.beans.apis.ApiBean) ApiGatewayBean(io.apiman.manager.api.beans.apis.ApiGatewayBean) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) Api(io.apiman.gateway.engine.beans.Api) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) Date(java.util.Date) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) ActionException(io.apiman.manager.api.rest.exceptions.ActionException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) PublishingException(io.apiman.gateway.engine.beans.exceptions.PublishingException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)

Aggregations

Api (io.apiman.gateway.engine.beans.Api)41 Client (io.apiman.gateway.engine.beans.Client)16 ApiContract (io.apiman.gateway.engine.beans.ApiContract)11 Contract (io.apiman.gateway.engine.beans.Contract)10 Policy (io.apiman.gateway.engine.beans.Policy)10 PublishingException (io.apiman.gateway.engine.beans.exceptions.PublishingException)10 ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)8 ApiRetiredException (io.apiman.gateway.engine.beans.exceptions.ApiRetiredException)8 NoContractFoundException (io.apiman.gateway.engine.beans.exceptions.NoContractFoundException)8 ClientNotFoundException (io.apiman.gateway.engine.beans.exceptions.ClientNotFoundException)7 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)7 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)7 StorageException (io.apiman.manager.api.core.exceptions.StorageException)7 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)7 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 RegistrationException (io.apiman.gateway.engine.beans.exceptions.RegistrationException)6 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)6 ActionException (io.apiman.manager.api.rest.exceptions.ActionException)6 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)6