use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class ActionService method retireApi.
/**
* Retires an API that is currently published to the Gateway.
*/
public void retireApi(String orgId, String apiId, String apiVersion) throws ActionException, NotAuthorizedException {
ApiVersionBean versionBean;
try {
versionBean = storage.getApiVersion(orgId, apiId, apiVersion);
} catch (ApiVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
} catch (StorageException e) {
throw new RuntimeException(e);
}
// Validate that it's ok to perform this action - API must be Published.
if (versionBean.getStatus() != ApiStatus.Published) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
Api gatewayApi = new Api();
gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
gatewayApi.setApiId(versionBean.getApi().getId());
gatewayApi.setVersion(versionBean.getVersion());
// Retire the API from all relevant gateways
try {
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.retireApi(gatewayApi);
gatewayLink.close();
}
versionBean.setStatus(ApiStatus.Retired);
versionBean.setRetiredOn(new Date());
ApiBean api = storage.getApi(orgId, apiId);
if (api == null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new PublishingException("Error: could not find API - " + orgId + "=>" + apiId);
}
if (api.getNumPublished() == null || api.getNumPublished() == 0) {
api.setNumPublished(0);
} else {
api.setNumPublished(api.getNumPublished() - 1);
}
storage.updateApi(api);
storage.updateApiVersion(versionBean);
storage.createAuditEntry(AuditUtils.apiRetired(versionBean, securityContext));
} catch (Exception e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("RetireError"), e);
}
LOGGER.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully retired API %s on specified gateways: %s", versionBean.getApi().getName(), versionBean.getApi()));
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean 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.apis.ApiVersionBean in project apiman by apiman.
the class ApiService method setApiDefinition.
private void setApiDefinition(String organizationId, String apiId, String version, ApiDefinitionType definitionType, InputStream data, String definitionUrl) throws StorageException {
ApiVersionBean apiVersion = getApiVersionFromStorage(organizationId, apiId, version);
if (apiVersion.getDefinitionType() != definitionType) {
apiVersion.setDefinitionType(definitionType);
storage.updateApiVersion(apiVersion);
}
// update the definition url silently in storage if it's a new one
if ((definitionUrl != null && (apiVersion.getDefinitionUrl() == null || !apiVersion.getDefinitionUrl().equals(definitionUrl)))) {
apiVersion.setDefinitionUrl(definitionUrl);
storage.updateApiVersion(apiVersion);
}
storage.createAuditEntry(AuditUtils.apiDefinitionUpdated(apiVersion, securityContext));
storage.updateApiDefinition(apiVersion, data);
apiVersion.setModifiedOn(new Date());
apiVersion.setModifiedBy(securityContext.getCurrentUser());
storage.updateApiVersion(apiVersion);
// $NON-NLS-1$
LOGGER.debug(String.format("Stored API definition %s: %s", apiId, apiVersion));
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class ApiService method getApiPolicyChain.
public PolicyChainBean getApiPolicyChain(String organizationId, String apiId, String version, String planId) throws ApiVersionNotFoundException, PlanNotFoundException {
// Try to get the API first - will throw an exception if not found.
ApiVersionBean avb = getApiVersionFromStorage(organizationId, apiId, version);
return tryAction(() -> {
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;
});
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class ApiService method reorderApiPolicies.
public void reorderApiPolicies(String organizationId, String apiId, String version, PolicyChainBean policyChain) throws OrganizationNotFoundException, ApiVersionNotFoundException, NotAuthorizedException {
// Make sure the API exists
ApiVersionBean avb = getApiVersionFromStorage(organizationId, apiId, version);
tryAction(() -> {
List<Long> newOrder = new ArrayList<>(policyChain.getPolicies().size());
for (PolicySummaryBean psb : policyChain.getPolicies()) {
newOrder.add(psb.getId());
}
storage.reorderPolicies(PolicyType.Api, organizationId, apiId, version, newOrder);
storage.createAuditEntry(AuditUtils.policiesReordered(avb, PolicyType.Api, securityContext));
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApiVersion(avb);
});
}
Aggregations