use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.
the class OrganizationResourceImpl method getApiPolicyChain.
/**
* @see IOrganizationResource#getApiPolicyChain(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public PolicyChainBean getApiPolicyChain(String organizationId, String apiId, String version, String planId) throws ApiVersionNotFoundException, PlanNotFoundException {
// No permission check is needed, because this would break All APIs UI
// Try to get the API first - will throw an exception if not found.
ApiVersionBean avb = getApiVersion(organizationId, apiId, version);
try {
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;
} catch (StorageException e) {
throw new SystemErrorException(e);
}
}
use of io.apiman.manager.api.beans.apis.ApiPlanBean 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.apis.ApiPlanBean 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.ApiPlanBean in project apiman by apiman.
the class JpaStorage method getApiVersionPlans.
/**
* {@inheritDoc}
*/
@Override
public // TODO(msavy): rewrite using projection
List<ApiPlanSummaryBean> getApiVersionPlans(String organizationId, String apiId, String version) throws StorageException {
List<ApiPlanSummaryBean> plans = new ArrayList<>();
ApiVersionBean versionBean = getApiVersion(organizationId, apiId, version);
Set<ApiPlanBean> apiPlans = versionBean.getPlans();
if (apiPlans != null) {
for (ApiPlanBean spb : apiPlans) {
PlanVersionBean planVersion = getPlanVersion(organizationId, spb.getPlanId(), spb.getVersion());
ApiPlanSummaryBean summary = new ApiPlanSummaryBean();
summary.setPlanId(planVersion.getPlan().getId());
summary.setPlanName(planVersion.getPlan().getName());
summary.setPlanDescription(planVersion.getPlan().getDescription());
summary.setVersion(spb.getVersion());
summary.setRequiresApproval(spb.getRequiresApproval());
summary.setDiscoverability(spb.getDiscoverability());
plans.add(summary);
}
}
return plans;
}
use of io.apiman.manager.api.beans.apis.ApiPlanBean in project apiman by apiman.
the class ApiService method updateApiVersionInternal.
private ApiVersionBean updateApiVersionInternal(ApiVersionBean avb, UpdateApiVersionBean update) throws ApiVersionNotFoundException {
if (avb.getStatus() == ApiStatus.Retired) {
throw ExceptionFactory.invalidApiStatusException();
}
avb.setModifiedBy(securityContext.getCurrentUser());
avb.setModifiedOn(new Date());
EntityUpdatedData data = new EntityUpdatedData();
if (AuditUtils.valueChanged(avb.getPlans(), update.getPlans())) {
Set<ApiPlanBean> updateEntities = update.getPlans().stream().map(dto -> ApiPlanMapper.INSTANCE.fromDto(dto, avb)).collect(Collectors.toSet());
// $NON-NLS-1$
data.addChange("plans", AuditUtils.asString_ApiPlanBeans(avb.getPlans()), AuditUtils.asString_ApiPlanBeans(updateEntities));
if (update.getPlans() != null) {
// Work around: https://hibernate.atlassian.net/browse/HHH-3799
// Step 1: Set intersection
Set<UpdateApiPlanDto> existingAsDto = ApiPlanMapper.INSTANCE.toDto(avb.getPlans());
existingAsDto.retainAll(update.getPlans());
// Step 2: Upsert
Set<ApiPlanBean> mergedPlans = new HashSet<>();
for (ApiPlanBean updateApb : updateEntities) {
existingAsDto.stream().map(e -> ApiPlanMapper.INSTANCE.fromDto(e, avb)).filter(e -> e.equals(updateApb)).findAny().ifPresentOrElse(// Merge (existing element to be updated)
ep -> {
ApiPlanMapper.INSTANCE.merge(updateApb, ep);
mergedPlans.add(ep);
}, // Insert (new element)
() -> {
mergedPlans.add(updateApb);
});
}
avb.setPlans(mergedPlans);
tryAction(() -> storage.merge(avb));
}
}
if (AuditUtils.valueChanged(avb.getGateways(), update.getGateways())) {
// $NON-NLS-1$
data.addChange("gateways", AuditUtils.asString_ApiGatewayBeans(avb.getGateways()), AuditUtils.asString_ApiGatewayBeans(update.getGateways()));
if (avb.getGateways() == null) {
avb.setGateways(new HashSet<>());
}
avb.getGateways().clear();
avb.getGateways().addAll(update.getGateways());
}
if (AuditUtils.valueChanged(avb.getEndpoint(), update.getEndpoint())) {
// validate the endpoint is a URL
validateEndpoint(update.getEndpoint());
// $NON-NLS-1$
data.addChange("endpoint", avb.getEndpoint(), update.getEndpoint());
avb.setEndpoint(update.getEndpoint());
}
if (AuditUtils.valueChanged(avb.getEndpointType(), update.getEndpointType())) {
// $NON-NLS-1$
data.addChange("endpointType", avb.getEndpointType(), update.getEndpointType());
avb.setEndpointType(update.getEndpointType());
}
if (AuditUtils.valueChanged(avb.getEndpointContentType(), update.getEndpointContentType())) {
// $NON-NLS-1$
data.addChange("endpointContentType", avb.getEndpointContentType(), update.getEndpointContentType());
avb.setEndpointContentType(update.getEndpointContentType());
}
if (AuditUtils.valueChanged(avb.getEndpointProperties(), update.getEndpointProperties())) {
if (avb.getEndpointProperties() == null) {
avb.setEndpointProperties(new HashMap<>());
} else {
avb.getEndpointProperties().clear();
}
if (update.getEndpointProperties() != null) {
avb.getEndpointProperties().putAll(update.getEndpointProperties());
}
}
if (AuditUtils.valueChanged(avb.isPublicAPI(), update.getPublicAPI())) {
// $NON-NLS-1$
data.addChange("publicAPI", String.valueOf(avb.isPublicAPI()), String.valueOf(update.getPublicAPI()));
avb.setPublicAPI(update.getPublicAPI());
}
if (AuditUtils.valueChanged(avb.isParsePayload(), update.getParsePayload())) {
// $NON-NLS-1$
data.addChange("parsePayload", String.valueOf(avb.isParsePayload()), String.valueOf(update.getParsePayload()));
avb.setParsePayload(update.getParsePayload());
}
if (AuditUtils.valueChanged(avb.getDisableKeysStrip(), update.getDisableKeysStrip())) {
// $NON-NLS-1$
data.addChange("disableKeysStrip", String.valueOf(avb.getDisableKeysStrip()), String.valueOf(update.getDisableKeysStrip()));
avb.setDisableKeysStrip(update.getDisableKeysStrip());
}
if (AuditUtils.valueChanged(avb.getExtendedDescription(), update.getExtendedDescription())) {
// $NON-NLS-1$
data.addChange("extendedDescription", String.valueOf(avb.getExtendedDescription()), String.valueOf(update.getExtendedDescription()));
avb.setExtendedDescription(update.getExtendedDescription());
}
if (AuditUtils.valueChanged(avb.getDiscoverability(), update.getPublicDiscoverability())) {
// $NON-NLS-1$
data.addChange("discoverability", String.valueOf(avb.getDiscoverability()), String.valueOf(update.getPublicDiscoverability()));
avb.setDiscoverability(update.getPublicDiscoverability());
}
return tryAction(() -> {
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();
}
}
encryptEndpointProperties(avb);
// Ensure all 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));
// $NON-NLS-1$
LOGGER.debug(String.format("Successfully updated API Version: %s", avb));
decryptEndpointProperties(avb);
return avb;
});
}
Aggregations