use of io.apiman.manager.api.beans.clients.ClientStatus in project apiman by apiman.
the class ClientAppService method changeStatus.
public void changeStatus(ClientVersionBean cvb, ClientStatus newStatus) {
ClientStatus oldStatus = cvb.getStatus();
cvb.setStatus(newStatus);
fireClientStatusChangeEvent(cvb, oldStatus);
LOGGER.debug("Change status of client version {0} -> {1}: {2}", oldStatus, newStatus, cvb);
tryAction(() -> storage.updateClientVersion(cvb));
}
use of io.apiman.manager.api.beans.clients.ClientStatus 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.clients.ClientStatus in project apiman by apiman.
the class ActionService method unregisterClient.
/**
* De-registers an client that is currently registered with the gateway.
*/
public void unregisterClient(String orgId, String clientId, String clientVersion) throws ActionException, NotAuthorizedException {
ClientVersionBean versionBean;
List<ContractSummaryBean> contractBeans;
try {
versionBean = clientAppService.getClientVersion(orgId, clientId, clientVersion);
} catch (ClientVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"));
}
try {
contractBeans = query.getClientContracts(orgId, clientId, clientVersion).stream().peek(c -> {
if (c.getStatus() != ContractStatus.Created) {
LOGGER.debug("Will not try to delete contract {0} from gateway(s) as it is not in 'Created' state", c);
}
}).filter(c -> c.getStatus() == ContractStatus.Created).collect(Collectors.toList());
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
}
// Validate that it's ok to perform this action - client must be either registered or awaiting approval (or there's nothing to unregister)
if (versionBean.getStatus() != ClientStatus.Registered && versionBean.getStatus() != ClientStatus.AwaitingApproval) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidClientStatus"));
}
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
// Each of those gateways must be told about the client.
try {
Map<String, IGatewayLink> links = new HashMap<>();
for (ContractSummaryBean contractBean : contractBeans) {
ApiVersionBean svb = storage.getApiVersion(contractBean.getApiOrganizationId(), contractBean.getApiId(), contractBean.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
}
for (ApiGatewayBean apiGatewayBean : gateways) {
if (!links.containsKey(apiGatewayBean.getGatewayId())) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
links.put(apiGatewayBean.getGatewayId(), gatewayLink);
}
}
}
for (IGatewayLink gatewayLink : links.values()) {
gatewayLink.unregisterClient(client);
gatewayLink.close();
}
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
}
ClientStatus oldStatus = versionBean.getStatus();
versionBean.setStatus(ClientStatus.Retired);
versionBean.setRetiredOn(new Date());
clientAppService.fireClientStatusChangeEvent(versionBean, oldStatus);
try {
storage.updateClientVersion(versionBean);
storage.createAuditEntry(AuditUtils.clientUnregistered(versionBean, securityContext));
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("UnregisterError"), e);
}
LOGGER.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
use of io.apiman.manager.api.beans.clients.ClientStatus in project apiman by apiman.
the class EntityValidator method determineStatus.
@Override
public ClientStatus determineStatus(ClientVersionBean cvb, List<ContractBean> contracts) {
ClientStatus currentStatus = cvb.getStatus();
boolean anyAwaitingApproval = contracts.stream().anyMatch(c -> c.getStatus() == ContractStatus.AwaitingApproval);
if (anyAwaitingApproval) {
return ClientStatus.AwaitingApproval;
}
// If already registered, then continue to be registered (indicates some number of contracts are still active on gateway).
if (currentStatus == ClientStatus.Registered) {
return ClientStatus.Registered;
}
// No contracts and not registered, then just created state
if (contracts.isEmpty()) {
return ClientStatus.Created;
}
// Ready to be published
return ClientStatus.Ready;
}
use of io.apiman.manager.api.beans.clients.ClientStatus in project apiman by apiman.
the class ActionService method registerClient.
/**
* Registers a client (along with all of its contracts) to the gateway.
*/
public void registerClient(String orgId, String clientId, String clientVersion) throws ActionException, NotAuthorizedException {
ClientVersionBean versionBean;
List<ContractSummaryBean> contractBeans;
try {
versionBean = clientAppService.getClientVersion(orgId, clientId, clientVersion);
} catch (ClientVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("clientVersionDoesNotExist", clientId, clientVersion));
}
try {
contractBeans = query.getClientContracts(orgId, clientId, clientVersion);
// Any awaiting approval then don't let them republish.
List<ContractSummaryBean> awaitingApproval = contractBeans.stream().filter(f -> f.getStatus() == ContractStatus.AwaitingApproval).collect(Collectors.toList());
if (!awaitingApproval.isEmpty()) {
throw ExceptionFactory.contractNotYetApprovedException(awaitingApproval);
}
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ClientNotFound"), e);
}
boolean isReregister = false;
// Validate that it's ok to perform this action
if (versionBean.getStatus() == ClientStatus.Registered) {
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("ClientReRegisterNotRequired"));
}
isReregister = true;
}
Client client = new Client();
client.setOrganizationId(versionBean.getClient().getOrganization().getId());
client.setClientId(versionBean.getClient().getId());
client.setVersion(versionBean.getVersion());
client.setApiKey(versionBean.getApikey());
Set<Contract> contracts = new HashSet<>();
for (ContractSummaryBean contractBean : contractBeans) {
Contract contract = new Contract();
contract.setPlan(contractBean.getPlanId());
contract.setApiId(contractBean.getApiId());
contract.setApiOrgId(contractBean.getApiOrganizationId());
contract.setApiVersion(contractBean.getApiVersion());
contract.getPolicies().addAll(contractService.aggregateContractPolicies(contractBean));
contracts.add(contract);
}
client.setContracts(contracts);
// Each of those gateways must be told about the client.
try {
Map<String, IGatewayLink> links = new HashMap<>();
for (Contract contract : client.getContracts()) {
ApiVersionBean svb = storage.getApiVersion(contract.getApiOrgId(), contract.getApiId(), contract.getApiVersion());
Set<ApiGatewayBean> gateways = svb.getGateways();
if (gateways == null) {
// $NON-NLS-1$
throw new PublishingException("No gateways specified for API: " + svb.getApi().getName());
}
for (ApiGatewayBean apiGatewayBean : gateways) {
if (!links.containsKey(apiGatewayBean.getGatewayId())) {
IGatewayLink gatewayLink = createGatewayLink(apiGatewayBean.getGatewayId());
links.put(apiGatewayBean.getGatewayId(), gatewayLink);
}
}
}
if (isReregister) {
// Once we figure out which gateways to register with, make sure we also "unregister"
// the client app from all other gateways. This is necessary because we may have broken
// contracts we previously had on APIs that are published to other gateways. And thus
// it's possible we need to remove a contract from a Gateway that is not otherwise/currently
// referenced.
//
// This is a fix for: https://issues.jboss.org/browse/APIMAN-895
Iterator<GatewayBean> gateways = storage.getAllGateways();
while (gateways.hasNext()) {
GatewayBean gbean = gateways.next();
if (!links.containsKey(gbean.getId())) {
IGatewayLink gatewayLink = createGatewayLink(gbean.getId());
try {
gatewayLink.unregisterClient(client);
} catch (Exception e) {
// We need to catch the error, but ignore it,
// in the event that the gateway is invalid.
}
gatewayLink.close();
}
}
}
for (IGatewayLink gatewayLink : links.values()) {
gatewayLink.registerClient(client);
gatewayLink.close();
}
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
ClientStatus oldStatus = versionBean.getStatus();
versionBean.setStatus(ClientStatus.Registered);
versionBean.setPublishedOn(new Date());
try {
storage.updateClientVersion(versionBean);
storage.createAuditEntry(AuditUtils.clientRegistered(versionBean, securityContext));
clientAppService.fireClientStatusChangeEvent(versionBean, oldStatus);
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
LOGGER.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully registered Client %s on specified gateways: %s", versionBean.getClient().getName(), versionBean.getClient()));
}
Aggregations