Search in sources :

Example 1 with PolicySummaryBean

use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.

the class OrganizationResourceImpl method reorderPlanPolicies.

/**
 * @see IOrganizationResource#reorderPlanPolicies(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.policies.PolicyChainBean)
 */
@Override
public void reorderPlanPolicies(String organizationId, String planId, String version, PolicyChainBean policyChain) throws OrganizationNotFoundException, PlanVersionNotFoundException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.planEdit, organizationId);
    // Make sure the plan version exists
    PlanVersionBean pvb = getPlanVersionInternal(organizationId, planId, version);
    try {
        storage.beginTx();
        List<Long> newOrder = new ArrayList<>(policyChain.getPolicies().size());
        for (PolicySummaryBean psb : policyChain.getPolicies()) {
            newOrder.add(psb.getId());
        }
        storage.reorderPolicies(PolicyType.Plan, organizationId, planId, version, newOrder);
        storage.createAuditEntry(AuditUtils.policiesReordered(pvb, PolicyType.Plan, securityContext));
        pvb.setModifiedBy(securityContext.getCurrentUser());
        pvb.setModifiedOn(new Date());
        storage.updatePlanVersion(pvb);
        storage.commitTx();
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ArrayList(java.util.ArrayList) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) Date(java.util.Date) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) NewPlanVersionBean(io.apiman.manager.api.beans.plans.NewPlanVersionBean) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean)

Example 2 with PolicySummaryBean

use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.

the class OrganizationResourceImpl method getApiPolicyChain.

/**
 * @see IOrganizationResource#getApiPolicyChain(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public PolicyChainBean getApiPolicyChain(String organizationId, String apiId, String version, String planId) throws ApiVersionNotFoundException, PlanNotFoundException {
    // No permission check is needed, because this would break All APIs UI
    // Try to get the API first - will throw an exception if not found.
    ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
    try {
        String planVersion = null;
        Set<ApiPlanBean> plans = avb.getPlans();
        if (plans != null) {
            for (ApiPlanBean apiPlanBean : plans) {
                if (apiPlanBean.getPlanId().equals(planId)) {
                    planVersion = apiPlanBean.getVersion();
                    break;
                }
            }
        }
        if (planVersion == null) {
            throw ExceptionFactory.planNotFoundException(planId);
        }
        // Hide sensitive data and set only needed data for the UI
        List<PolicySummaryBean> apiPolicies = RestHelper.hideSensitiveDataFromPolicySummaryBeanList(securityContext, query.getPolicies(organizationId, apiId, version, PolicyType.Api));
        List<PolicySummaryBean> planPolicies = RestHelper.hideSensitiveDataFromPolicySummaryBeanList(securityContext, query.getPolicies(organizationId, planId, planVersion, PolicyType.Plan));
        PolicyChainBean chain = new PolicyChainBean();
        chain.getPolicies().addAll(planPolicies);
        chain.getPolicies().addAll(apiPolicies);
        return chain;
    } catch (StorageException e) {
        throw new SystemErrorException(e);
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException) PolicyChainBean(io.apiman.manager.api.beans.policies.PolicyChainBean)

Example 3 with PolicySummaryBean

use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.

the class OrganizationResourceImpl method createApiVersion.

/**
 * @see IOrganizationResource#createApiVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.NewApiVersionBean)
 */
