use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class ActionResourceImpl method publishApi.
/**
* Publishes an API to the gateway.
* @param action
*/
private void publishApi(ActionBean action) throws ActionException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiAdmin, action.getOrganizationId());
ApiVersionBean versionBean;
try {
versionBean = orgs.getApiVersion(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion());
} catch (ApiVersionNotFoundException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("ApiNotFound"));
}
// Validate that it's ok to perform this action - API must be Ready.
if (!versionBean.isPublicAPI() && versionBean.getStatus() != ApiStatus.Ready) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.isPublicAPI()) {
if (versionBean.getStatus() == ApiStatus.Retired || versionBean.getStatus() == ApiStatus.Created) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("InvalidApiStatus"));
}
if (versionBean.getStatus() == ApiStatus.Published) {
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("ApiRePublishNotRequired"));
}
}
}
Api gatewayApi = new Api();
gatewayApi.setEndpoint(versionBean.getEndpoint());
gatewayApi.setEndpointType(versionBean.getEndpointType().toString());
if (versionBean.getEndpointContentType() != null) {
gatewayApi.setEndpointContentType(versionBean.getEndpointContentType().toString());
}
gatewayApi.setEndpointProperties(versionBean.getEndpointProperties());
gatewayApi.setOrganizationId(versionBean.getApi().getOrganization().getId());
gatewayApi.setApiId(versionBean.getApi().getId());
gatewayApi.setVersion(versionBean.getVersion());
gatewayApi.setPublicAPI(versionBean.isPublicAPI());
gatewayApi.setParsePayload(versionBean.isParsePayload());
gatewayApi.setKeysStrippingDisabled(versionBean.getDisableKeysStrip());
boolean hasTx = false;
try {
if (versionBean.isPublicAPI()) {
List<Policy> policiesToPublish = new ArrayList<>();
List<PolicySummaryBean> apiPolicies = query.getPolicies(action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), PolicyType.Api);
storage.beginTx();
hasTx = true;
for (PolicySummaryBean policySummaryBean : apiPolicies) {
PolicyBean apiPolicy = storage.getPolicy(PolicyType.Api, action.getOrganizationId(), action.getEntityId(), action.getEntityVersion(), policySummaryBean.getId());
Policy policyToPublish = new Policy();
policyToPublish.setPolicyJsonConfig(apiPolicy.getConfiguration());
policyToPublish.setPolicyImpl(apiPolicy.getDefinition().getPolicyImpl());
policiesToPublish.add(policyToPublish);
}
gatewayApi.setApiPolicies(policiesToPublish);
}
} catch (StorageException e) {
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
} finally {
if (hasTx) {
storage.rollbackTx();
}
}
// Publish the API to all relevant gateways
try {
storage.beginTx();
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.publishApi(gatewayApi);
gatewayLink.close();
}
versionBean.setStatus(ApiStatus.Published);
versionBean.setPublishedOn(new Date());
ApiBean api = storage.getApi(action.getOrganizationId(), action.getEntityId());
if (api == null) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new PublishingException("Error: could not find API - " + action.getOrganizationId() + "=>" + action.getEntityId());
}
if (api.getNumPublished() == null) {
api.setNumPublished(1);
} else {
api.setNumPublished(api.getNumPublished() + 1);
}
storage.updateApi(api);
storage.updateApiVersion(versionBean);
storage.createAuditEntry(AuditUtils.apiPublished(versionBean, securityContext));
storage.commitTx();
} catch (PublishingException e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
} catch (Exception e) {
storage.rollbackTx();
// $NON-NLS-1$
throw ExceptionFactory.actionException(Messages.i18n.format("PublishError"), e);
}
log.debug(// $NON-NLS-1$
String.format(// $NON-NLS-1$
"Successfully published 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 DeveloperResourceImpl method getAllPublicApiVersions.
@Override
public List<ApiVersionBean> getAllPublicApiVersions() throws NotAuthorizedException {
List<ApiVersionBean> apiVersionBeans = new ArrayList<>();
Iterator<ApiVersionBean> iterator;
try {
storage.beginTx();
iterator = storage.getAllPublicApiVersions();
storage.commitTx();
while (iterator.hasNext()) {
ApiVersionBean apiVersionBean = iterator.next();
apiVersionBeans.add(apiVersionBean);
}
} catch (StorageException e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
return apiVersionBeans;
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class OrganizationResourceImpl method updateApiPolicy.
/**
* @see IOrganizationResource#updateApiPolicy(java.lang.String,
* java.lang.String, java.lang.String, long, io.apiman.manager.api.beans.policies.UpdatePolicyBean)
*/
@Override
public void updateApiPolicy(String organizationId, String apiId, String version, long policyId, UpdatePolicyBean bean) throws OrganizationNotFoundException, ApiVersionNotFoundException, PolicyNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
// Make sure the API exists
ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
try {
storage.beginTx();
PolicyBean policy = storage.getPolicy(PolicyType.Api, organizationId, apiId, version, policyId);
if (policy == null) {
throw ExceptionFactory.policyNotFoundException(policyId);
}
// TODO capture specific change values when auditing policy updates
if (AuditUtils.valueChanged(policy.getConfiguration(), bean.getConfiguration())) {
policy.setConfiguration(bean.getConfiguration());
}
policy.setModifiedOn(new Date());
policy.setModifiedBy(securityContext.getCurrentUser());
storage.updatePolicy(policy);
storage.createAuditEntry(AuditUtils.policyUpdated(policy, PolicyType.Api, securityContext));
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
storage.updateApiVersion(avb);
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Updated API policy %s", policy));
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class OrganizationResourceImpl method getApiVersionStatus.
/**
* @see IOrganizationResource#getApiVersionStatus(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public ApiVersionStatusBean getApiVersionStatus(String organizationId, String apiId, String version) throws ApiVersionNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiView, organizationId);
ApiVersionBean versionBean = getApiVersion(organizationId, apiId, version);
List<PolicySummaryBean> policies = listApiPolicies(organizationId, apiId, version);
return apiValidator.getStatus(versionBean, policies);
}
use of io.apiman.manager.api.beans.apis.ApiVersionBean in project apiman by apiman.
the class ContractService method probePolicy.
// TODO make properly optimised query for this
public List<IPolicyProbeResponse> probePolicy(Long contractId, long policyId, String rawPayload) throws ClientNotFoundException, ContractNotFoundException {
ContractBean contract = getContract(contractId);
ApiVersionBean avb = contract.getApi();
OrganizationBean apiOrg = avb.getApi().getOrganization();
String apiKey = contract.getClient().getApikey();
Set<String> gatewayIds = contract.getApi().getGateways().stream().map(ApiGatewayBean::getGatewayId).collect(Collectors.toSet());
if (gatewayIds.size() == 0) {
return List.of();
}
List<PolicyBean> policyChain = aggregateContractPolicies(contract);
int idxFound = -1;
for (int i = 0, policyChainSize = policyChain.size(); i < policyChainSize; i++) {
PolicyBean policy = policyChain.get(i);
if (policy.getId().equals(policyId)) {
idxFound = i;
}
}
if (idxFound == -1) {
throw new IllegalArgumentException("Provided policy ID not found in contract " + policyId);
}
List<GatewayBean> gateways = tryAction(() -> storage.getGateways(gatewayIds));
LOGGER.debug("Gateways for contract {0}: {1}", contractId, gateways);
List<IPolicyProbeResponse> probeResponses = new ArrayList<>(gateways.size());
for (GatewayBean gateway : gateways) {
IGatewayLink link = gatewayLinkFactory.create(gateway);
try {
probeResponses.add(link.probe(apiOrg.getId(), avb.getApi().getId(), avb.getVersion(), idxFound, apiKey, rawPayload));
} catch (GatewayAuthenticationException e) {
throw new SystemErrorException(e);
}
}
LOGGER.debug("Probe responses for contract {0}: {1}", contractId, probeResponses);
return probeResponses;
}
Aggregations