use of io.apiman.manager.api.beans.summary.ContractSummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method getApiVersionContracts.
/**
* @see IOrganizationResource#getApiVersionContracts(java.lang.String, java.lang.String, java.lang.String, int, int)
*/
@Override
public List<ContractSummaryBean> getApiVersionContracts(String organizationId, String apiId, String version, int page, int pageSize) throws ApiVersionNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiView, organizationId);
if (page <= 1) {
page = 1;
}
if (pageSize == 0) {
pageSize = 20;
}
// Try to get the API first - will throw an exception if not found.
getApiVersion(organizationId, apiId, version);
try {
List<ContractSummaryBean> contracts = query.getContracts(organizationId, apiId, version, page, pageSize);
// $NON-NLS-1$
log.debug(String.format("Got API %s version %s contracts: %s", apiId, version, contracts));
return contracts;
} catch (StorageException e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.summary.ContractSummaryBean 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.summary.ContractSummaryBean in project apiman by apiman.
the class ActionResourceImpl method registerClient.
/**
* Registers an client (along with all of its contracts) to the gateway.
* @param action
*/
private void registerClient(ActionBean action) throws ActionException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.clientAdmin, action.getOrganizationId());
ClientVersionBean versionBean;
List<ContractSummaryBean> contractBeans;
try {
versionBean = orgs.getClientVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} catch (ClientVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("clientVersionDoesNotExist", action.getEntityId(), action.getEntityVersion()));
}
try {
contractBeans = query.getClientContracts(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} 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(aggregateContractPolicies(contractBean));
contracts.add(contract);
}
client.setContracts(contracts);
// Each of those gateways must be told about the client.
try {
storage.beginTx();
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();
}
storage.commitTx();
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
versionBean.setStatus(ClientStatus.Registered);
versionBean.setPublishedOn(new Date());
try {
storage.beginTx();
storage.updateClientVersion(versionBean);
storage.createAuditEntry(AuditUtils.clientRegistered(versionBean, securityContext));
storage.commitTx();
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RegisterError"), e);
}
log.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.summary.ContractSummaryBean in project apiman by apiman.
the class OrganizationResourceImpl method getClientVersionContracts.
/**
* @see IOrganizationResource#getClientVersionContracts(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public List<ContractSummaryBean> getClientVersionContracts(String organizationId, String clientId, String version) throws ClientNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.clientView, organizationId);
// Try to get the client first - will throw a ClientNotFoundException if not found.
getClientVersionInternal(organizationId, clientId, version);
try {
List<ContractSummaryBean> contracts = query.getClientContracts(organizationId, clientId, version);
return contracts;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.summary.ContractSummaryBean 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()));
}
Aggregations