Search in sources :

Example 1 with NewContractBean

use of io.apiman.manager.api.beans.contracts.NewContractBean in project apiman by apiman.

the class OrganizationResourceImpl method createContract.

/**
 * @see IOrganizationResource#createContract(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.contracts.NewContractBean)
 */
@Override
public ContractBean createContract(String organizationId, String clientId, String version, NewContractBean bean) throws OrganizationNotFoundException, ClientNotFoundException, ApiNotFoundException, PlanNotFoundException, ContractAlreadyExistsException, NotAuthorizedException {
    securityContext.checkPermissions(PermissionType.clientEdit, organizationId);
    try {
        storage.beginTx();
        ContractBean contract = createContractInternal(organizationId, clientId, version, bean);
        storage.commitTx();
        // $NON-NLS-1$
        log.debug(String.format("Created new contract %s: %s", contract.getId(), contract));
        return contract;
    } catch (AbstractRestException e) {
        storage.rollbackTx();
        throw e;
    } catch (Exception e) {
        storage.rollbackTx();
        // reduce overhead on the typical happy path.
        if (contractAlreadyExists(organizationId, clientId, version, bean)) {
            throw ExceptionFactory.contractAlreadyExistsException();
        } else {
            throw new SystemErrorException(e);
        }
    }
}
Also used : SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) 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) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 2 with NewContractBean

use of io.apiman.manager.api.beans.contracts.NewContractBean 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 3 with NewContractBean

use of io.apiman.manager.api.beans.contracts.NewContractBean in project apiman by apiman.

the class ContractService method createContractInternal.

/**
 * Creates a contract.
 */
protected ContractBean createContractInternal(String clientOrgId, String clientId, String clientVersion, NewContractBean bean) throws Exception {
    ClientVersionBean cvb = clientAppService.getClientVersion(clientOrgId, clientId, clientVersion);
    if (cvb.getStatus() == ClientStatus.Retired) {
        throw ExceptionFactory.invalidClientStatusException();
    }
    ApiVersionBean avb = storage.getApiVersion(bean.getApiOrgId(), bean.getApiId(), bean.getApiVersion());
    if (avb == null) {
        throw ExceptionFactory.apiNotFoundException(bean.getApiId());
    }
    if (avb.getStatus() != ApiStatus.Published) {
        throw ExceptionFactory.invalidApiStatusException();
    }
    Set<ApiPlanBean> plans = Optional.ofNullable(avb.getPlans()).orElse(Collections.emptySet());
    ApiPlanBean apiPlanBean = plans.stream().filter(apb -> apb.getPlanId().equals(bean.getPlanId())).findFirst().orElseThrow(() -> ExceptionFactory.planNotFoundException(bean.getPlanId()));
    PlanVersionBean pvb = planService.getPlanVersion(bean.getApiOrgId(), bean.getPlanId(), apiPlanBean.getVersion());
    if (pvb.getStatus() != PlanStatus.Locked) {
        throw ExceptionFactory.invalidPlanStatusException();
    }
    ContractBean contract = new ContractBean();
    contract.setClient(cvb);
    contract.setApi(avb);
    contract.setPlan(pvb);
    contract.setCreatedBy(securityContext.getCurrentUser());
    contract.setCreatedOn(new Date());
    OrganizationBean planOrg = pvb.getPlan().getOrganization();
    if (!apiPlanBean.isRequiresApproval() || securityContext.hasPermission(planAdmin, planOrg.getId())) {
        LOGGER.debug("Contract valid immediately ✅: {0}", contract);
        contract.setStatus(Created);
    } else {
        LOGGER.debug("Contract requires approval ✋: {0}", contract);
        contract.setStatus(ContractStatus.AwaitingApproval);
    }
    try {
        storage.createContract(contract);
    } catch (IllegalStateException ise) {
        throw ExceptionFactory.contractDuplicateException();
    }
    storage.createAuditEntry(AuditUtils.contractCreatedFromClient(contract, securityContext));
    storage.createAuditEntry(AuditUtils.contractCreatedToApi(contract, securityContext));
    // Determine what status of CVB should be now
    ClientStatus oldStatus = cvb.getStatus();
    ClientStatus newStatus = clientValidator.determineStatus(cvb);
    if (oldStatus != newStatus) {
        cvb.setStatus(newStatus);
        clientAppService.fireClientStatusChangeEvent(cvb, oldStatus);
    }
    // Update the version with new meta-data (e.g. modified-by)
    cvb.setModifiedBy(securityContext.getCurrentUser());
    cvb.setModifiedOn(new Date());
    storage.updateClientVersion(cvb);
    return contract;
}
Also used : ClientVersionBean(io.apiman.manager.api.beans.clients.ClientVersionBean) ClientStatus(io.apiman.manager.api.beans.clients.ClientStatus) ApiPlanBean(io.apiman.manager.api.beans.apis.ApiPlanBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) ApiVersionBean(io.apiman.manager.api.beans.apis.ApiVersionBean) Date(java.util.Date) PlanVersionBean(io.apiman.manager.api.beans.plans.PlanVersionBean) NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 4 with NewContractBean

