use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.
the class OrganizationResourceImpl method createApiVersionInternal.
/**
* Creates an API version.
* @param bean
* @param api
* @param gateway
* @throws Exception
* @throws StorageException
*/
protected ApiVersionBean createApiVersionInternal(NewApiVersionBean bean, ApiBean api, GatewaySummaryBean gateway) throws Exception, StorageException {
if (!BeanUtils.isValidVersion(bean.getVersion())) {
// $NON-NLS-1$
throw new StorageException("Invalid/illegal API version: " + bean.getVersion());
}
ApiVersionBean newVersion = new ApiVersionBean();
newVersion.setVersion(bean.getVersion());
newVersion.setCreatedBy(securityContext.getCurrentUser());
newVersion.setCreatedOn(new Date());
newVersion.setModifiedBy(securityContext.getCurrentUser());
newVersion.setModifiedOn(new Date());
newVersion.setStatus(ApiStatus.Created);
newVersion.setApi(api);
newVersion.setEndpoint(bean.getEndpoint());
newVersion.setEndpointType(bean.getEndpointType());
newVersion.setEndpointContentType(bean.getEndpointContentType());
newVersion.setDefinitionUrl(bean.getDefinitionUrl());
if (bean.getPublicAPI() != null) {
newVersion.setPublicAPI(bean.getPublicAPI());
}
if (bean.getParsePayload() != null) {
newVersion.setParsePayload(bean.getParsePayload());
}
if (bean.getDisableKeysStrip() != null) {
newVersion.setDisableKeysStrip(bean.getDisableKeysStrip());
}
if (bean.getPlans() != null) {
newVersion.setPlans(bean.getPlans());
}
if (bean.getDefinitionType() != null) {
newVersion.setDefinitionType(bean.getDefinitionType());
} else {
newVersion.setDefinitionType(ApiDefinitionType.None);
}
if (gateway != null && newVersion.getGateways() == null) {
newVersion.setGateways(new HashSet<>());
ApiGatewayBean sgb = new ApiGatewayBean();
sgb.setGatewayId(gateway.getId());
newVersion.getGateways().add(sgb);
}
if (apiValidator.isReady(newVersion)) {
newVersion.setStatus(ApiStatus.Ready);
} else {
newVersion.setStatus(ApiStatus.Created);
}
// Ensure all of the plans are in the right status (locked)
Set<ApiPlanBean> plans = newVersion.getPlans();
if (plans != null) {
for (ApiPlanBean splanBean : plans) {
String orgId = newVersion.getApi().getOrganization().getId();
PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
if (pvb == null) {
// $NON-NLS-1$
throw new StorageException(Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion()));
}
if (pvb.getStatus() != PlanStatus.Locked) {
// $NON-NLS-1$
throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion()));
}
}
}
storage.createApiVersion(newVersion);
if (bean.getDefinitionUrl() != null) {
InputStream definition = null;
try {
definition = new URL(bean.getDefinitionUrl()).openStream();
storage.updateApiDefinition(newVersion, definition);
} catch (Exception e) {
// $NON-NLS-1$
log.error("Unable to store API definition from: " + bean.getDefinitionUrl(), e);
// Set definition type silently to None
newVersion.setDefinitionType(ApiDefinitionType.None);
storage.updateApiVersion(newVersion);
} finally {
IOUtils.closeQuietly(definition);
}
}
storage.createAuditEntry(AuditUtils.apiVersionCreated(newVersion, securityContext));
return newVersion;
}
use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.
the class OrganizationResourceImpl method createContractInternal.
/**
* Creates a contract.
* @param organizationId
* @param clientId
* @param version
* @param bean
* @throws StorageException
* @throws Exception
*/
protected ContractBean createContractInternal(String organizationId, String clientId, String version, NewContractBean bean) throws StorageException, Exception {
ClientVersionBean cvb = getClientVersionFromStorage(organizationId, clientId, version);
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 = avb.getPlans();
String planVersion = null;
if (plans != null) {
for (ApiPlanBean apiPlanBean : plans) {
if (apiPlanBean.getPlanId().equals(bean.getPlanId())) {
planVersion = apiPlanBean.getVersion();
}
}
}
if (planVersion == null) {
throw ExceptionFactory.planNotFoundException(bean.getPlanId());
}
PlanVersionBean pvb = getPlanVersionFromStorage(bean.getApiOrgId(), bean.getPlanId(), planVersion);
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());
// Move the client to the "Ready" state if necessary.
if (cvb.getStatus() == ClientStatus.Created && clientValidator.isReady(cvb, true)) {
cvb.setStatus(ClientStatus.Ready);
}
storage.createContract(contract);
storage.createAuditEntry(AuditUtils.contractCreatedFromClient(contract, securityContext));
storage.createAuditEntry(AuditUtils.contractCreatedToApi(contract, securityContext));
// 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.apis.ApiPlanBean in project apiman by apiman.
the class OrganizationResourceImpl method updateApiVersion.
/**
* @see IOrganizationResource#updateApiVersion(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.apis.UpdateApiVersionBean)
*/
@Override
public ApiVersionBean updateApiVersion(String organizationId, String apiId, String version, UpdateApiVersionBean bean) throws ApiVersionNotFoundException, NotAuthorizedException {
securityContext.checkPermissions(PermissionType.apiEdit, organizationId);
ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
if (avb.isPublicAPI()) {
if (avb.getStatus() == ApiStatus.Retired) {
throw ExceptionFactory.invalidApiStatusException();
}
} else {
if (avb.getStatus() == ApiStatus.Published || avb.getStatus() == ApiStatus.Retired) {
throw ExceptionFactory.invalidApiStatusException();
}
}
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
EntityUpdatedData data = new EntityUpdatedData();
if (AuditUtils.valueChanged(avb.getPlans(), bean.getPlans())) {
// $NON-NLS-1$
data.addChange("plans", AuditUtils.asString_ApiPlanBeans(avb.getPlans()), AuditUtils.asString_ApiPlanBeans(bean.getPlans()));
if (avb.getPlans() == null) {
avb.setPlans(new HashSet<>());
}
avb.getPlans().clear();
if (bean.getPlans() != null) {
avb.getPlans().addAll(bean.getPlans());
}
}
if (AuditUtils.valueChanged(avb.getGateways(), bean.getGateways())) {
// $NON-NLS-1$
data.addChange("gateways", AuditUtils.asString_ApiGatewayBeans(avb.getGateways()), AuditUtils.asString_ApiGatewayBeans(bean.getGateways()));
if (avb.getGateways() == null) {
avb.setGateways(new HashSet<>());
}
avb.getGateways().clear();
avb.getGateways().addAll(bean.getGateways());
}
if (AuditUtils.valueChanged(avb.getEndpoint(), bean.getEndpoint())) {
// validate the endpoint is a URL
validateEndpoint(bean.getEndpoint());
// $NON-NLS-1$
data.addChange("endpoint", avb.getEndpoint(), bean.getEndpoint());
avb.setEndpoint(bean.getEndpoint());
}
if (AuditUtils.valueChanged(avb.getEndpointType(), bean.getEndpointType())) {
// $NON-NLS-1$
data.addChange("endpointType", avb.getEndpointType(), bean.getEndpointType());
avb.setEndpointType(bean.getEndpointType());
}
if (AuditUtils.valueChanged(avb.getEndpointContentType(), bean.getEndpointContentType())) {
// $NON-NLS-1$
data.addChange("endpointContentType", avb.getEndpointContentType(), bean.getEndpointContentType());
avb.setEndpointContentType(bean.getEndpointContentType());
}
if (AuditUtils.valueChanged(avb.getEndpointProperties(), bean.getEndpointProperties())) {
if (avb.getEndpointProperties() == null) {
avb.setEndpointProperties(new HashMap<>());
} else {
avb.getEndpointProperties().clear();
}
if (bean.getEndpointProperties() != null) {
avb.getEndpointProperties().putAll(bean.getEndpointProperties());
}
}
if (AuditUtils.valueChanged(avb.isPublicAPI(), bean.getPublicAPI())) {
// $NON-NLS-1$
data.addChange("publicAPI", String.valueOf(avb.isPublicAPI()), String.valueOf(bean.getPublicAPI()));
avb.setPublicAPI(bean.getPublicAPI());
}
if (AuditUtils.valueChanged(avb.isParsePayload(), bean.getParsePayload())) {
// $NON-NLS-1$
data.addChange("parsePayload", String.valueOf(avb.isParsePayload()), String.valueOf(bean.getParsePayload()));
avb.setParsePayload(bean.getParsePayload());
}
if (AuditUtils.valueChanged(avb.getDisableKeysStrip(), bean.getDisableKeysStrip())) {
// $NON-NLS-1$
data.addChange("disableKeysStrip", String.valueOf(avb.getDisableKeysStrip()), String.valueOf(bean.getDisableKeysStrip()));
avb.setDisableKeysStrip(bean.getDisableKeysStrip());
}
try {
if (avb.getGateways() == null || avb.getGateways().isEmpty()) {
GatewaySummaryBean gateway = getSingularGateway();
if (gateway != null && avb.getGateways() == null) {
avb.setGateways(new HashSet<>());
ApiGatewayBean sgb = new ApiGatewayBean();
sgb.setGatewayId(gateway.getId());
avb.getGateways().add(sgb);
}
}
if (avb.getStatus() != ApiStatus.Published) {
if (apiValidator.isReady(avb)) {
avb.setStatus(ApiStatus.Ready);
} else {
avb.setStatus(ApiStatus.Created);
}
} else {
if (!apiValidator.isReady(avb)) {
throw ExceptionFactory.invalidApiStatusException();
}
}
} catch (Exception e) {
throw new SystemErrorException(e);
}
try {
encryptEndpointProperties(avb);
storage.beginTx();
// Ensure all of the plans are in the right status (locked)
Set<ApiPlanBean> plans = avb.getPlans();
if (plans != null) {
for (ApiPlanBean splanBean : plans) {
String orgId = avb.getApi().getOrganization().getId();
PlanVersionBean pvb = storage.getPlanVersion(orgId, splanBean.getPlanId(), splanBean.getVersion());
if (pvb == null) {
// $NON-NLS-1$
throw new StorageException(Messages.i18n.format("PlanVersionDoesNotExist", splanBean.getPlanId(), splanBean.getVersion()));
}
if (pvb.getStatus() != PlanStatus.Locked) {
// $NON-NLS-1$
throw new StorageException(Messages.i18n.format("PlanNotLocked", splanBean.getPlanId(), splanBean.getVersion()));
}
}
}
storage.updateApiVersion(avb);
storage.createAuditEntry(AuditUtils.apiVersionUpdated(avb, data, securityContext));
storage.commitTx();
// $NON-NLS-1$
log.debug(String.format("Successfully updated API Version: %s", avb));
decryptEndpointProperties(avb);
return avb;
} catch (AbstractRestException e) {
storage.rollbackTx();
throw e;
} catch (Exception e) {
storage.rollbackTx();
throw new SystemErrorException(e);
}
}
Aggregations