@Override
public ApiVersionBean createApiVersion(String organizationId, String apiId, NewApiVersionBean bean) throws ApiNotFoundException, NotAuthorizedException, InvalidVersionException, ApiVersionAlreadyExistsException {
    securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
    FieldValidator.validateVersion(bean.getVersion());
    ApiVersionBean newVersion;
    try {
        GatewaySummaryBean gateway = getSingularGateway();
        storage.beginTx();
        ApiBean api = getApiFromStorage(organizationId, apiId);
        if (storage.getApiVersion(organizationId, apiId, bean.getVersion()) != null) {
            throw ExceptionFactory.apiVersionAlreadyExistsException(apiId, bean.getVersion());
        }
        newVersion = createApiVersionInternal(bean, api, gateway);
        storage.commitTx();
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
    if (bean.isClone() && bean.getCloneVersion() != null) {
        try {
            ApiVersionBean cloneSource = getApiVersion(organizationId, apiId, bean.getCloneVersion());
            // Clone primary attributes of the API version unless those attributes
            // were included in the NewApiVersionBean.  In other words, information
            // sent as part of the "create version" payload take precedence over the
            // cloned attributes.
            UpdateApiVersionBean updatedApi = new UpdateApiVersionBean();
            if (bean.getEndpoint() == null) {
                updatedApi.setEndpoint(cloneSource.getEndpoint());
            }
            if (bean.getEndpointType() == null) {
                updatedApi.setEndpointType(cloneSource.getEndpointType());
            }
            if (bean.getEndpointContentType() == null) {
                updatedApi.setEndpointContentType(cloneSource.getEndpointContentType());
            }
            updatedApi.setEndpointProperties(cloneSource.getEndpointProperties());
            updatedApi.setGateways(cloneSource.getGateways());
            if (bean.getPlans() == null) {
                updatedApi.setPlans(cloneSource.getPlans());
            }
            if (bean.getPublicAPI() == null) {
                updatedApi.setPublicAPI(cloneSource.isPublicAPI());
            }
            if (bean.getParsePayload() == null) {
                updatedApi.setParsePayload(bean.getParsePayload());
            }
            newVersion = updateApiVersion(organizationId, apiId, bean.getVersion(), updatedApi);
            if (bean.getDefinitionUrl() == null) {
                // Clone the API definition document
                InputStream definition = null;
                try {
                    Response response = getApiDefinition(organizationId, apiId, bean.getCloneVersion());
                    definition = (InputStream) response.getEntity();
                    storeApiDefinition(organizationId, apiId, newVersion.getVersion(), cloneSource.getDefinitionType(), definition, cloneSource.getDefinitionUrl());
                } catch (ApiDefinitionNotFoundException svnfe) {
                // This is ok - it just means the API doesn't have one, so do nothing.
                } catch (Exception sdnfe) {
                    // $NON-NLS-1$
                    log.error("Unable to create response", sdnfe);
                } finally {
                    IOUtils.closeQuietly(definition);
                }
            }
            // Clone all API policies
            List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, bean.getCloneVersion());
            for (PolicySummaryBean policySummary : policies) {
                PolicyBean policy = getApiPolicy(organizationId, apiId, bean.getCloneVersion(), policySummary.getId());
                NewPolicyBean npb = new NewPolicyBean();
                npb.setDefinitionId(policy.getDefinition().getId());
                npb.setConfiguration(policy.getConfiguration());
                createApiPolicy(organizationId, apiId, newVersion.getVersion(), npb);
            }
        } catch (Exception e) {
            // TODO it's ok if the clone fails - we did our best
            if (e != null) {
                Throwable t = e;
                e = (Exception) t;
            }
        }
    }
    return newVersion;
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) GatewaySummaryBean(io.apiman.manager.api.beans.summary.GatewaySummaryBean) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) UpdateApiBean(io.apiman.manager.api.beans.apis.UpdateApiBean) ApiBean(io.apiman.manager.api.beans.apis.ApiBean) NewApiBean(io.apiman.manager.api.beans.apis.NewApiBean) ClientUsagePerApiBean(io.apiman.manager.api.beans.metrics.ClientUsagePerApiBean) Response(javax.ws.rs.core.Response) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) UpdateApiVersionBean(io.apiman.manager.api.beans.apis.UpdateApiVersionBean) NewApiVersionBean(io.apiman.manager.api.beans.apis.NewApiVersionBean)

Example 4 with PolicySummaryBean

use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.

the class OrganizationResourceImpl method createClientVersion.

/**
 * @see IOrganizationResource#createClientVersion(java.lang.String, java.lang.String, io.apiman.manager.api.beans.clients.NewClientVersionBean)
 */