use of io.apiman.manager.api.beans.contracts.NewContractBean in project apiman by apiman.

the class ContractService method createContract.

public ContractBean createContract(String organizationId, String clientId, String version, NewContractBean bean) throws OrganizationNotFoundException, ClientNotFoundException, ApiNotFoundException, PlanNotFoundException, ContractAlreadyExistsException, NotAuthorizedException {
    ContractBean contract = tryAction(() -> createContractInternal(organizationId, clientId, version, bean));
    // $NON-NLS-1$
    LOGGER.debug("Created new contract {0}: {1}", contract.getId(), contract);
    if (contract.getStatus() == ContractStatus.AwaitingApproval) {
        fireContractApprovalRequest(securityContext.getCurrentUser(), contract);
    }
    return contract;
}
Also used : NewContractBean(io.apiman.manager.api.beans.contracts.NewContractBean) ContractBean(io.apiman.manager.api.beans.contracts.ContractBean)

Example 5 with NewContractBean

use of io.apiman.manager.api.beans.contracts.NewContractBean in project apiman by apiman.

the class ClientAppService method createClientVersion.

public ClientVersionBean createClientVersion(String organizationId, String clientId, NewClientVersionBean bean) throws ClientNotFoundException, NotAuthorizedException, InvalidVersionException, ClientVersionAlreadyExistsException {
    FieldValidator.validateVersion(bean.getVersion());
    ClientVersionBean newVersion;
    try {
        ClientBean client = getClientFromStorage(organizationId, clientId);
        if (storage.getClientVersion(organizationId, clientId, bean.getVersion()) != null) {
            throw ExceptionFactory.clientVersionAlreadyExistsException(clientId, bean.getVersion());
        }
        newVersion = createClientVersionInternal(bean, client);
    } catch (AbstractRestException e) {
        throw e;
    } catch (Exception e) {
        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());
                contractService.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) 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) InvalidClientStatusException(io.apiman.manager.api.rest.exceptions.InvalidClientStatusException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) ClientAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException) InvalidVersionException(io.apiman.manager.api.rest.exceptions.InvalidVersionException) ClientVersionAlreadyExistsException(io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) EntityStillActiveException(io.apiman.manager.api.rest.exceptions.EntityStillActiveException) PolicyNotFoundException(io.apiman.manager.api.rest.exceptions.PolicyNotFoundException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) ClientNotFoundException(io.apiman.manager.api.rest.exceptions.ClientNotFoundException) AbstractRestException(io.apiman.manager.api.rest.exceptions.AbstractRestException) InvalidNameException(io.apiman.manager.api.rest.exceptions.InvalidNameException) ClientVersionNotFoundException(io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException) OrganizationNotFoundException(io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)

Aggregations

NewContractBean (io.apiman.manager.api.beans.contracts.NewContractBean)6 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)4 ContractBean (io.apiman.manager.api.beans.contracts.ContractBean)4 StorageException (io.apiman.manager.api.core.exceptions.StorageException)3 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)3 ClientAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientAlreadyExistsException)3 ClientNotFoundException (io.apiman.manager.api.rest.exceptions.ClientNotFoundException)3 ClientVersionAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ClientVersionAlreadyExistsException)3 ClientVersionNotFoundException (io.apiman.manager.api.rest.exceptions.ClientVersionNotFoundException)3 EntityStillActiveException (io.apiman.manager.api.rest.exceptions.EntityStillActiveException)3 InvalidClientStatusException (io.apiman.manager.api.rest.exceptions.InvalidClientStatusException)3 InvalidNameException (io.apiman.manager.api.rest.exceptions.InvalidNameException)3 InvalidVersionException (io.apiman.manager.api.rest.exceptions.InvalidVersionException)3 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)3 OrganizationNotFoundException (io.apiman.manager.api.rest.exceptions.OrganizationNotFoundException)3 PolicyNotFoundException (io.apiman.manager.api.rest.exceptions.PolicyNotFoundException)3 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)3 NewClientVersionBean (io.apiman.manager.api.beans.clients.NewClientVersionBean)2 GatewayAuthenticationException (io.apiman.manager.api.gateway.GatewayAuthenticationException)2 ApiAlreadyExistsException (io.apiman.manager.api.rest.exceptions.ApiAlreadyExistsException)2