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