@Override
public ClientVersionBean createClientVersion(String organizationId, String clientId, NewClientVersionBean bean) throws ClientNotFoundException, NotAuthorizedException, InvalidVersionException, ClientVersionAlreadyExistsException {
    securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
    FieldValidator.validateVersion(bean.getVersion());
    ClientVersionBean newVersion;
    try {
        storage.beginTx();
        ClientBean client = getClientFromStorage(organizationId, clientId);
        if (storage.getClientVersion(organizationId, clientId, bean.getVersion()) != null) {
            throw ExceptionFactory.clientVersionAlreadyExistsException(clientId, bean.getVersion());
        }
        newVersion = createClientVersionInternal(bean, client);
        storage.commitTx();
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        throw new SystemErrorException(e);
    }
    if (bean.isClone() && bean.getCloneVersion() != null) {
        try {
            List<ContractSummaryBean> contracts = getClientVersionContracts(organizationId, clientId, bean.getCloneVersion());
            for (ContractSummaryBean contract : contracts) {
                NewContractBean ncb = new NewContractBean();
                ncb.setPlanId(contract.getPlanId());
                ncb.setApiId(contract.getApiId());
                ncb.setApiOrgId(contract.getApiOrganizationId());
                ncb.setApiVersion(contract.getApiVersion());
                createContract(organizationId, clientId, newVersion.getVersion(), ncb);
            }
            List<PolicySummaryBean> policies = listClientPolicies(organizationId, clientId, bean.getCloneVersion());
            for (PolicySummaryBean policySummary : policies) {
                PolicyBean policy = getClientPolicy(organizationId, clientId, bean.getCloneVersion(), policySummary.getId());
                NewPolicyBean npb = new NewPolicyBean();
                npb.setDefinitionId(policy.getDefinition().getId());
                npb.setConfiguration(policy.getConfiguration());
                createClientPolicy(organizationId, clientId, newVersion.getVersion(), npb);
            }
        } catch (Exception e) {
        // TODO it's ok if the clone fails - we did our best
        }
    }
    return newVersion;
}
Also used : NewClientVersionBean(io.apiman.manager.api.beans.clients.NewClientVersionBean) ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) UpdateClientBean(io.apiman.manager.api.beans.clients.UpdateClientBean) UsagePerClientBean(io.apiman.manager.api.beans.metrics.UsagePerClientBean) ResponseStatsPerClientBean(io.apiman.manager.api.beans.metrics.ResponseStatsPerClientBean) ClientBean(io.apiman.manager.api.beans.clients.ClientBean) NewClientBean(io.apiman.manager.api.beans.clients.NewClientBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) NewPolicyBean(io.apiman.manager.api.beans.policies.NewPolicyBean) UpdatePolicyBean(io.apiman.manager.api.beans.policies.UpdatePolicyBean) ContractSummaryBean(io.apiman.manager.api.beans.summary.ContractSummaryBean) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) ApiVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiVersionAlreadyExistsException) GatewayNotFoundException(io.apiman.manager.api.rest.exceptions.GatewayNotFoundException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) OrganizationAlreadyExistsException(io.apiman.manager.api.rest.exceptions.OrganizationAlreadyExistsException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) PlanAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanAlreadyExistsException) ApiAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) UserNotFoundException(io.apiman.manager.api.rest.exceptions.UserNotFoundException) GatewayAuthenticationException(io.apiman.manager.api.gateway.GatewayAuthenticationException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) PlanVersionNotFoundException(io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException) RoleNotFoundException(io.apiman.manager.api.rest.exceptions.RoleNotFoundException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) IOException(java.io.IOException) InvalidApiStatusException(io.apiman.manager.api.rest.exceptions.InvalidApiStatusException) ApiNotFoundException(io.apiman.manager.api.rest.exceptions.ApiNotFoundException) ContractAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ContractAlreadyExistsException) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) ApiVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) InvalidPlanStatusException(io.apiman.manager.api.rest.exceptions.InvalidPlanStatusException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) ContractNotFoundException(io.apiman.manager.api.rest.exceptions.ContractNotFoundException) InvalidParameterException(io.apiman.manager.api.rest.exceptions.InvalidParameterException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) PlanNotFoundException(io.apiman.manager.api.rest.exceptions.PlanNotFoundException) InvalidMetricCriteriaException(io.apiman.manager.api.rest.exceptions.InvalidMetricCriteriaException) MalformedURLException(java.net.MalformedURLException) PlanVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.PlanVersionAlreadyExistsException) PolicyDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyDefinitionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException) ApiDefinitionNotFoundException(io.apiman.manager.api.rest.exceptions.ApiDefinitionNotFoundException)

Example 5 with PolicySummaryBean

use of io.apiman.manager.api.beans.summary.PolicySummaryBean in project apiman by apiman.

the class ActionResourceImpl method publishApi.

/**
 * Publishes an API to the gateway.
 * @param action
 */
private void publishApi(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 Ready.
    if (!versionBean.isPublicAPI() && versionBean.getStatus() != ApiStatus.Ready) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
    }
    if (versionBean.isPublicAPI()) {
        if (versionBean.getStatus() == ApiStatus.Retired || versionBean.getStatus() == ApiStatus.Created) {
            // $NON-NLS-1$
            throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
        }
        if (versionBean.getStatus() == ApiStatus.Published) {
            Date modOn = versionBean.getModifiedOn();
            Date publishedOn = versionBean.getPublishedOn();
            int c = modOn.compareTo(publishedOn);
            if (c <= 0) {
                // $NON-NLS-1$
                throw ExceptionFactory.actionException(Messages.i18n.format("ApiRePublishNotRequired"));
            }
        }
    }
    Api gatewayApi = new Api();
    gatewayApi.setEndpoint(versionBean.getEndpoint());
    gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
    if (versionBean.getEndpointContentType() != null) {
        gatewayApi.setEndpointContentType(versionBean.getEndpointContentType().toString());
    }
    gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
    gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
    gatewayApi.setApiId(versionBean.getApi().getId());
    gatewayApi.setVersion(versionBean.getVersion());
    gatewayApi.setPublicAPI(versionBean.isPublicAPI());
    gatewayApi.setParsePayload(versionBean.isParsePayload());
    gatewayApi.setKeysStrippingDisabled(versionBean.getDisableKeysStrip());
    boolean hasTx = false;
    try {
        if (versionBean.isPublicAPI()) {
            List<Policy> policiesToPublish = new ArrayList<>();
            List<PolicySummaryBean> apiPolicies = query.getPolicies(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), PolicyType.Api);
            storage.beginTx();
            hasTx = true;
            for (PolicySummaryBean policySummaryBean : apiPolicies) {
                PolicyBean apiPolicy = storage.getPolicy(PolicyType.Api, action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), policySummaryBean.getId());
                Policy policyToPublish = new Policy();
                policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
                policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
                policiesToPublish.add(policyToPublish);
            }
            gatewayApi.setApiPolicies(policiesToPublish);
        }
    } catch (StorageException e) {
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
    } finally {
        if (hasTx) {
            storage.rollbackTx();
        }
    }
    // Publish the API to 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.publishApi(gatewayApi);
            gatewayLink.close();
        }
        versionBean.setStatus(ApiStatus.Published);
        versionBean.setPublishedOn(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.setNumPublished(1);
        } else {
            api.setNumPublished(api.getNumPublished() + 1);
        }
        storage.updateApi(api);
        storage.updateApiVersion(versionBean);
        storage.createAuditEntry(AuditUtils.apiPublished(versionBean, securityContext));
        storage.commitTx();
    } catch (PublishingException e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
    } catch (Exception e) {
        storage.rollbackTx();
        // $NON-NLS-1$
        throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
    }
    log.debug(// $NON-NLS-1$
    String.format(// $NON-NLS-1$
    "Successfully published API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
Also used : Policy(io.apiman.gateway.engine.beans.Policy) PolicySummaryBean(io.apiman.manager.api.beans.summary.PolicySummaryBean) PolicyBean(io.apiman.manager.api.beans.policies.PolicyBean) ArrayList(java.util.ArrayList) Date(java.util.Date) IGatewayLink(io.apiman.manager.api.gateway.IGatewayLink) 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) 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) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Aggregations

PolicySummaryBean (io.apiman.manager.api.beans.summary.PolicySummaryBean)24 StorageException (io.apiman.manager.api.core.exceptions.StorageException)17 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)14 ArrayList (java.util.ArrayList)14 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)13 OrganizationNotFoundException (io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)11 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)11 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)10 ApiNotFoundException (io.apiman.manager.api.rest.exceptions.ApiNotFoundException)10 ApiVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ApiVersionNotFoundException)10 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)10 GatewayNotFoundException (io.apiman.manager.api.rest.exceptions.GatewayNotFoundException)10 PlanNotFoundException (io.apiman.manager.api.rest.exceptions.PlanNotFoundException)10 PlanVersionNotFoundException (io.apiman.manager.api.rest.exceptions.PlanVersionNotFoundException)10 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)9 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)9 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)9 InvalidClientStatusException (io.apiman.manager.api.rest.exceptions.InvalidClientStatusException)9 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)9 InvalidVersionException (io.apiman.manager.api.rest.exceptions.InvalidVersionException)9